|
1 | | -{% macro sqlserver__information_schema_name(database) -%} |
2 | | - information_schema |
3 | | -{%- endmacro %} |
4 | | - |
5 | | - |
6 | | -{% macro sqlserver__get_columns_in_query(select_sql) %} |
7 | | - {% call statement('get_columns_in_query', fetch_result=True, auto_begin=False) -%} |
8 | | - select TOP 0 * from ( |
9 | | - {{ select_sql }} |
10 | | - ) as __dbt_sbq |
11 | | - where 0 = 1 |
12 | | - {% endcall %} |
13 | | - |
14 | | - {{ return(load_result('get_columns_in_query').table.columns | map(attribute='name') | list) }} |
15 | | -{% endmacro %} |
16 | | - |
17 | | - |
18 | | -{% macro sqlserver__list_relations_without_caching(schema_relation) %} |
19 | | - {% call statement('list_relations_without_caching', fetch_result=True) -%} |
20 | | - select |
21 | | - table_catalog as [database], |
22 | | - table_name as [name], |
23 | | - table_schema as [schema], |
24 | | - case when table_type = 'BASE TABLE' then 'table' |
25 | | - when table_type = 'VIEW' then 'view' |
26 | | - else table_type |
27 | | - end as table_type |
28 | | - |
29 | | - from [{{ schema_relation.database }}].information_schema.tables |
30 | | - where table_schema like '{{ schema_relation.schema }}' |
31 | | - {% endcall %} |
32 | | - {{ return(load_result('list_relations_without_caching').table) }} |
33 | | -{% endmacro %} |
34 | | - |
35 | | -{% macro sqlserver__list_schemas(database) %} |
36 | | - {% call statement('list_schemas', fetch_result=True, auto_begin=False) -%} |
37 | | - USE {{ database }}; |
38 | | - select name as [schema] |
39 | | - from sys.schemas |
40 | | - {% endcall %} |
41 | | - {{ return(load_result('list_schemas').table) }} |
42 | | -{% endmacro %} |
43 | | - |
44 | | -{% macro sqlserver__create_schema(relation) -%} |
45 | | - {% call statement('create_schema') -%} |
46 | | - USE [{{ relation.database }}]; |
47 | | - IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}') |
48 | | - BEGIN |
49 | | - EXEC('CREATE SCHEMA {{ relation.without_identifier().schema }}') |
50 | | - END |
51 | | - {% endcall %} |
52 | | -{% endmacro %} |
53 | | - |
54 | | -{% macro sqlserver__drop_schema(relation) -%} |
55 | | - {%- set tables_in_schema_query %} |
56 | | - SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES |
57 | | - WHERE TABLE_SCHEMA = '{{ relation.schema }}' |
58 | | - {% endset %} |
59 | | - {% set tables_to_drop = run_query(tables_in_schema_query).columns[0].values() %} |
60 | | - {% for table in tables_to_drop %} |
61 | | - {%- set schema_relation = adapter.get_relation(database=relation.database, |
62 | | - schema=relation.schema, |
63 | | - identifier=table) -%} |
64 | | - {% do drop_relation(schema_relation) %} |
65 | | - {%- endfor %} |
66 | | - |
67 | | - {% call statement('drop_schema') -%} |
68 | | - IF EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}') |
69 | | - BEGIN |
70 | | - EXEC('DROP SCHEMA {{ relation.schema }}') |
71 | | - END {% endcall %} |
72 | | -{% endmacro %} |
73 | | - |
74 | | -{% macro sqlserver__drop_relation(relation) -%} |
75 | | - {% call statement('drop_relation', auto_begin=False) -%} |
76 | | - {{ sqlserver__drop_relation_script(relation) }} |
77 | | - {%- endcall %} |
78 | | -{% endmacro %} |
79 | | - |
80 | | -{% macro sqlserver__drop_relation_script(relation) -%} |
81 | | - {% if relation.type == 'view' -%} |
82 | | - {% set object_id_type = 'V' %} |
83 | | - {% elif relation.type == 'table'%} |
84 | | - {% set object_id_type = 'U' %} |
85 | | - {%- else -%} invalid target name |
86 | | - {% endif %} |
87 | | - USE [{{ relation.database }}]; |
88 | | - if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null |
89 | | - begin |
90 | | - drop {{ relation.type }} {{ relation.include(database=False) }} |
91 | | - end |
92 | | -{% endmacro %} |
93 | | - |
94 | | -{% macro sqlserver__check_schema_exists(information_schema, schema) -%} |
95 | | - {% call statement('check_schema_exists', fetch_result=True, auto_begin=False) -%} |
96 | | - --USE {{ database_name }} |
97 | | - SELECT count(*) as schema_exist FROM sys.schemas WHERE name = '{{ schema }}' |
98 | | - {%- endcall %} |
99 | | - {{ return(load_result('check_schema_exists').table) }} |
100 | | -{% endmacro %} |
101 | | - |
102 | | - |
103 | | -{% macro sqlserver__create_view_exec(relation, sql) -%} |
104 | | - {%- set temp_view_sql = sql.replace("'", "''") -%} |
105 | | - execute('create view {{ relation.include(database=False) }} as |
106 | | - {{ temp_view_sql }} |
107 | | - '); |
108 | | -{% endmacro %} |
109 | | - |
110 | | - |
111 | | -{% macro sqlserver__create_view_as(relation, sql) -%} |
112 | | - USE [{{ relation.database }}]; |
113 | | - {{ sqlserver__create_view_exec(relation, sql) }} |
114 | | -{% endmacro %} |
115 | | - |
116 | | - |
117 | | -{% macro sqlserver__rename_relation(from_relation, to_relation) -%} |
118 | | - {% call statement('rename_relation') -%} |
119 | | - USE [{{ to_relation.database }}]; |
120 | | - EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}' |
121 | | - IF EXISTS( |
122 | | - SELECT * |
123 | | - FROM sys.indexes |
124 | | - WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation.schema }}.{{ to_relation.identifier }}')) |
125 | | - EXEC sp_rename N'{{ from_relation.schema }}.{{ to_relation.identifier }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX' |
126 | | - {%- endcall %} |
127 | | -{% endmacro %} |
128 | | - |
129 | | -{% macro sqlserver__create_clustered_columnstore_index(relation) -%} |
130 | | - {%- set cci_name = relation.schema ~ '_' ~ relation.identifier ~ '_cci' -%} |
131 | | - {%- set relation_name = relation.schema ~ '_' ~ relation.identifier -%} |
132 | | - {%- set full_relation = relation.schema ~ '.' ~ relation.identifier -%} |
133 | | - use [{{ relation.database }}]; |
134 | | - if EXISTS ( |
135 | | - SELECT * FROM |
136 | | - sys.indexes WHERE name = '{{cci_name}}' |
137 | | - AND object_id=object_id('{{relation_name}}') |
138 | | - ) |
139 | | - DROP index {{full_relation}}.{{cci_name}} |
140 | | - CREATE CLUSTERED COLUMNSTORE INDEX {{cci_name}} |
141 | | - ON {{full_relation}} |
142 | | -{% endmacro %} |
143 | | - |
144 | | -{% macro sqlserver__create_table_as(temporary, relation, sql) -%} |
145 | | - {%- set as_columnstore = config.get('as_columnstore', default=true) -%} |
146 | | - {% set tmp_relation = relation.incorporate( |
147 | | - path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'}, |
148 | | - type='view')-%} |
149 | | - {%- set temp_view_sql = sql.replace("'", "''") -%} |
150 | | - |
151 | | - {{ sqlserver__drop_relation_script(tmp_relation) }} |
152 | | - |
153 | | - {{ sqlserver__drop_relation_script(relation) }} |
154 | | - |
155 | | - USE [{{ relation.database }}]; |
156 | | - EXEC('create view {{ tmp_relation.include(database=False) }} as |
157 | | - {{ temp_view_sql }} |
158 | | - '); |
159 | | - |
160 | | - SELECT * INTO {{ relation }} FROM |
161 | | - {{ tmp_relation }} |
162 | | - |
163 | | - {{ sqlserver__drop_relation_script(tmp_relation) }} |
164 | | - |
165 | | - {% if not temporary and as_columnstore -%} |
166 | | - {{ sqlserver__create_clustered_columnstore_index(relation) }} |
167 | | - {% endif %} |
168 | | - |
169 | | -{% endmacro %} |
170 | | - |
171 | | -{% macro sqlserver__insert_into_from(to_relation, from_relation) -%} |
| 1 | +{# {% macro sqlserver__insert_into_from(to_relation, from_relation) -%} |
172 | 2 | SELECT * INTO {{ to_relation }} FROM {{ from_relation }} |
173 | | -{% endmacro %} |
174 | | - |
175 | | -{% macro sqlserver__current_timestamp() -%} |
176 | | - SYSDATETIME() |
177 | | -{%- endmacro %} |
178 | | - |
179 | | -{% macro sqlserver__get_columns_in_relation(relation) -%} |
180 | | - {% call statement('get_columns_in_relation', fetch_result=True) %} |
181 | | - SELECT |
182 | | - column_name, |
183 | | - data_type, |
184 | | - character_maximum_length, |
185 | | - numeric_precision, |
186 | | - numeric_scale |
187 | | - FROM |
188 | | - (select |
189 | | - ordinal_position, |
190 | | - column_name, |
191 | | - data_type, |
192 | | - character_maximum_length, |
193 | | - numeric_precision, |
194 | | - numeric_scale |
195 | | - from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS |
196 | | - where table_name = '{{ relation.identifier }}' |
197 | | - and table_schema = '{{ relation.schema }}' |
198 | | - UNION ALL |
199 | | - select |
200 | | - ordinal_position, |
201 | | - column_name collate database_default, |
202 | | - data_type collate database_default, |
203 | | - character_maximum_length, |
204 | | - numeric_precision, |
205 | | - numeric_scale |
206 | | - from tempdb.INFORMATION_SCHEMA.COLUMNS |
207 | | - where table_name like '{{ relation.identifier }}%') cols |
208 | | - order by ordinal_position |
209 | | - |
210 | | - |
211 | | - {% endcall %} |
212 | | - {% set table = load_result('get_columns_in_relation').table %} |
213 | | - {{ return(sql_convert_columns_in_relation(table)) }} |
214 | | -{% endmacro %} |
215 | | - |
216 | | -{% macro sqlserver__make_temp_relation(base_relation, suffix) %} |
217 | | - {% set tmp_identifier = '#' ~ base_relation.identifier ~ suffix %} |
218 | | - {% set tmp_relation = base_relation.incorporate( |
219 | | - path={"identifier": tmp_identifier}) -%} |
220 | | - |
221 | | - {% do return(tmp_relation) %} |
222 | | -{% endmacro %} |
223 | | - |
224 | | -{% macro sqlserver__snapshot_string_as_time(timestamp) -%} |
225 | | - {%- set result = "CONVERT(DATETIME2, '" ~ timestamp ~ "')" -%} |
226 | | - {{ return(result) }} |
227 | | -{%- endmacro %} |
228 | | - |
229 | | -{% macro default__alter_relation_add_remove_columns(relation, add_columns, remove_columns) %} |
230 | | - {# default__ macro uses "add column" |
231 | | - TSQL preferes just "add" |
232 | | - #} |
233 | | - {% if add_columns is none %} |
234 | | - {% set add_columns = [] %} |
235 | | - {% endif %} |
236 | | - {% if remove_columns is none %} |
237 | | - {% set remove_columns = [] %} |
238 | | - {% endif %} |
239 | | - |
240 | | - {% set sql -%} |
241 | | - |
242 | | - alter {{ relation.type }} {{ relation }} |
243 | | - |
244 | | - {% for column in add_columns %} |
245 | | - add {{ column.name }} {{ column.data_type }}{{ ',' if not loop.last }} |
246 | | - {% endfor %}{{ ',' if add_columns and remove_columns }} |
247 | | - |
248 | | - {% for column in remove_columns %} |
249 | | - drop column {{ column.name }}{{ ',' if not loop.last }} |
250 | | - {% endfor %} |
251 | | - |
252 | | - {%- endset -%} |
253 | | - |
254 | | - {% do run_query(sql) %} |
255 | | - |
256 | | -{% endmacro %} |
257 | | - |
258 | | -{% macro sqlserver__alter_column_type(relation, column_name, new_column_type) %} |
259 | | - |
260 | | - {%- set tmp_column = column_name + "__dbt_alter" -%} |
261 | | - |
262 | | - {% call statement('alter_column_type') -%} |
263 | | - |
264 | | - alter {{ relation.type }} {{ relation }} add {{ tmp_column }} {{ new_column_type }}; |
265 | | - update {{ relation }} set {{ tmp_column }} = {{ column_name }}; |
266 | | - alter {{ relation.type }} {{ relation }} drop column {{ column_name }}; |
267 | | - exec sp_rename '{{ relation | replace('"', '') }}.{{ tmp_column }}', '{{ column_name }}', 'column' |
268 | | -
|
269 | | - {%- endcall -%} |
270 | | -
|
271 | | -{% endmacro %} |
| 3 | +{% endmacro %} #} |
0 commit comments