I have a Store procedure with a Dynamic SQL that I want to pass a variable into it with a Union statment. Is that Possible? Below is my Store procedure syntax. Let me know if there is a way to make this work. Thanks.
Create Proc DYSQL @searchDate Date, @Mode Char(1)
As
Declare @SQL AS LongVarchar(1000),
Declare @Type Varchar(100)
IF @mode ='R'
Then @Type =' ReturnDate '
END IF
IF @mode ='S'
Then @Type =' SalesDate '
END IF
Set @SQL =
'Insert into #Profit( Items, Amt)
Select
Items,
Amt
From Sales
Where State ='SC'
AND @Type = @searchDate
UNION ALL
Select
Items,
Amt
From Sales
Where State ='TX'
AND @Type = @searchDate '
Create Proc DYSQL @searchDate Date, @Mode Char(1)
As
Declare @SQL AS LongVarchar(1000),
Declare @Type Varchar(100)
IF @mode ='R'
Then @Type =' ReturnDate '
END IF
IF @mode ='S'
Then @Type =' SalesDate '
END IF
Set @SQL =
'Insert into #Profit( Items, Amt)
Select
Items,
Amt
From Sales
Where State ='SC'
AND @Type = @searchDate
UNION ALL
Select
Items,
Amt
From Sales
Where State ='TX'
AND @Type = @searchDate '