KNOWLEDGE BASE

Error "Incorrect syntax near the keyword 'DECLARE'" When Using DECLARE in Custom SQL


Published: 03 Sep 2018
Last Modified Date: 14 Nov 2023

Issue

When using a DECLARE statement to declare a variable in Custom SQL with a SQL Server data source, the following error occurs:

[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near the keyword 'DECLARE'.
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near ')'.

Environment

  • Tableau Desktop
  • Microsoft SQL Server

Resolution

Run the DECLARE statement in an initial SQL command.

Cause

Tableau Desktop wraps Custom SQL queries in a SELECT statement before sending them to the database.

Additional Information

You can find an example of using initial SQL to run a DECLARE statement in the following blog post: Actually you can use variables, CTEs and other fancy SQL with Tableau after all.

Note: The above blog post is hosted on a third-party website. Please note that while we make every effort to keep references to third-party content accurate and up to date, the information we provide here might change without notice.


 
Did this article resolve the issue?