sql server insert into select + in select part if else
Database
2
Posts
2
Posters
0
Views
1
Watching
-
How to execute the below piece of code correctly. Please help me.
INSERT INTO #TempTable
(PatchAppId, PatchName,HostName)if (@Id = 0) BEGIN SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData1 where Criticality=@Criticality end else begin SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData2 where ServerTypeName=@ServerTypeName end
-
How to execute the below piece of code correctly. Please help me.
INSERT INTO #TempTable
(PatchAppId, PatchName,HostName)if (@Id = 0) BEGIN SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData1 where Criticality=@Criticality end else begin SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData2 where ServerTypeName=@ServerTypeName end
Erk! You have an insert statement Then an if statement on a variable that is not in scope (of the question) Try moving the insert inside the if statement, so you will need the insert twice if begin insert select end else begin insert select end
Never underestimate the power of human stupidity RAH