I have a table which stores Finance Fiscal Period Close dates.
I wanted to run a stored procedure on those dates. How can I link the database table to pick the date and time from those tables and run the job accordingly?
These dates usually get populated at the beginning of the FY but can get update between.
You can create a job that runs daily with a T-SQL Script step that executes something like this:
IF EXISTS(SELECT *
FROM FinanceFiscalPeriodCloseDates
WHERE CloseDate = CAST(GETDATE() AS DATE())
EXECUTE SomeProcedure;
If you don't know how to create and schedule a job, then the problem might be broader.
You cannot create a dynamic schedule on which a job will run.
What you can do is schedule the job to run every day, and have the first step of the stored proc be to check if today's date is in the table. If yes, then proceed with the other steps. If no, then don't do anything.
Related
I have SQL Server Agent Job on my System that copies data into at table for later evaluation purposes. The Job runs on two types of schedules every Friday every week and last day of the month. The target data records should also contain a column indicating the schedule that originally triggered the job. But I found no way so far to receive this data as parameter or so. I'm using a Microsoft SQL Server 2017.
I did a web search but maybe searched for the wrong keywords. I also thought about comparing current time to expected runtime per schedule but that seemed to be not a fault tolerant option to me.
I like to fill a column "schedule" with values like "End of week", "End of month"
sys tables are your friend here. Documentation
sysjobs has your job information.
sysjobschedules links your job to its schedule.
sysschedules has your schedule info.
SELECT j.*
, s.*
FROM sysjobs j
JOIN sysjobschedules js ON j.id = js.job_id
JOIN sysschedules s ON js.schedule_id = s.schedule_id
WHERE j.name = 'your job name here'
After long search and analyzing I finally found a solution that at least fit my needs:
The undocumented and unsupport stored procedures provides the schedule that triggered a job ind Column Request Source ID:
EXEC master.dbo.xp_sqlagent_enum_jobs 1, garbage
see also: https://am2.co/2016/02/xp_sqlagent_enum_jobs_alt/
So i'm doing this SQL SERVER school project and i want to add to a specific user the right to select for a limited amount of time lets say till 24 September 2016.
Is it possible and if yes how ?
THANKS!
There is no provision to grant access for a limited amount of time.
Best of luck.
The only way I can think of is creating a SQL Server Agent Job.
Create 2 steps in the job one for adding the user to database role db_datareader and then anotehr step to take away the role membership of that user.
The step to grant user permission should look something like
IF (GETDATE() >= '2016-09-24 08:58:59.000'
AND GETDATE() <= '2016-09-24 09:01:00.000')
BEGIN
EXEC sp_addrolemember N'db_datareader', N'UserA'
END
The second step to revoke user's permission should look something like
IF (GETDATE() >= '2016-09-24 16:58:59.000')
BEGIN
EXEC sp_droprolemember N'db_datareader', N'UserA'
END
Schedule the job to run twice a day. Occurrence every 8 hours. Assuming you only want the user to have access for 8 hours.
In the start date and end date options use 24 September 2016. So the Job is only executed on that date and never before or after that date.
Make sure that the interval between occurrences matches the logic in your IF statement of each step.
The job database context should be the database where your user need permissions.
I'd write a job that will run on that date that revokes the user's right.
I would NOT modify the schema to handle a one-off case. Control of rights to the database should be external to the database.
You can create a new column with a date datatype and call it endDate. You'd insert September 24, 2016 into endDate for a user and (depending on how you're accessing it) simply use an if statement comparing the current date and the end date. For example :-
if(CURDATE() < endDate)
For the purpose of my project I cannot use session based temp tables. They need to be persistent but automatically deleted after a certain period of inactivity (no CRUD performed). Is this at all possible?
You can use the SQL Server Agent to Schedule a Job that calls a Stored Procedure that does this work for you. (How to Schedule a Job?)
How do you identify the tables that have not updated since X amount of time ?
Use this Query:
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, last_user_update,
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID('DatabaseName')
AND OBJECT_NAME(OBJECT_ID) LIKE '%%' -- Here is the template name for your tables
AND DATEDIFF(MINUTE, last_user_update, GETDATE()) > 10 -- Last updated more than 10 minutes
Now that you have the tables to be deleted, you can use whatever logic you want to DROP them (Cursor, While, Procedure)
Sure it is. Write it into your program layer.
AUTOMATICALLY - within SQL Server: no. Well, you cold use the agent to start a script regularly.
Tracking what "inactivity" means - your responsibility.
You need save modification date of this table somewhere (for example in the same table or in another special table) and then you can create job, which checks last modification date and then drops the table.
This trigger backs up data from dbo.node to dbo.nodearchive. While backups are important, I only need to do this once per day. Note that there is a field called dbo.NodeArchive.versionDate (smalldDatetime).
CREATE TRIGGER [dbo].[Node_update]
ON [dbo].[Node]
for UPDATE
AS
BEGIN
INSERT INTO dbo.NodeArchive ([NodeID]
,[ParentNodeID]
,[Slug]
,[xmlTitle]
...
,[ModifyBy]
,[ModifyDate]
,[CreateBy]
,[CreateDate])
SELECT [deleted].[NodeID]
,[deleted].[ParentNodeID]
,[deleted].[Slug]
,[deleted].[xmlTitle]
...
,[deleted].[ModifyBy]
,[deleted].[ModifyDate]
,[deleted].[CreateBy]
,[deleted].[CreateDate]
FROM [deleted] LEFT JOIN dbo.Node
ON [deleted].NodeID = dbo.Node.NodeID
WHERE deleted.ModifyDate <> dbo.Node.ModifyDate
END
GO
I am looking to backup changes, but never more than one backup version per day. If there is no change, there is no backup.
That's not a trigger anymore - that'll be a scheduled job. Triggers by their very definition execute whenever a given operation (INSERT, DELETE, UPDATE) happens.
Use the SQL Server Agent facility to schedule that T-SQL code to run once per day.
Read all about SQL Server Agent Jobs in the SQL Server Books Online on MSDN
Update: so if I understand correctly: you want to have an UPDATE trigger - but that trigger would only record the NodeID that were affected, into a "these nodes need to be backed up at night" sort of table. Then, at night, you would have a SQL Agent Job that runs and that scans that "work table" and for all NodeID values stored in there, it would then execute that T-SQL statement to copy their data into the NodeArchive table.
With this approach, if your nodes with NodeID = 42 changes ten times, you'll still only have a single entry NodeID = 42 in your work table, and the nightly backup job would then copy that node only once into the NodeArchive.
With this approach, you can decouple the actual copying (which might take time) from the update process. The UPDATE trigger only records which NodeID rows need processing - the actual processing then happens sometime later, at an off-peak hour, without disturbing users of your system.
I have a SQL Agent Job that has multiple steps which are scheduled to run Monday - Friday at a certain time. I need to be able to implement a stop feature on this job to not run on Holidays that are listed in a table.
I don't really know how to proceed with this. Do I need to create a first step that checks if it is a holiday and then fails the job if it is?
I have a stored procedure that will check the date that I pass to see if it is a holiday, I just don't know how to force it to report failure if the result is yes it is a holiday. Any help would be greatly appreciated.
Idea:
SQL Server Agent runs job
1st step is "check for holiday"
code throws error
job step silently fails
Point 3: To get the error from from the stored procedure to SQL Server Agent, you use RAISERROR
...
IF EXISTS (SELECT * FROM Holidays WHERE Date = GETDATE())
RAISERROR ('Do nothing: relax: chill out', 16, 1);
...
Points 4: In this case, use "Quit with success" (1) for the #on_fail_action parameter to sp_add_jobstep