SQL comparison of dates does not work

67 views Asked by At

This code does not work:

conn.Execute("CREATE PROCEDURE mySuperProcedure 
AS
BEGIN 
    IF GETDATE() >= DATEADD(hour," & waitHours & ",
        DATEADD(minutes," & waitMinutes & ",'" & now & "') 
    BEGIN
        blah blah...
    END
END")

A SQL Agent Job runs this procedure every few seconds. It shall not do the "blah blah..." part until a certain time, but it does it every time because the IF statement returns true.

What could possibly be wrong? It makes no sense at all... :'(

1

There are 1 answers

3
user1859022 On

not clear what you try to achieve but a quick look shows 2 syntax errors: ist MINUTE not minutes and you are missing a closing parenthesis in your dateadd

some questions that come to mind:

  • do you try to create the stored procedure every time the job executes?
  • why not set the job to execute a the time you want it to run instead of checking every second if that time has come by running the job? :)
  • why the dynamic SQL?