Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlserver__get_columns_in_query does not work with CTEs #450

Open
robertkosz opened this issue Sep 27, 2023 · 0 comments
Open

sqlserver__get_columns_in_query does not work with CTEs #450

robertkosz opened this issue Sep 27, 2023 · 0 comments

Comments

@robertkosz
Copy link

The implementation use the following pattern:
select TOP 0 * from (
{{ select_sql }}
) as __dbt_sbq
where 0 = 1

When select_sql contains CTEs (with) this will fail as TSQL does not support this syntax.
A possible solution would be to use something like this:

{% macro sqlserver__get_columns_in_query(select_sql) %}
{% set get_columns_in_query -%}
SELECT name
FROM
sys.dm_exec_describe_first_result_set
('{{ escape_single_quotes(select_sql) }}', NULL, 0)
{% endset %}

{{ return (run_query(get_columns_in_query).columns[0].values() | list ) }}

{% endmacro %}

It works with any TSQL select statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant