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)
 Lopping through table for insertion

Author  Topic 

kiekar
Starting Member

7 Posts

Posted - 2008-07-31 : 19:23:45
Hello,

Lets say I have 3 tables A, B, and C. Table A is the parent and B and C are child tables. Table A has 10 records. Is it possible to loop through table A and insert the first five id values into table B and the next five id values into table C? If this is possible, how would I go about doing this.

Any help would be much appreciated...

Thanks

Karl

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-31 : 19:27:07
No need to loop.

INSERT INTO Table2 (...)
SELECT TOP 5 ...
FROM Table1
ORDER BY SomeColumn ASC

INSERT INTO Table2 (...)
SELECT TOP 5 ...
FROM Table1
ORDER BY SomeColumn DESC

But why do you want to do this?


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kiekar
Starting Member

7 Posts

Posted - 2008-07-31 : 20:05:35
Hello Tara,

Thank you for your reply. I'm just throwing some ideas in my head based on a new DB schema I want to create. This DB is based on a two dimensional table in a word document which is a checklist.

Karl
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-08-01 : 00:12:55
There are a few ways to achieve this, but I think you may be using the wrong approach to your design. Please describe why you want to do this and someone may be able to provide a better solution.
Go to Top of Page

kiekar
Starting Member

7 Posts

Posted - 2008-08-01 : 16:01:58
Hello,

I've created a link which displays an image of the partial word document I want to create the db schema for. If there is anyone who can let me know if I'm going in the right direction would be much appreciated..

Karl

[url]http://www.enter-net.com/~kkieslin/Checklist.jpg[/url]
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-08-01 : 17:08:52
If I'm understanding this correctly I would create the following tables (forgoing index, keys and such):

Document (Name, whatever other high level attributes define the document)
DocumentItem (DocumentItemCategoryCode, Component, Checked, QCCategory, ISARP, QCObservation, IPM_AH, Action, VerifiedBy)
DocumentItemCategory (Code, Description)
Go to Top of Page

kiekar
Starting Member

7 Posts

Posted - 2008-08-07 : 16:20:53
Hello,

I created a db schema per the previous reply. You will find a link below

[url]http://www.enter-net.com/~kkieslin/RegChklist.jpg[/url].

How can I insert the id's (ChecklistItemCategoryID) form the ChecklistItemCategorys table into the IARRegistrationChecklists table?

Presently I have 3 different categories. When a new checklist is created, the fields are entered by one user. Once the user enters the data in the IARRegistrationChecklist table, the fields in the remaining tables are entered with null values. These fields would be entered with data later on by different users . I hope my explanation was clear enough.

Any help would be much appreciated.

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-08 : 01:56:10
quote:
Originally posted by kiekar

Hello,

I created a db schema per the previous reply. You will find a link below

[url]http://www.enter-net.com/~kkieslin/RegChklist.jpg[/url].

How can I insert the id's (ChecklistItemCategoryID) form the ChecklistItemCategorys table into the IARRegistrationChecklists table?

Presently I have 3 different categories. When a new checklist is created, the fields are entered by one user. Once the user enters the data in the IARRegistrationChecklist table, the fields in the remaining tables are entered with null values. These fields would be entered with data later on by different users . I hope my explanation was clear enough.

Any help would be much appreciated.

Thanks


have a look at OUTPUT clause in books online.
Go to Top of Page
   

- Advertisement -