Forum Discussion
How to use dynamic SQL in Fabric Warehouse
Hi kkoba ,
NVARCHAR data type is not supported in Fabric Warehouse and sp_executesql command requires NVARCHAR.
I have an alternative approach:
Try to define all variables and parameters as VARCHAR and do string concatenation directly in SQL script instead of using parameterized form.
Code:
DECLARE @area VARCHAR(10)
DECLARE @SQL VARCHAR(2000)
SET @area = 'Area1'
SET @SQL = '
SELECT [Area], [Category], SUM(CalcValue) AS sum_CalcValue, COUNT(*) AS [Count]
FROM [dbo].[TestFact1] WHERE [Area] = ''' + @area + '''
GROUP BY [Area], [Category]
ORDER BY [Area], [Category]'
EXECUTE(@SQL)
This worked for me.
If you have any other questions please feel free to contact me.
Best Regards,
Yang
Community Support Team
If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!
- AndyDDC1 year ago
Most Valuable Professional
I would add to not suggest EXECUTE/EXEC(@SQL) as there is a higher risk of SQL injection attacks. As sp_executesql is paramterised, this lowers the risk of malicious attacks.