2

I recently had a first contact with the Azure Synapse Analytics environment, and when implementing some SQL queries, I came across a problem when trying to declare a variable to run a query with WHILE.

I am sending below the error messages I am getting. I've read the T-SQL documentation and I'm not understanding what I'm doing wrong. If anyone has gone through the same situation and could shed some light on it, I would be very grateful!

%%sql

DECLARE @Counter INT;
SET @Counter = 0;
WHILE ( @Counter <= 3)
BEGIN
    PRINT 'The counter value is = ' + CONVERT(VARCHAR,@Counter)
    SET @Counter  = @Counter  + 1
END

and the message error:

Error: mismatched input 'DECLARE' expecting {'(', 'ADD', 'ALTER', 'ANALYZE', 'CACHE', 'CLEAR', 'COMMENT', 'COMMIT', 'CREATE', 'DELETE', 'DESC', 'DESCRIBE', 'DFS', 'DROP', 'EXPLAIN', 'EXPORT', 'FROM', 'GRANT', 'IMPORT', 'INSERT', 'LIST', 'LOAD', 'LOCK', 'MAP', 'MERGE', 'MSCK', 'REDUCE', 'REFRESH', 'REPLACE', 'RESET', 'REVOKE', 'ROLLBACK', 'SELECT', 'SET', 'SHOW', 'START', 'TABLE', 'TRUNCATE', 'UNCACHE', 'UNLOCK', 'UPDATE', 'USE', 'VALUES', 'WITH'}(line 2, pos 0)

== SQL ==

DECLARE '@Counter' INT
^^^

3
  • 1
    If you’re in a notebook, using the sql magic, you are writing SparkSQL against a Spark pool not T-SQL against a dedicated SQL pool. Commented May 27, 2022 at 19:44
  • I got it! And how can I get around this problem? Commented May 27, 2022 at 19:55
  • Just write that SQL in Synapse Studio or SSMS or Azure Data Studio when connected to your dedicated or serverless SQL pool. Commented May 27, 2022 at 20:47

1 Answer 1

2

If you’re in a notebook, using the sql magic, you are writing SparkSQL against a Spark pool not T-SQL against a dedicated SQL pool.

Just write that SQL in Synapse Studio or SSMS or Azure Data Studio when connected to your dedicated or serverless SQL pool.

Sign up to request clarification or add additional context in comments.

1 Comment

Would you consider upvoting and / or marking that as the answer if that has been useful to you? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.