Jan 19, 2016 I'm currently using tableau public for a proof of concept for work. Is it possible for me to connect to a query in Access without having to rewrite the query in Tableau? Looking further ahead, I'll be writing queries in SQL server and want to connect to them via Tableau server. Tableau Server enables everyone in an organisation to see and understand data, with offerings for every user type. Starting with casual users looking to make data-driven decisions from a published dashboard, data enthusiasts who want to use web authoring to ask new questions from published data source, to data geeks who want to create and share.
This article describes how to connect Tableau to a Microsoft SQL Server database and set up the data source.
Note: Use the Microsoft SQL Server connector to connect to Microsoft SQL Server Parallel Data Warehouse (PDW), Microsoft Azure Synapse Analytics (formerly Azure SQL Data Warehouse), or Microsoft Azure SQL Database.
Before you begin, gather this connection information:
Name of the server you want to connect to
(Optional) Port number if you want to connect to a non-default port
(Optional) Database name if you want to connect to a contained database
Authentication method: Windows Authentication or user name and password
Are you connecting to an SSL server?
Do you want to set the database isolation level to read uncommitted data?
(Optional) Initial SQL statement to run every time Tableau connects
This connector requires a driver to talk to the database. You might already have the required driver installed on your computer. If the driver is not installed on your computer, Tableau displays a message in the connection dialog box with a link to the Driver Download(Link opens in a new window) page where you can find driver links and installation instructions.
Start Tableau and under Connect, select Microsoft SQL Server. For a complete list of data connections, select More under To a Server. Then do the following:
Enter the name of the server you want to connect to.
To connect to a non-default port, use the
<server name>, <port number> format when entering the server name. For example,
(Optional) Enter a database name if you want to connect to a contained database.
Select how you want to sign in to the server. Specify whether to use Windows Authentication or a specific user name and password. If the server is password protected, and you are not in a Kerberos environment, you must enter the user name and password.
Select the Require SSL check box when connecting to an SSL server.
Specify whether to Read uncommitted data. This option lets Tableau running at the read-uncommitted isolation level. Long queries from other transactions, including extract refreshes, can lock the database and delay Tableau's transactions. Select this option to allow queries to read rows that have been modified by other transactions even when they have not been committed yet. When this option is cleared, Tableau uses the default isolation level specified by the database.
(Optional) Select Initial SQL to specify a SQL command to run at the beginning of every connection, such as when you open the workbook, refresh an extract, sign in to Tableau Server, or publish to Tableau Server. For more information, see Run Initial SQL.
Select Sign In.
If Tableau can't make the connection, verify that your credentials are correct. If you still can't connect, your computer is having trouble locating the server. Contact your network administrator or database administrator.
On the data source page, do the following:
(Optional) Select the default data source name at the top of the page, and then enter a unique data source name for use in Tableau. For example, use a data source naming convention that helps other users of the data source figure out which data source to connect to.
From the Database drop-down list, select a database or use the text box to search for a database by name.
Under Table, select a table or use the text box to search for a table by name.
You can also specify a stored procedure in the database. For more information about stored procedures, including a list of constraints specific to SQL Server databases, see Use a Stored Procedure.
Starting with Tableau version 2018.1, you can connect to spatial columns. For more information, see Connect to Spatial Data in a Database.
Drag the table or stored procedure to the canvas, and then select the sheet tab to start your analysis.
Use custom SQL to connect to a specific query rather than the entire data source. For more information, see Connect to a Custom SQL Query. Starting with Tableau version 2018.1, you can use Custom SQL to perform advanced spatial analysis on spatial columns in Microsoft SQL Server. For more information, see Use Custom SQL and RAWSQL to perform advanced spatial analysis.
Note: Tableau Desktop does not support the Microsoft SQL Server TIME data type. Fields of this data type are not imported and do not appear in Tableau Desktop. If included in stored procedures, TIME data type fields will not appear in Tableau Desktop. For more information, see Use a Stored Procedure.
If you use Tableau Desktop on a Mac, when you enter the server name to connect, use a fully qualified domain name, such as mydb.test.ourdomain.lan, instead of a relative domain name, such as mydb or mydb.test.
Alternatively, you can add the domain to the list of Search Domains for the Mac computer so that when you connect, you need to provide only the server name. To update the list of Search Domains, go to System Preferences > Network > Advanced, and then open the DNS tab.
When you open a workbook, you may see a “Worksheet Unavailable” error message and then, when you select Edit Connection, you are prompted to provide your sign-in credentials. This error typically displays under one of two circumstances:
You don't have access to a data connection used in the workbook.
Your credentials are invalid, for example, your password has expired.
To resolve, verify that your sign-in credentials are correct. If they are, contact the Tableau administrator to ask about access to the data connection.
SQL Server (among other databases) has a handy feature for easy re-use of queries, in the guise of variables. You can declare variables and use them repeatedly in any query in the current session. That’s pretty handy for any complicated query forms you use repeatedly, where each time you might need to change some basic criteria.
As a trivial example: here’s how to select all the customers I have that live in postcode 12345
DECLARE @postcode varchar(5)
SET @postcode = '12345'
SELECT POSTAL_CODE, CUSTOMER_NO FROM [MY_CUSTOMERS] WHERE POSTAL_CODE = @postcode
I can then run it another day for postcode 23456 by just changing the second line to
SET @postcode = '23456'
This example is a bit silly because you might as well just type 12345 or 23456 into the query itself – but imagine a more complex SQL statement, or set of coordinated SQL statements, with screenfuls of code, and you might see how it makes it much easier to maintain.
But, sad to say, you can’t use these sort of variables as part of the custom SQL feature in Tableau. If you try, then you’ll get this slightly unhelpful message:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near the keyword 'DECLARE'.
[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ')'.
But rather than have to hardcode your queries and modify them for every extract you can use a very simple alternative: our friend, the Tableau parameter.
For each SQL variable, create a Tableau parameter of the same data type. You can put in a default value as the “Current value” when setting it up.
Then, when connecting to your database, paste in the original SQL statement you had into the Custom SQL box, but removing the variable declaration and setting from the top.
SELECT POSTAL_CODE, CUSTOMER_NO FROM [MY_CUSTOMERS] WHERE POSTAL_CODE = @postcode
One at a time, highlight each SQL variable there, i.e. the @postcode part of the above, press the “Insert Parameter” button under the text field and select the appropriate Tableau parameter.
(There is actually the option to create a new parameter at this stage, if you didn’t do so in advance).
You’ll end up with a similar looking SQL statement, but with the variables now referred to in this sort of way:
SELECT POSTAL_CODE, CUSTOMER_NO FROM [MY_CUSTOMERS]
WHERE POSTAL_CODE = <Parameters.Postcode>
And it’ll work!
You can then use the standard Tableau functionality to change the parameters in Tableau, and behind the scenes your SQL statement will update appropriately.
Note firstly, for performance, that each change of this sort of parameter is going to cause the query to be re-run on the database (unless it’s already been cached).
Secondly, if you have created a Tableau Data Extract from this datasource, then altering the parameter won’t change anything until you refresh the extract.
Finally, the Tableau parameter is simply a normal Tableau parameter, just like anything else. So there’s nothing to stop a Tableau user altering it, which depending on your setup might be either exactly what you want, or exactly what you don’t want. If the latter, you might consider having the datasource involving parameters in a separate workbook to the user-facing dashboard (publishing to Tableau Server to share), or at least making sure it’s not visible on the end-user dashboards if they have no reason to change it.
Quick sidenote: I wondered if the “Initial SQL” feature might work, to declare variables upon connection to the datasource which could then be used in custom SQL later. But sadly not, I guess it doesn’t count as the same session. If you try, you’ll see a message like:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Must declare the scalar variable '@postcode'