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
 General SQL Server Forums
 New to SQL Server Programming
 Stored procedures IF statement (SQL2005)

Author  Topic 

tmack
Starting Member

6 Posts

Posted - 2006-01-30 : 22:49:23
Hello, I need to use a if statement in stored procedures.

What I need to know is the proper syntax because anything I try does not work.

I need to check if 24 hours passed:
datediff(hh, Users.LastLogin, GetDate())

Should return the number of hours that has passed. I need to use this in the IF statement, and I need to check if it's > 24.

Thanks =)!

nr
SQLTeam MVY

12543 Posts

Posted - 2006-01-30 : 23:00:21
You can't use an if statement in a query.
You can
if (select Users.LastLogin - GetDate() from users where userid = @userid) > 24
begin
......
end
....


or
select * from users where Users.LastLogin - GetDate() > 24
or
select
case when Users.LastLogin - GetDate() > 24 then 'a' else 'b' end
from users

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-30 : 23:54:08
wont this work in SQL Server 2005?
select * from users where datediff(hh, Users.LastLogin, GetDate())>24


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -