Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to prepare a script which will run only on 1st day of the every month at 9PM IST and rest other days it will not run.

See script below. I am getting error in executing it.
Please suggest correct way to do .

What I have tried:

SQL
Declare @@ScriptRunDate  datetime
 set @@ScriptRunDate=  DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) 
 select @@ScriptRunDate
Case when 
@@ScriptRunDate
 then 
  update Cssscript set ScriptState='Active' where id='Adhoc'
else 
update Cssscript set ScriptState='InActive' where id='Adhoc'
Posted
Updated 1-Aug-18 6:00am
v2
Comments
Patrice T 1-Aug-18 8:05am    
'I am getting error in executing it.'
Which error message ?

You are getting two errors:
Incorrect syntax near the keyword 'Case'.
and
Incorrect syntax near the keyword 'else'.

This should give you a clue that you are using the wrong construct. You actually want to use an IF-Statement - see IF...ELSE (Transact-SQL) | Microsoft Docs[^]. Something like
SQL
Declare @@ScriptRunDate  datetime
 set @@ScriptRunDate=  DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) 
 select @@ScriptRunDate
IF @@ScriptRunDate 
	update Cssscript set ScriptState='Active' where id='Adhoc'
ELSE 
	update Cssscript set ScriptState='InActive' where id='Adhoc'
However the IF part is still wrong ... you probably want something like
SQL
IF @@ScriptRunDate = GETDATE() -- or whatever 
 
Share this answer
 
It is not polite to ask people to guess which of the 23,235,331,213 possible errors you are getting. However, I will take a stab. It is at the Case WHEN.

If looks like you actually want to use an IF statement.

SQL
IF someCondition
  BEGIN
    UPDATE ...
  END
ELSE 
  BEGIN
    UPDATE  ...
  END
 
Share this answer
 
The easiest way to schedule the script you actually want to run and save it as a Stored Procedure, and then create an SQL Agent Job to execute that script on the first day of the month at 9AM.

Create a Job[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900