forked from dbt-msft/dbt-sqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.sql
More file actions
50 lines (39 loc) · 1.84 KB
/
create.sql
File metadata and controls
50 lines (39 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{% macro sqlserver__create_table_as(temporary, relation, sql) -%}
{%- set query_label = apply_label() -%}
{%- set tmp_relation = relation.incorporate(path={"identifier": relation.identifier ~ '__dbt_tmp_vw'}, type='view') -%}
{%- do adapter.drop_relation(tmp_relation) -%}
USE [{{ relation.database }}];
{{ get_create_view_as_sql(tmp_relation, sql) }}
{%- set table_name -%}
{{ relation }}
{%- endset -%}
{%- set contract_config = config.get('contract') -%}
{%- set query -%}
{% if contract_config.enforced and (not temporary) %}
CREATE TABLE {{table_name}}
{{ get_assert_columns_equivalent(sql) }}
{{ build_columns_constraints(relation) }}
{% set listColumns %}
{% for column in model['columns'] %}
{{ "["~column~"]" }}{{ ", " if not loop.last }}
{% endfor %}
{%endset%}
INSERT INTO {{relation}} WITH (TABLOCK) ({{listColumns}})
SELECT {{listColumns}} FROM {{tmp_relation}} {{ query_label }}
{% else %}
SELECT * INTO {{ table_name }} FROM {{ tmp_relation }} {{ query_label }}
{% endif %}
{%- endset -%}
EXEC('{{- escape_single_quotes(query) -}}')
{# For some reason drop_relation is not firing. This solves the issue for now. #}
EXEC('DROP VIEW IF EXISTS {{tmp_relation.schema}}.{{tmp_relation.identifier}}')
{% set as_columnstore = config.get('as_columnstore', default=true) %}
{% if not temporary and as_columnstore -%}
{#-
add columnstore index
this creates with dbt_temp as its coming from a temporary relation before renaming
could alter relation to drop the dbt_temp portion if needed
-#}
{{ sqlserver__create_clustered_columnstore_index(relation) }}
{% endif %}
{% endmacro %}