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 ')'.
Tableau Desktop wraps Custom SQL queries in a SELECT statement before sending them to the database.
Run the DECLARE statement in an initial SQL command.
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.
001471583

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.