In a select statement, in from clause, table name changes depending on a parameter. I tried:
from
IIf(@prmdataVal ='Dummy', tbl1, tbl2) T1
It's showing an error. How to do this?
In a select statement, in from clause, table name changes depending on a parameter. I tried:
from
IIf(@prmdataVal ='Dummy', tbl1, tbl2) T1
It's showing an error. How to do this?
Try something like the following:
DECLARE @stmt nvarchar(max);
SELECT @stmt = 'SELECT * FROM ' + CASE WHEN @prmdataVal = 'Dummy' THEN 'tbl1' ELSE 'tbl2' END
EXEC @stmt
Declaring a variable for table name and then calling it in the from clause of dynamic SQL, worked.
Declare @TblName AS SYSNAME = CASE WHEN @prmdataVal='Dummy' THEN 'tbl1'
ELSE 'tbl2' end
and then calling it in the dynamic sql:
from '+@TblName +' as T1...