Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with IF Statement

Author  Topic 

bboyce
Starting Member

2 Posts

Posted - 2009-12-07 : 15:20:15
I am trying to write a stored procedure in SQL but I am getting an error when creating it. The code:
CREATE PROCEDURE CheckForCalls 

IF (SELECT max(StartDateTime) FROM dbo.Calls WHERE Recorded = 'true' AND CallDirection = 1) < DATEADD(minute, - 30, GETDATE())
begin
exec AnotherSP
end
go

When I try to execute I get:
Msg 156, Level 15, State 1, Procedure CheckForCalls, Line 4
Incorrect syntax near the keyword 'IF'.


I'm not sure what I am doing wrong, the if statement works if I run it on it's own in a query. I can do:
IF (SELECT  max(StartDateTime) FROM dbo.Calls WHERE Recorded = 'true' AND CallDirection = 1) < DATEADD(minute, - 30, GETDATE())
print 'NO'
else
print 'YES'


This works fine. Hopefully someone can tell me what I am doing wrong. Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-07 : 15:23:13
You are missing "AS" in your stored procedure body.

CREATE PROC sprocName
AS
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

bboyce
Starting Member

2 Posts

Posted - 2009-12-07 : 15:25:22
HAHAHA...Thanks, that was a simple fix. I'm still learning.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-07 : 15:26:03
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -