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
 General SQL Server Forums
 New to SQL Server Programming
 Insert statement using results of select query

Author  Topic 

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2015-02-11 : 09:20:08
I need to insert data into a table based on the results returned by a select statement. Basically, the select statement below gives me a list of all the work orders created in the last hour.

select worknumber from worksorderhdr where date_created  > DATEADD(HOUR, -1, GETDATE())


This might return anywhere between 5 and 50 records each time. What I then need to do is use each of the work numbers returned to create a record in the spec_checklist_remind table. The other details in the insert statement will be the same for each insert, it's just the worknumber from the select statement that needs to be added to the insert where the ?? are below:

INSERT INTO spec_checklist_remind (form, record_type, linked_to_worknumber, spec_checklist_id) values (5, 0, '??',52)"


What is the best way to achieve this?

Many thanks
Martyn

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2015-02-11 : 10:18:01
[code]INSERT INTO spec_checklist_remind (form, record_type, linked_to_worknumber, spec_checklist_id)
SELECT 5, 0, worknumber, 52 from worksorderhdr where date_created > DATEADD(HOUR, -1, GETDATE());[/code]
Go to Top of Page

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2015-02-11 : 11:16:01
Excellent, that works perfectly.


Many thanks
Martyn
Go to Top of Page
   

- Advertisement -