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
 update in case statement

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-08-22 : 15:07:43
Hi all,

I want to update different fields when @status=1 and others when @status = 0. I have used following SQL but getting error. Can some one correct the syntax of this query

ALTER PROCEDURE [dbo].[ConvertToRetailer] (
@WholesalerID int,
@stawtus bit
)

AS

SET NOCOUNT ON

case when @status = 1 then
UPDATE
WHOLESALERS
SET
WholeSaleEndDate = getdate(),
statusChangedOn = getdate(),
[ModifiedOn] = getdate()
WHERE
WHOLESALERID = @WholesalerID

DECLARE @userid int

select @userid = u.userid from users u inner join wholesalers w
on u.userid = w.userid
where w.wholesalerid = @WholesalerID

update users set iswholesaler = 0,
[ModifiedOn] = getdate()
where userid = @userid

else

UPDATE
WHOLESALERS
SET
WholeSaleStartDate = getdate(),
statusChangedOn = getdate(),
[ModifiedOn] = getdate()
WHERE
WHOLESALERID = @WholesalerID
end

Regards,
Asif Hameed

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-22 : 15:35:12
I would make 2 seperate SPs, but, here ya go:

ALTER PROCEDURE [dbo].[ConvertToRetailer] (
@WholesalerID int,
@stawtus bit
)

AS

SET NOCOUNT ON

IF @status = 1 then
BEGIN
UPDATE WHOLESALERS
SET WholeSaleEndDate = getdate(),
statusChangedOn = getdate(),
[ModifiedOn] = getdate()
WHERE WHOLESALERID = @WholesalerID

DECLARE @userid int

select @userid = u.userid
from users u
inner join
wholesalers w
on u.userid = w.userid
where w.wholesalerid = @WholesalerID

update users set iswholesaler = 0,
[ModifiedOn] = getdate()
where userid = @userid

END
else
BEGIN
UPDATE WHOLESALERS
SET WholeSaleStartDate = getdate(),
statusChangedOn = getdate(),
[ModifiedOn] = getdate()
WHERE WHOLESALERID = @WholesalerID
end
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-22 : 15:37:58
It maybe a typo but in the head of your sp you're writing @stawtus


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

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-08-22 : 20:15:36
webfred, it was typing error :) thanks russell, ur code worked except if statement dont have 'then' word. I tried without then adn it worked. thanks
Go to Top of Page
   

- Advertisement -