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)
 Help needed for inserting data into junction Table

Author  Topic 

kiekar
Starting Member

7 Posts

Posted - 2008-08-13 : 20:20:22
Hello

I’m using a junction table, ChecklistItems Table with two columns, ChecklistID and ItemID from two different tables, Checklists Table and Items Table.
The Items Table has 11 records and the Checklists Table has 1 record.

When using a stored procedure with
INSERT INTO ChecklistItems (ItemID)
SELECT ItemID FROM Items


All 11 records get inserted into the ItemID column of the ChecklistItems table.
How would I go about inserting the 1 record (ChecklistID) into all 11 rows of the ChecklistID in the ChecklistItem Table? In a nut shell what I’m trying to accomplish is the same as the Orders Details table from the Northwind database.

Your help would be much appreciated.

Karl

dexter.knudson
Constraint Violating Yak Guru

260 Posts

Posted - 2008-08-13 : 21:51:33
Ideally, you should pass both IDs as paramaters to your stored procedure- look up SP syntax. This is based on the precondition that those values exist in their respective tables. Then you just code something like the following in your SP. Remember to do error checking (See @@ERROR).
INSERT INTO ChecklistItems (ItemID, ChecklistID)
VALUES (@ItemID, @ChecklistID)
Go to Top of Page
   

- Advertisement -