Solved

Executing a Stored Procedure on certain days/dates

Posted on 2013-06-13
3
285 Views
Last Modified: 2013-06-14
Hi,

I need to trigger a stored procedure only on a Monday, or on the 1st of the month.
Unfortunately, I have no access to the scheduled jobs in the SQL Server Agent, so cannot set up my own schedule. There is a scheduled job that runs daily, and all it does is trigger all daily stored procedures, so I can only execute my stored procedures from here. I think all I really need to do run a quick check at the start of the sp, and see if the current date is either a Monday, or a 1st of the month. I guess, something like this (and forgive me, I have very limited exposure to SQL) -- Below I am trying to trigger spTEST, only if the current day is a "Thursday"

SELECT
CASE WHEN DATENAME(DW,CONVERT(DATE,REPLACE(GETDATE(),'-',''),103)) = 'Thursday'
THEN EXEC ([mi].[spTEST])
0
Comment
Question by:BananaFury
3 Comments
 
LVL 2

Accepted Solution

by:
vivekkumarSharma earned 150 total points
ID: 39244969
two solutions

select case when datepart(dw,GETDATE())= 2 or datepart(dd,GETDATE()) =1 then EXEC ([mi].[spTEST] else print 'No Execution of any SP' end


OR

if datepart(dw,GETDATE())= 2 or datepart(dd,GETDATE()) =1
begin
EXEC ([mi].[spTEST]
end
else
begin
print 'No Execution of any SP'
end
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39245227
The method below will not be affected by DATEFIRST or language settings:


IF DATEDIFF(DAY, 0, GETDATE()) % 7 = 0 --Monday (0=Mon,1=Tue,...,6=Sun).
OR DATEPART(DAY, GETDATE()) = 1 --first day of any month
    EXEC [mi].[spTEST]
0
 

Author Closing Comment

by:BananaFury
ID: 39247619
Great, thanks for your help!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now