Skip to content

Commit 724f8eb

Browse files
author
Andrew Ardill
committed
macros: add get_columns_in_relation
1 parent 6ff8e67 commit 724f8eb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

dbt/include/sqlserver/macros/adapter/columns.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,36 @@
4848
{% do run_query(rename_column) %}
4949
5050
{% endmacro %}
51+
52+
{% macro sqlserver__get_columns_in_relation(relation) -%}
53+
{% set query_label = apply_label() %}
54+
{% call statement('get_columns_in_relation', fetch_result=True) %}
55+
{{ get_use_database_sql(relation.database) }}
56+
with mapping as (
57+
select
58+
row_number() over (partition by object_name(c.object_id) order by c.column_id) as ordinal_position,
59+
c.name collate database_default as column_name,
60+
t.name as data_type,
61+
c.max_length as character_maximum_length,
62+
c.precision as numeric_precision,
63+
c.scale as numeric_scale
64+
from sys.columns c {{ information_schema_hints() }}
65+
inner join sys.types t {{ information_schema_hints() }}
66+
on c.user_type_id = t.user_type_id
67+
where c.object_id = object_id('{{ 'tempdb..' ~ relation.include(database=false, schema=false) if '#' in relation.identifier else relation }}')
68+
)
69+
70+
select
71+
column_name,
72+
data_type,
73+
character_maximum_length,
74+
numeric_precision,
75+
numeric_scale
76+
from mapping
77+
order by ordinal_position
78+
{{ query_label }}
79+
80+
{% endcall %}
81+
{% set table = load_result('get_columns_in_relation').table %}
82+
{{ return(sql_convert_columns_in_relation(table)) }}
83+
{% endmacro %}

0 commit comments

Comments
 (0)