Skip to content

Instantly share code, notes, and snippets.

@dlgombert
Last active April 14, 2022 13:03
Show Gist options
  • Select an option

  • Save dlgombert/875331abe035724cda2ae04369379959 to your computer and use it in GitHub Desktop.

Select an option

Save dlgombert/875331abe035724cda2ae04369379959 to your computer and use it in GitHub Desktop.
[tables in sp] Gets tables referenced in stored procedure #tsql #sql
-- Replace {{stored_procedure_name}} with Stored procedure name
DECLARE
@sp nvarchar(100)
SET @sp = N'{{stored_procedure_name}}'
-- Objects that depends on [@sp]
SELECT
referencing_entity_name
FROM sys.dm_sql_referencing_entities(@sp, 'OBJECT')
-- Objects on which [@sp] depends
SELECT DISTINCT
referenced_entity_name
FROM sys.dm_sql_referenced_entities(@sp, 'OBJECT')
SELECT DISTINCT
referenced_entity_name
FROM sys.sql_expression_dependencies
WHERE referencing_id = OBJECT_ID(@sp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment