retrieve views containing reference to a specific column

  • How one might one retrieve a list of views containing a reference to a specific column (even if that column is not returned as part of the view, but is used in a conditional statement)?

    I've used sp_depends 'table_name' to retrieve views dependent on a specific table, but now I would like to retrieve a subset of those views conditionally referencing a specific column in that table.

    Thank you

  • here you go...look at the WHERE statement at the end..specific column name, and must be a view...you can change that to fit your needs if needed:

    /*

    obj_nmcol_nmdep_obj_nmdep_obj_typedep_col_nm

    GMAACCPACTTBLKEYVW_GMBENEFViewNULL

    GMAACCPACTTBLKEYVW_CDBGACCOMPViewNULL

    */

    DECLARE @ColumnName VARCHAR(100)

    SET @ColumnName = 'ACTTBLKEY'

    select

    obj.name as obj_nm

    , col.name as col_nm

    , depobj.name as dep_obj_nm

    , CASE depobj.type

    WHEN 'C' THEN 'CHECK constraint'

    WHEN 'D' THEN 'Default'

    WHEN 'F' THEN 'FOREIGN KEY'

    WHEN 'FN' THEN 'Scalar function'

    WHEN 'IF' THEN 'In-lined table-function'

    WHEN 'K' THEN 'PRIMARY KEY'

    WHEN 'L' THEN 'Log'

    WHEN 'P' THEN 'Stored procedure'

    WHEN 'R' THEN 'Rule'

    WHEN 'RF' THEN 'Replication filter stored procedure'

    WHEN 'S' THEN 'System table'

    WHEN 'TF' THEN 'Table function'

    WHEN 'TR' THEN 'Trigger'

    WHEN 'U' THEN 'User table'

    WHEN 'V' THEN 'View'

    WHEN 'X' THEN 'Extended stored procedure'

    END as dep_obj_type

    , null as dep_col_nm

    from sysobjects obj

    join syscolumns col on obj.id = col.id

    left join (sysdepends dep join sysobjects depobj on depobj.id = dep.id)

    on obj.id = dep.depid

    and col.colid = dep.depnumber

    where col.name = @ColumnName

    AND depobj.type='V'

    --ROLLBACK TRAN

    --COMMIT TRAN

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Unbelievable. Beautiful. I plugged my column name into variable and voila, totally awesome. Thanks.

  • glad I could help!

    Thansk for the feedback!

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Kindly show me the edit that would include stored procedures in the result set:unsure:

  • hxkresl (5/26/2011)


    Kindly show me the edit that would include stored procedures in the result set:unsure:

    it's right at the end: remove the last AND to see ALL dependancies, ro make it look like the example below for views/procs/functions

    AND depobj.type='V'

    --change to

    AND depobj.type IN('V','P','FN','IF','TF')

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • oh, it's an IN, simple!~:kiss:

  • ^^^^^^^^^

    Old Skool 😀

    SELECT *

    FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE

    WHERE COLUMN_NAME = ''

    admittedly, it doesnt cover sp's

    These may be useful

    SELECT *

    FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS

    SELECT *

    FROM INFORMATION_SCHEMA.PARAMETERS

  • MysteryJimbo, however this doesn't retrieve views that make reference to the column in areas other than column headings. In many cases, my views are not pulling in the column but rows conditionally on the basis of whether that column, which has a bit datatype, is 0 or 1. Therefore, your script is very limited.

  • Lowell (5/26/2011)


    glad I could help!

    Thansk for the feedback!

    Great code but it's another reason for me to hate what MS thinks is "progress". :sick: The f4 key use to have that functionality and more in Query Analyzer.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • hxkresl (5/26/2011)


    MysteryJimbo, however this doesn't retrieve views that make reference to the column in areas other than column headings. In many cases, my views are not pulling in the column but rows conditionally on the basis of whether that column, which has a bit datatype, is 0 or 1. Therefore, your script is very limited.

    I wasnt really giving a script, meerly pointing out MS progress as Jeff is hinting at as well.

  • well, mince and dice however you like, it was exactly what I needed. Thanks.

    ps. AND made my day, in fact made my two days.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply