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.
| Author |
Topic |
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2011-06-10 : 07:31:53
|
| I have the following Select QuerySELECT ID, orgID, certifiedStandardsID, publishStatusIDFROM certifiedStandardsToOrganisationWHERE (certifiedStandardsID = 4) AND (publishStatusID = 1)And for every record returned I want to perform 2 inserts into a different tableWhich Has the following layoutINSERT 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, publishStatusIDFROM certifiedStandardsToOrganisationWHERE certifiedStandardsID = 4AND publishStatusID = 1)across 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. |
 |
|
|
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) bGetting Msg 102, Level 15, State 1, Line 2Incorrect syntax near '('. |
 |
|
|
velnias2010
Posting Yak Master
125 Posts |
Posted - 2011-06-10 : 07:55:43
|
| Sorry got that working, worked great thanks a lot. |
 |
|
|
|
|
|
|
|