Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Azure Data warehouse Dynamic SQL Select query

In Azure SQL Data Warehouse stored procedure I try to form Dynamic SQL and perform select query,I unable to fetch the resutSet Below is code Snippet:

DECLARE @sql nvarchar(400)
DECLARE @cnt int
BEGIN
---Some Business Logic---
SELECT @sql = N'select @cnt = count(*) from  '+quotename(@src_TableName)+' where warn_remarks  like ''%'+ @condition +'%''';
SET @parameter = N'@cnt int OUTPUT'
EXECUTE sp_executesql @sql,@parameter,@cnt = @cnt OUTPUT        
END

The Error it is showing is "Incorrect syntax near '='",In the select query when assigning count(*) to a variable it is giving error.But the same logic is working fine in Azure SQL Database.Kindly help to solve this.

like image 271
Prakash Avatar asked Jan 06 '23 03:01

Prakash


1 Answers

You'll need to use SET instead, because you can't set variables using SELECT in SQL DW or PDW.

DECLARE @sql nvarchar(400)
DECLARE @cnt int
BEGIN
---Some Business Logic---
SET @sql = N'SET @cnt = (select count(*) from  '+quotename(@src_TableName)+' where warn_remarks  like ''%'+ @condition +'%'')'; --replaced
SET @parameter = N'@cnt int OUTPUT'
EXECUTE sp_executesql @sql,@parameter,@cnt = @cnt OUTPUT        
END
like image 188
Rob Farley Avatar answered Jan 18 '23 17:01

Rob Farley