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
 if statement in stored procedure

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-23 : 12:13:20
how would i write an if clause in a stored procedure

if (select count(*) from table) > 1

then

begin

return count

end

else

begin
return 0
end

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-23 : 12:18:57
Maybe this:

declare @cnt int
select @cnt= count(*) from Table
if @cnt>1
begin
select @cnt as result
end
else
begin
select 0 as result
end



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sanjnep
Posting Yak Master

191 Posts

Posted - 2010-04-23 : 12:27:23
dude you can do like this but you can't use return here coz you are not using function. balle balle

if exists(select a.cnt from (
select count(*) as cnt from sysobjects
) a where a.cnt > 1)
begin
select count(*) from sysobjects
end
else
print(0)
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-23 : 13:16:43
thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-26 : 04:09:41

why dont you use just this?

declare @cnt int
select @cnt= count(*) from Table
select @cnt as result


Madhivanan

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

- Advertisement -