Skip to content

Commit

Permalink
Merge pull request #403 from dbt-msft/use-sql_expression_dependencies
Browse files Browse the repository at this point in the history
replace stored proc with sql_expression_dependencies
  • Loading branch information
sdebruyn authored May 22, 2023
2 parents 517d18a + ed8a024 commit b46bca3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions dbt/include/sqlserver/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
{% macro sqlserver__drop_relation_script(relation) -%}
{% call statement('find_references', fetch_result=true) %}
USE [{{ relation.database }}];
SELECT referencing_schema_name, referencing_entity_name
FROM sys.dm_sql_referencing_entities ('{{ relation.include(database=false) }}', 'object')
select
sch.name as schema_name,
obj.name as view_name
from sys.sql_expression_dependencies refs
inner join sys.objects obj
on refs.referencing_id = obj.object_id
inner join sys.schemas sch
on obj.schema_id = sch.schema_id
where refs.referenced_database_name = '{{ relation.database }}'
and refs.referenced_schema_name = '{{ relation.schema }}'
and refs.referenced_entity_name = '{{ relation.identifier }}'
and refs.referencing_class = 1
and obj.type = 'V'
{% endcall %}
{% set references = load_result('find_references')['data'] %}
{% for reference in references -%}
Expand Down

0 comments on commit b46bca3

Please sign in to comment.