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
 Performing Insert after Select Query

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-06-10 : 07:31:53
I have the following Select Query

SELECT ID, orgID, certifiedStandardsID, publishStatusID
FROM certifiedStandardsToOrganisation
WHERE (certifiedStandardsID = 4) AND (publishStatusID = 1)

And for every record returned I want to perform 2 inserts into a different table

Which Has the following layout

INSERT INTO [voluntaryCodesToOrganisation]
([orgID]
,[voluntaryCodesID]
,[explanatorytext]
,[publishStatusID])
VALUES
(orgId, int,>
,<voluntaryCodesID, int,>
,<explanatorytext, varchar,>
,<publishStatusID, int,>)

WHere on the insert I can use the orgId, publishStatusId value from the select, explanatoryText can be left blank and for the first insert the volunatary ID is 4 and for the second = 11

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-10 : 07:38:54
insert [voluntaryCodesToOrganisation]
(
([orgID]
,[voluntaryCodesID]
,[explanatorytext]
,[publishStatusID])
)
select
a.[orgID] ,
case when b.i = 1 then 4 else 11 end ,
'' ,
a.[publishStatusID]
from
(
SELECT ID, orgID, certifiedStandardsID, publishStatusID
FROM certifiedStandardsToOrganisation
WHERE certifiedStandardsID = 4
AND publishStatusID = 1
)a
cross join
(select i=1 union all select 2) b


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-06-10 : 07:44:01
INSERT [voluntaryCodesToOrganisation]
(([orgID], [voluntaryCodesID], [explanatorytext], [publishStatusID]))
SELECT a.[orgID], CASE WHEN b.i = 1 THEN 4 ELSE 11 END, '', a.[publishStatusID]
FROM (SELECT ID, orgID, certifiedStandardsID, publishStatusID
FROM certifiedStandardsToOrganisation
WHERE certifiedStandardsID = 4 AND publishStatusID = 1) a CROSS JOIN
(SELECT i = 1
UNION ALL
SELECT 2) b

Getting Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-06-10 : 07:55:43
Sorry got that working, worked great thanks a lot.
Go to Top of Page
   

- Advertisement -