Author |
Topic |
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 17:15:12
|
Is there someway I can do a for each or loop in my sp?For example, I am running a select and for each one found, I want to run an update sql:Declare @dstID intSelect @dstID =DSTID from EmpCIPWHERE EmpCIPID = 26Update....*for each @dstID run the update sql for that @dstID foundCan anyone show me how to do this? I cannot even find what keyword to look for in the books online |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 18:00:36
|
ok - I found the "While" statement, but when I use it, I get Incorrect syntax in my select statement next to the while:Declare @dstID int,@desc varchar (1000),@reward varchar (1000),@target varchar (1000),@out varchar (1000)While exists (Select @dstID =DSTID from EmpCIP WHERE EmpCIPID = 26)BEGINSelect @desc = Description ,@reward = Rewarded ,@target = Target ,@out = OutstandingFrom DSTGoals WHERE DSTID=@DSTIDUPDATE EmpCIPSET Description = @desc,Threshold = @reward,Target = @target,Outstanding = @outWHERE DSTID=@DSTIDEND |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-06 : 18:06:46
|
please post your update statement, perhaps theres a way to avoid the loop and do the update in a batch..************************Life is short. Enjoy it.************************ |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-06 : 18:12:35
|
Try something like this:UPDATE ESET Description = D.Description,Threshold = D.Rewarded,Target = d.target,Outstanding = d.OutstandingFROM EmpCIP EJOIN DSTGoals D ON E.DSTID = D.DSTIDWHERE E.EmpCIPID = 26the issue with your current code is, change this line :(Select @dstID =DSTID from EmpCIP WHERE EmpCIPID = 26)to(Select * from EmpCIP WHERE EmpCIPID = 26)EXISTS () will return TRUE or FALSE. So trying to assign the value to a variable within EXISTS function is incorrect. ************************Life is short. Enjoy it.************************ |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 18:13:38
|
My update statement is in there:BEGINSelect @desc = Description,@reward = Rewarded,@target = Target,@out = OutstandingFrom DSTGoals WHERE DSTID=@DSTIDUPDATE EmpCIPSET Description = @desc,Threshold = @reward,Target = @target,Outstanding = @outWHERE DSTID=@DSTIDEND |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 18:22:26
|
I tried your change "Select * from EmpCIP WHERE EmpCIPID = 26" with the new update statement - it is soooo slow. It is taking over 2 minutes to process tables that have less than 10 rows of data each.Is there another method I can use? |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-06 : 18:26:30
|
Not only would it be slow, I think it will go into infinite loop because the status of EmpCIPID = 26 is not being changed in the loop. So there is no exit point. Check out the UPDATE statement I provided if it helps.************************Life is short. Enjoy it.************************ |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 18:28:34
|
I am using your update statement - and you're right, it's just looping, never breaking. How do I get it to break? |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-04-06 : 18:50:25
|
I am now using the "Fetch" statement, and it seems to work along with your better update:@EmpID int as Declare dst_cursor CURSOR FORSelect * from EmpCIP WHERE EmpCIPID = @EmpIDOPEN dst_cursor-- Perform the first fetch.FETCH NEXT FROM dst_cursor-- Check @@FETCH_STATUS to see if there are any more rows to fetch.WHILE @@FETCH_STATUS = 0BEGINUPDATE EmpCIPSET Description = D.Description,Threshold = D.Rewarded,Target = d.target,Outstanding = d.OutstandingFROM EmpCIP EJOIN DSTGoals D ON E.DSTID = D.DSTIDWHERE E.EmpCIPID = 26 FETCH NEXT FROM dst_cursorENDCLOSE dst_cursorDEALLOCATE dst_cursorGO |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-04-06 : 19:18:34
|
You don't need a loop nor a select. You just need to use an update with a join:UPDATE eSET Description = d.Description,Threshold = d.Rewarded,Target = d.Target,Outstanding = d.OutstandingFROM EmpCIP eINNER JOIN DSTGoals dON e.DSTID = d.DSTIDWHERE e.EmpCIPID = 26Just one command!Note: you may need to make slight adjustments as I whipped this up fast.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|