-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathcreate_table_as.sql
More file actions
35 lines (27 loc) · 1.07 KB
/
create_table_as.sql
File metadata and controls
35 lines (27 loc) · 1.07 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
{% macro sqlserver__create_table_as(temporary, relation, sql) -%}
{%- set as_columnstore = config.get('as_columnstore', default=true) -%}
{%- set option_clause = config.get('option_clause') -%}
{%- set sql_header = config.get('sql_header') -%}
{% set tmp_relation = relation.incorporate(
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
type='view')-%}
{%- set temp_view_sql = sql.replace("'", "''") -%}
{{ sqlserver__drop_relation_script(tmp_relation) }}
{{ sqlserver__drop_relation_script(relation) }}
USE [{{ relation.database }}];
EXEC('create view {{ tmp_relation.include(database=False) }} as
{{ temp_view_sql }}
');
{% if sql_header %}
{{ sql_header }}
{% endif %}
SELECT * INTO {{ relation }} FROM
{{ tmp_relation }}
{% if option_clause %}
OPTION( {{ option_clause }} )
{% endif %}
{{ sqlserver__drop_relation_script(tmp_relation) }}
{% if not temporary and as_columnstore -%}
{{ sqlserver__create_clustered_columnstore_index(relation) }}
{% endif %}
{% endmacro %}