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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help for implementing loop in stored procedur

Author  Topic 

nkmekal
Starting Member

4 Posts

Posted - 2009-04-13 : 13:37:08
Hi All,
I am trying to write a store procedure that would implement loop in it. I have not written any stored procedures so far and its a difficult task for me. Although i have the algorithm of how the procedure should work which is as follows:

query 1: select emp_Id from Employees
query 2: select emp_address from Address where emp_Id=@emp_Id

while(i<count(no of rows returned by query 1)
Begin
execute query 2 (with relevent emp_Id as parameter)
(finally here i am trying to insert this address into another table)
i++
End

I know how to write a basic while loop but my problem is how can i assign the emp_id as parameter one at a time. As there is no way for me to specify the position nor i am using an array or list.

Even though the table structure isn't the same but i tried to emulate the scenario here.

CAN ANYONE HELP ME OUT IN THIS ASPECT.

MNK...

MNK

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-04-13 : 13:53:38
Whats query2 there ? I'm thinking that you don't need a loop.
Go to Top of Page

nkmekal
Starting Member

4 Posts

Posted - 2009-04-13 : 14:03:55
I know that there is no need to run the loop for this scenario. As I said the tables structures are not so simple as i mentioned above but in order to fullfill the purpose i have to do something like this.

As far as the query 2 is concerned i just want to know that when i run a loop how do i assign a emp_id as parameter based on the position or value of variable "i".




MNK
Go to Top of Page
   

- Advertisement -