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 exsists

Author  Topic 

craigmacca
Posting Yak Master

142 Posts

Posted - 2008-01-25 : 04:14:24
Hi i need a sql stored procedure

i am currently using the select statement below, but i need to use a if exsits i think.

i want

select Alo_Qty
from table
where Alo_UserID = @UserID and Alo_Stock_Code = @StockCode

but if there is no record in the table that matches then i need to set Alo_Qty = 0

any ideas what i need to do?

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-01-25 : 04:30:41
Declare @Table table
(id int,
val int)
INSERT @Table
Select 1,3 union all
Select 2,5 union all
Select 3,6 union all
Select 4,8

Declare @Val1 int
Declare @Val2 int

IF EXISTS(select @Val1=val from @Table where id=2 )
BEGIN
Print' Your logic '
END
ELSE
BEGIN
Set Alo_Qty = 0
END

You can take help from above code snippet.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-25 : 08:50:31
SELECT ISNULL(t.Alo_Qty,0)
FROM
(
select Alo_Qty
from table
where Alo_UserID = @UserID and Alo_Stock_Code = @StockCode
)t
Go to Top of Page
   

- Advertisement -