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
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/
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.
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)
I have a scheduled job in Sql Server 2012. When the job runs every 20 mins, it executes a stored procedure and fills a new table. It takes a lot of time to update the table every time and I don't want an empty table when the job is running.
This is something I'm trying,
IF EXISTS(SELECT 1
FROM msdb.dbo.sysjobs J
JOIN msdb.dbo.sysjobactivity A
ON A.job_id=J.job_id
WHERE J.name=N'MyJobName'
AND A.run_requested_date IS NOT NULL
AND A.stop_execution_date IS NULL
)
PRINT 'The job is running!'
ELSE
PRINT 'The job is not running.'
So, basically when the job is running, instead of printing, I want to return data from a new table or something. The idea is, I do not want an empty data set even when the job is running.
Any ideas will be helpful.
Thanks.
We want to log the job history into a table named JobHistory. And the solution of us maybe as follows.
Set three steps in the job.
Step one call the sp which will write the start time, job name, job id and other job information into JobHistory.
Step two call the sp which do the actual works.
Step three call the sp which get the executing information of step two, such as duration, error message, executing output, and update the end time of JobHistory.
Is this solution works? If it works, how can we get the executing information of step two in step three?
Thanks.
Your approach looks good to me.
In SQL-Server 2008+ you can get information about Duration, LastExecutionTime, Logical reads and wrights using system view sys.dm_exec_procedure_stats
select eps.object_id, eps.last_execution_time, eps.execution_count, eps.last_worker_time/1000 as CPU,
eps.last_logical_writes as [Writes],
eps.last_logical_reads+eps.last_logical_writes as [AggIO]
from sys.dm_exec_procedure_stats eps
where eps.object_id=object_id(N'Your_procedure_name')
As for error, in SQL-Server 2005+ you can get it using function ERROR_MESSAGE
BEGIN TRY
exec YOUR_PROCEDURE_NAME
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH