Common Table Expressions (CTE's) do not work for Microsoft SQL Server database
See original GitHub issueProblem
Common Table Expressions (CTE) queries are not working as expected in SQL Lab against SQL Server databases. These are queries written using the WITH
clause.
Steps to Reproduce
Start superset using:
superset run -p 8080 --with-threads --reload --debugger
Add a SQL Server database and tables in Sources
.
Make sure to place a check for Allow DML
and ensure that it is selected and set to True.
Navigate to SQL Lab. Run the following query:
WITH t AS (SELECT 1 a) SELECT * FROM t
Expected Result
------
| a |
------
| 1 |
------
Observed Result
The following error message is seen:
2019-08-20 14:07:53,005:ERROR:root:('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near the keyword 'WITH'. (156) (SQLExecDirectW)")
Traceback (most recent call last):
File "/home/user/anaconda3/envs/superset/lib/python3.6/site-packages/superset/sql_lab.py", line 190, in execute_sql_statement
db_engine_spec.execute(cursor, sql, async_=True)
File "/home/user/anaconda3/envs/superset/lib/python3.6/site-packages/superset/db_engine_specs.py", line 421, in execute
cursor.execute(query)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near the keyword 'WITH'. (156) (SQLExecDirectW)")
2019-08-20 14:07:53,017:INFO:werkzeug:127.0.0.1 - - [20/Aug/2019 14:07:53] "POST /superset/sql_json/ HTTP/1.1" 500 -
System and Environment Details
Database: Microsoft SQL Server 2017
Operating System: Red Hat Enterprise Linux 7 x86_64
Python: Python 3.6.7
(Anaconda Python)
Superset:
Superset 0.33.0rc1
flask 1.1.1
werkzeug 0.15.5
Other packages:
sqlalchemy 1.3.7
sqlparse 0.3.0
unixodbc 2.3.7
pyodbc 4.0.23
FreeTDS 1.1.11
Additional Notes
The query runs as expected against the same database:
- Directly in
Microsoft SQL Server Management Studio 17
- Through FreeTDS (by running
tsql -S <FQDN>
and then executing the query) - Through pyODBC and SQLAlchemy in a python program.
The query runs as expected against a PostGreSQL database through SQL Lab.
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (8 by maintainers)
Top Results From Across the Web
WITH common_table_expression (Transact-SQL) - SQL Server
A view that contains a recursive common table expression can't be used to update data. Cursors may be defined on queries using CTEs....
Read more >Common Table Expressions (CTE's) do not work for Microsoft ...
Problem Common Table Expressions (CTE) queries are not working as expected in SQL Lab against SQL Server databases.
Read more >SQL Server Common Table Expressions (CTE)
A Common Table Expression, also called as CTE in short form, is a temporary named result set that you can reference within a...
Read more >What Is a Common Table Expression (CTE) in SQL?
CTEs work as virtual tables (with records and columns), created during the execution of a query, used by the query, and eliminated after...
Read more >Mastering Common Table Expression or CTE in SQL Server
to support us ❤️ in paying for web hosting to keep the website running. Summary: in this tutorial, you will learn about the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you check the diff, I added a warning about using CTEs on non-ANSIs.
Btw, I’m working on something that might improve CTE support on all engines and should fix this problem on non-ANSIs, too, but we’ll see how that works out. But in the meantime this is probably the best compromise I can offer.
Reproduced on master; running the same on sqlite works just fine. Will take a closer look.