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.
Author |
Topic |
murrayb3024
Yak Posting Veteran
79 Posts |
Posted - 2007-10-30 : 11:20:41
|
I am not sure how to do this. I need to run 3 sql statements against a table with a variable created in one of them.Here is the first statementSelect ID from table1 where value = 1 Need to store that value in a variableUpdate table1 Set value = 0 Where ID = variableUpdate table1Set value = 1 Where ID = variable + 1ID is a incremental identity field, so it is numeric. Basically I need to change the value of one record to 0, and make the next records value = one.Any help is appreciated. |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-10-30 : 12:28:44
|
Why not do this in a single batch (in fact a stored proc would be best)?create proc aasdeclare @id intselect @id = ID from table1 where value = 1 update table1 set value = 0 where ID = @idupdate table1 set value = 1 where ID = @id+1goor you could do it in a single statementupdate t1set t1.ID = case when t1.ID = t2.ID+1 then 1 else 0 endfrom Table1 t1join (select ID from Table1 where value = 1) t2on t1.ID in (t2.ID, t2.ID + 1)==========================================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. |
 |
|
|
|
|