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 |
|
corey
Starting Member
13 Posts |
Posted - 2004-11-30 : 14:04:50
|
| I can't seem to get this to run correctly. The while statement causes it to hang. If I exclude the WHILE I am allowed to update the top record but ideally it would be nice to run through all the records meeting this criteria and update them all at the same time.Can someone help?BEGINdeclare @total as intdeclare @counter as intdeclare @pk_id as varchar(10)SET @counter = 0SET @total = (SELECT count(f.dup_id) FROM sysadm.fixdup f WHERE f.result = '10' and left(f.dup_id,1) = 'A')WHILE @counter < @totalSET @pk_id = (SELECT TOP 1 (f1.dup_id) FROM sysadm.fixdup f1 WHERE f1.result = '10' and left(f1.dup_id,1) = 'A' ORDER BY f1.update_stamp) /* Insert a delete statement here for another table OR insert an update statement to update the correct pk and settingsequence field = max of sequence + 1 */ UPDATE sysadm.fixdup SET result = NULL WHERE dup_id = @animal_id SET @counter = @counter + 1END |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-11-30 : 14:12:49
|
| You are only executing the update statement in the loop - as you don't update the counter then it just carries on forever updating the same rec.BEGINdeclare @total as intdeclare @counter as intdeclare @pk_id as varchar(10)SET @counter = 0SET @total = (SELECT count(f.dup_id)FROM sysadm.fixdup fWHERE f.result = '10' and left(f.dup_id,1) = 'A')WHILE @counter < @totalbegin SET @pk_id = (SELECT TOP 1 (f1.dup_id) FROM sysadm.fixdup f1 WHERE f1.result = '10' and left(f1.dup_id,1) = 'A' ORDER BY f1.update_stamp) /* Insert a delete statement here for another table OR insert an update statement to update the correct pk and setting sequence field = max of sequence + 1 */ UPDATE sysadm.fixdup SET result = NULL WHERE dup_id = @animal_id SET @counter = @counter + 1endEND==========================================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. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-11-30 : 14:15:31
|
| But couldn't you d this in a single update statement?And what's @animal_id?==========================================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. |
 |
|
|
corey
Starting Member
13 Posts |
Posted - 2004-11-30 : 14:16:46
|
| Thank you so much nr...I knew I was overlooking something. Works great! |
 |
|
|
corey
Starting Member
13 Posts |
Posted - 2004-11-30 : 14:19:47
|
| I guess I could but I am looking at updating or deleting as well. Maybe I am making this harder than it needs to be but I couldn't think of another route.@animal_id was a variable I was using and forgot to remove, it is basically @pk_id. Sorry to confuse you. |
 |
|
|
|
|
|