5

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.

Cydrick Trudel
  • 9,957
  • 8
  • 41
  • 63
Prakash
  • 281
  • 5
  • 18

2 Answers2

8

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
Rob Farley
  • 15,625
  • 5
  • 44
  • 58
  • 1
    You can vote for features on the SQL DW features forum here: https://feedback.azure.com/forums/307516-sql-data-warehouse – Matt Usher May 11 '16 at 06:15
1

Select @var is not supported in Azure SQL data warehouse. https://msdn.microsoft.com/en-us/library/ms187330.aspx

You can find workarounds https://azure.microsoft.com/en-us/documentation/articles/sql-data-warehouse-develop-variable-assignment/

ckarst
  • 751
  • 5
  • 11