jjss jjss - 2 years ago 82
SQL Question

Nested if statement in SQL stored procedure error. Expecting ), AND, or OR

I'm trying to write a stored procedure that will check if a datetime value in a column in a table (it will have only one value in the column). If it exists, I want to see if the datetime value is withing +-5 minutes of current time. If it is then I will execute another stored procedure and then update that datetime.

If the value didn't exist at all then I insert current date to the column.

Answer Source
    IF ( @DateToKeepForTrim >=  dateadd(MINUTE,-5,(SELECT top 1 TrimCheck.TrimCheckDate FROM TrimCheck)) AND @DateToKeepForTrim <= dateadd(MINUTE,5, @DateToKeepForTrim)

Should read:

    IF ( @DateToKeepForTrim >=  dateadd(MINUTE,-5,(SELECT top 1 TrimCheck.TrimCheckDate FROM TrimCheck)) AND @DateToKeepForTrim <= dateadd(MINUTE,5, @DateToKeepForTrim) )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download