G_Man
G_Man

Reputation: 1343

Using IDs from table and insert into secondary table

Currently I am trying to write a sql statement that selects from one table then uses the primary key to insert into a secondary table. I am having a hard time figuring out how I would do it. This is the select and insert I have right now. The first select will return multiple results. I need to run this nightly so I need to make it dynamic.

SELECT ParentTypeId FROM Config_OrderParentQueueType
INSERT INTO [dbo].[Config_OrderParentQueueTypeNotes]
       ([ParentTypeId]
       ,[NoteDate]
       ,[NoteText]
       ,[NoteSubmittedById])
 VALUES
       (This is the ID I need to insert from the select
       ,GETDATE()
       ,'Default Note'
       ,6)

I have tried to mess with rowcount but the IDs are not always sequential. Appreciate any help in advance on how I would do this.

Upvotes: 0

Views: 460

Answers (1)

GMB
GMB

Reputation: 222402

Use insert .. select:

INSERT INTO [dbo].[Config_OrderParentQueueTypeNotes]
       ([ParentTypeId]
       ,[NoteDate]
       ,[NoteText]
       ,[NoteSubmittedById])
SELECT ParentTypeId, getdate(), 'Default Note', 6
FROM Config_OrderParentQueueType

Upvotes: 6

Related Questions