|
26 | 26 | else table_type |
27 | 27 | end as table_type |
28 | 28 |
|
29 | | - from information_schema.tables |
| 29 | + from [{{ schema_relation.database }}].information_schema.tables |
30 | 30 | where table_schema like '{{ schema_relation.schema }}' |
31 | | - and table_catalog like '{{ schema_relation.database }}' |
32 | 31 | {% endcall %} |
33 | 32 | {{ return(load_result('list_relations_without_caching').table) }} |
34 | 33 | {% endmacro %} |
35 | 34 |
|
36 | 35 | {% macro sqlserver__list_schemas(database) %} |
37 | 36 | {% call statement('list_schemas', fetch_result=True, auto_begin=False) -%} |
| 37 | + USE {{ database }}; |
38 | 38 | select name as [schema] |
39 | 39 | from sys.schemas |
40 | 40 | {% endcall %} |
|
43 | 43 |
|
44 | 44 | {% macro sqlserver__create_schema(relation) -%} |
45 | 45 | {% call statement('create_schema') -%} |
46 | | - USE [{{ relation.database }}] |
| 46 | + USE [{{ relation.database }}]; |
47 | 47 | IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}') |
48 | 48 | BEGIN |
49 | 49 | EXEC('CREATE SCHEMA {{ relation.without_identifier().schema }}') |
|
72 | 72 | {% endmacro %} |
73 | 73 |
|
74 | 74 | {% macro sqlserver__drop_relation(relation) -%} |
75 | | - {% if relation.type == 'view' -%} |
76 | | - {% set object_id_type = 'V' %} |
77 | | - {% elif relation.type == 'table'%} |
78 | | - {% set object_id_type = 'U' %} |
79 | | - {%- else -%} invalid target name |
80 | | - {% endif %} |
81 | 75 | {% call statement('drop_relation', auto_begin=False) -%} |
82 | | - if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null |
83 | | - begin |
84 | | - drop {{ relation.type }} {{ relation.include(database=False) }} |
85 | | - end |
| 76 | + {{ sqlserver__drop_relation_script(relation) }} |
86 | 77 | {%- endcall %} |
87 | 78 | {% endmacro %} |
88 | 79 |
|
|
93 | 84 | {% set object_id_type = 'U' %} |
94 | 85 | {%- else -%} invalid target name |
95 | 86 | {% endif %} |
| 87 | + USE [{{ relation.database }}]; |
96 | 88 | if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null |
97 | 89 | begin |
98 | 90 | drop {{ relation.type }} {{ relation.include(database=False) }} |
|
107 | 99 | {{ return(load_result('check_schema_exists').table) }} |
108 | 100 | {% endmacro %} |
109 | 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 | + |
110 | 111 | {% macro sqlserver__create_view_as(relation, sql) -%} |
111 | | - create view {{ relation.schema }}.{{ relation.identifier }} as |
112 | | - {{ sql }} |
| 112 | + USE [{{ relation.database }}]; |
| 113 | + {{ sqlserver__create_view_exec(relation, sql) }} |
113 | 114 | {% endmacro %} |
114 | 115 |
|
115 | 116 |
|
116 | 117 | {% macro sqlserver__rename_relation(from_relation, to_relation) -%} |
117 | 118 | {% call statement('rename_relation') -%} |
| 119 | + USE [{{ to_relation.database }}]; |
118 | 120 | EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}' |
119 | 121 | IF EXISTS( |
120 | 122 | SELECT * |
|
128 | 130 | {%- set cci_name = relation.schema ~ '_' ~ relation.identifier ~ '_cci' -%} |
129 | 131 | {%- set relation_name = relation.schema ~ '_' ~ relation.identifier -%} |
130 | 132 | {%- set full_relation = relation.schema ~ '.' ~ relation.identifier -%} |
| 133 | + use [{{ relation.database }}]; |
131 | 134 | if EXISTS ( |
132 | 135 | SELECT * FROM |
133 | 136 | sys.indexes WHERE name = '{{cci_name}}' |
|
149 | 152 |
|
150 | 153 | {{ sqlserver__drop_relation_script(relation) }} |
151 | 154 |
|
152 | | - EXEC('create view {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} as |
| 155 | + USE [{{ relation.database }}]; |
| 156 | + EXEC('create view {{ tmp_relation.include(database=False) }} as |
153 | 157 | {{ temp_view_sql }} |
154 | 158 | '); |
155 | 159 |
|
156 | | - SELECT * INTO {{ relation.schema }}.{{ relation.identifier }} FROM |
157 | | - {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} |
| 160 | + SELECT * INTO {{ relation }} FROM |
| 161 | + {{ tmp_relation }} |
158 | 162 |
|
159 | 163 | {{ sqlserver__drop_relation_script(tmp_relation) }} |
160 | 164 |
|
|
165 | 169 | {% endmacro %}_ |
166 | 170 |
|
167 | 171 | {% macro sqlserver__insert_into_from(to_relation, from_relation) -%} |
168 | | - {%- set full_to_relation = to_relation.schema ~ '.' ~ to_relation.identifier -%} |
169 | | - {%- set full_from_relation = from_relation.schema ~ '.' ~ from_relation.identifier -%} |
170 | | - |
171 | | - SELECT * INTO {{full_to_relation}} FROM {{full_from_relation}} |
172 | | - |
| 172 | + SELECT * INTO {{ to_relation }} FROM {{ from_relation }} |
173 | 173 | {% endmacro %} |
174 | 174 |
|
175 | 175 | {% macro sqlserver__current_timestamp() -%} |
|
192 | 192 | character_maximum_length, |
193 | 193 | numeric_precision, |
194 | 194 | numeric_scale |
195 | | - from INFORMATION_SCHEMA.COLUMNS |
| 195 | + from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS |
196 | 196 | where table_name = '{{ relation.identifier }}' |
197 | 197 | and table_schema = '{{ relation.schema }}' |
198 | 198 | UNION ALL |
|
0 commit comments