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 2000 Forums
 SQL Server Development (2000)
 Stored Procedure: Looping through records

Author  Topic 

nickjones
Starting Member

16 Posts

Posted - 2003-06-25 : 06:17:43
This seems like a rather obvious question, yet I've searched here, on the net, and in MSDN library and I've yet to come up with the answer.

All I was to do is do a SELECT statement and then loop through each record of the result, all in the stored procedure. In the loop I'll be running an INSERT based on the record's fields.

So it would hopefully look something like

SET @SelectResult = (SELECT thing, thing2 FROM SomeTable)
FOR EACH @Record in @SelectResult
INSERT INTO AnotherTable VALUES (selectresult.thing, selectresult.thing2)
NEXT

Thanks a lot in advance, and I appologise if this is a common request :)

mr_mist
Grunnio

1870 Posts

Posted - 2003-06-25 : 06:25:47
Forget about loops...

INSERT into anothertable
SELECT (thing, thing2) FROM sometable

-------
Moo.
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-06-25 : 06:29:09
If you are doing a straight insert then you don't need to store the result in a variables.


INSERT INTO SomeTable(thing1, thing2)
SELECT thing1, thing2
FROM SomeTable



However if you are going to manipulate the data in a certain way and you need to look at each record individually then consider the following approach :

I know people will bite my head off for suggesting this but have you tried using a cursor?

It seems that most SQL developers here avoid using cursors but your code suggests that it is probably the best option.


SET @SelectResult = (SELECT thing, thing2 FROM SomeTable)



You are trying to store thing and thing2 in one variable?!

something like the following will help you,


DECLARE TBL CURSOR FOR
SELECT thing, thing1 from SomeTable

OPEN TBL
FETCH TBL INTO @thing1, @thing2

WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO AnotherTable VALUES (@Tthing1,@thing2)
FETCH TBL INTO @thing, @thing2
END
CLOSE TBL
DEALLOCATE TBL





----------------
Have a good day!

Edited by - Amethystium on 06/25/2003 06:31:52
Go to Top of Page

nickjones
Starting Member

16 Posts

Posted - 2003-06-25 : 06:31:48
Excellent, thanks mr_mist, ha I feel rather stupid now. That works perfectly.

Out of curiosity, how would this be done as a loop? In situations wher e it wasn't a simple insert you wanted to perform, looping would be the only option.

Go to Top of Page

nickjones
Starting Member

16 Posts

Posted - 2003-06-25 : 06:33:38
And thanks Amethystium, answering my second post before I even sent it.

Wow, great responses in less than 15 minutes. I'd forgotten how helpful you guys were, thanks a lot :)

Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-06-25 : 06:34:13
quote:

If you are doing a straight insert then you don't need to store the result in a variables.


INSERT INTO SomeTable(thing1, thing2)
SELECT thing1, thing2
FROM SomeTable



However if you are going to manipulate the data in a certain way and you need to look at each record individually then consider the following approach :

I know people will bite my head off for suggesting this but have you tried using a cursor?

It seems that most SQL developers here avoid using cursors but your code suggests that it is probably the best option.


SET @SelectResult = (SELECT thing, thing2 FROM SomeTable)



You are trying to store thing and thing2 in one variable?!

something like the following will help you,


DECLARE TBL CURSOR FOR
SELECT thing, thing1 from SomeTable

OPEN TBL
FETCH TBL INTO @thing1, @thing2

WHILE @@FETCH_STATUS=0
BEGIN

-- manipulate the data as you want...

INSERT INTO AnotherTable VALUES (@Tthing1,@thing2)
FETCH TBL INTO @thing, @thing2
END
CLOSE TBL
DEALLOCATE TBL





----------------
Have a good day!

Edited by - Amethystium on 06/25/2003 06:31:52



----------------
Have a good day!
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2003-06-25 : 06:41:34
quote:

Excellent, thanks mr_mist, ha I feel rather stupid now. That works perfectly.

Out of curiosity, how would this be done as a loop? In situations wher e it wasn't a simple insert you wanted to perform, looping would be the only option.





I'd be tempted to post on here again and ask for another solution that avoided the looping :)

You could use a temporary table to hold the data whilst you manipulate it, for example, then insert from that.

-------
Moo.
Go to Top of Page
   

- Advertisement -