I got a quite big query with CTEs, Temp Tables and I'd like to put it into IF and ELSE blocks,for having different conditions for both of them, depened on parameter. In simplified it looks as below:
DECLARE @parameter varchar(15)
If(OBJECT_ID('tempdb..#table1') Is Not Null)
Begin
Drop Table #table1
End
create table #table1
(
column1
column2
column3
)
If(OBJECT_ID('tempdb..#table2') Is Not Null)
Begin
Drop Table #table2
End
create table #table2
(
column1
column2
column3
)
IF @parameter = 'Option1' BEGIN
select * from (
INSERT INTO #table1
select * from... where 'condition_for_Option1'
INSERT INTO #table2
select * from... where 'condition_for_Option1'
select * from #table1
union all
select * from #table2) as DATA
END ELSE
IF @parameter = 'Option2' BEGIN
SELECT *
FROM
(
INSERT INTO #table1
select * from... where 'condition_for_Option2'
INSERT INTO #table2
select * from... where 'condition_for_Option2'
select * from #table1
union all
select * from #table2)
as DATA
END
I put the #Temp tables at the beginning and it fixed a part of issues, but I still got:
Msg 156, Level 15, State 1, Line 31
Incorrect syntax near the keyword 'INSERT'.
Msg 102, Level 15, State 1, Line 37
Incorrect syntax near ')'.
Msg 156, Level 15, State 1, Line 48
Incorrect syntax near the keyword 'INSERT'.
Msg 102, Level 15, State 1, Line 54
Incorrect syntax near ')'.
Is this any way how to make this work?
Aucun commentaire:
Enregistrer un commentaire