简体   繁体   中英

How to identify used columns of a view in a stored procedure

There is a view View1 in Database D1 with columns Column1 , Column2 , Column3

The view View1 is consumed in another Database D2 within a stored procedure PROC1
Only Column2 is used in the stored procedure PROC1 while other columns are not used

The view View1 is consumed in another Database D3 within a stored procedure PROC2
Column1 and Column2 are used in the stored procedure PROC2 while other column is not used

How do we identify which column of the View1 are consumed in the stored procedure PROC1 , PROC2 by using a query.

I have almost 100 views which are consumed in multiple databases and I need to know which all columns are consumed by stored procedures in their respective database.

You can execute:

exec sp_depends 'name of view'

this will return all the objects (include procedures) that refer to the view. You would have to examine the named proc itself to determine which columns are used in the procedure.

Within a DB you can identify where column1 is used in stored procedure:

DECLARE @Search VARCHAR(255)
SET @Search = 'Column1'

SELECT DISTINCT
    o.name AS Object_Name, o.type_desc
FROM 
    sys.sql_modules m 
INNER JOIN 
    sys.objects o ON m.object_id = o.object_id
WHERE 
    m.definition LIKE '%' + @Search + '%' 
    AND type_desc = 'SQL_STORED_PROCEDURE'
ORDER BY 
    2, 1

If you're looking in a different DB, then include [differentDBname].sys.sql_modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM