Groups | Blog | Home
all groups > sql server programming > february 2007 >

sql server programming : Simple question.


Rudy
2/24/2007 9:40:00 PM
Hello All!

Seem to be having a little problem with a syntax error on this.

UPDATE Bankroll
IF UserID IN (SELECT USERID FROM tblUserID WHERE Pass = '1')
SET NewCredit = Credits + Cast(@win as int)
ELSE
SET NewCredit = '0'

SQL tells me incorrect syntax near IF and near "="

Any thoughts?

Thanks!

EMartinez
2/24/2007 10:20:54 PM
[quoted text, click to view]


I don't believe you can use conditional statements as part of the
update statement. You might want to use something like this:

UPDATE Bankroll
SET NewCredit = Credits + Cast(@win as int)
where UserID IN (SELECT USERID FROM tblUserID WHERE Pass = '1')

UPDATE Bankroll
SET NewCredit = '0'
where UserID NOT IN (SELECT USERID FROM tblUserID WHERE Pass =
'1')

Regards,

Enrique Martinez
Sr. SQL Server Developer
Uri Dimant
2/25/2007 12:00:00 AM
Rudy
I simplified your structure a littel bit , but I'm sure you will get an
idea

create table #t (NewCredit int,Credits int ,USERID int)
insert into #t values (10,20,1)
insert into #t values(1,30,1)
insert into #t values(88,58,20)



declare @win int
set @win=1000
update #t set NewCredit =case when userid in(select userid from #t where
userid=1) then Credits+@win else 0 end


select * from #t

drop table #t



[quoted text, click to view]

Rudy
2/25/2007 9:26:08 AM
Thanks EMartinez! Works perfect! Thanks for your suggestion too Uri, but
I'm not able to change the SP that much.

Rudy

[quoted text, click to view]
EMartinez
2/25/2007 4:49:45 PM
[quoted text, click to view]

You're welcome. Glad it worked out.

Regards,

Enrique Martinez
Sr. SQL Server Developer
AddThis Social Bookmark Button