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
 Transact-SQL (2000)
 Looping thru a table and inserting....

Author  Topic 

interclubs
Yak Posting Veteran

63 Posts

Posted - 2003-06-03 : 22:16:54
I know this is easy, but I am an idiot and can't figure it out. I have a table:
StoryID|Headline|Story
(StoryID) is an identity field

and I want to write a stored proc that will loop thru the table and insert the data into two tables like:
Table1: StoryID|Headline
Table2: StoryID|Story
(where StoryID is the same StoryID from the original table)


Thanks guys, I appreciate it.




ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-06-03 : 22:30:34
INSERT INTO Table1 (StoryID, Headline)
SELECT StoryID, Headline
FROM OriginalTable

INSERT INTO Table2 (StoryID, Story)
SELECT StoryID, Story
FROM OriginalTable



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-06-03 : 22:34:43
No looping needed here. Just need to be able to insert the identity column into the other tables so you need to turn that feature on, well that is if StoryID is an identity column in the other two tables also.

Let's call your original table Table1, and the other two tables Table2 (contains Headline column) and Table3 (contains Story column).

SET IDENTITY_INSERT ON Table2

INSERT INTO Table2 (StoryID, Headline)
SELECT StoryID, Headline
FROM Table1

SET IDENTITY_INSERT OFF Table2

SET IDENTITY_INSERT ON Table3

INSERT INTO Table3 (StoryID, Story)
SELECT StoryID, Story
FROM Table1

SET IDENTITY_INSERT OFF Table3


Tara

Edited by - tduggan on 06/03/2003 22:51:00
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-04 : 12:08:50
Building a normalized database "on th fly"...interesting...

How often does this need to be done? Everytime a row is inserted?

Guess it'd be kinda hard to have RI..guess you could use a non identifying relationship...



Brett

8-)
Go to Top of Page
   

- Advertisement -