Solved

Executing a Stored Procedure on certain days/dates

Posted on 2013-06-13
3
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Scott Pletcher
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

691 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