Search a column name across all databases

后端 未结 6 1729
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 12:21

I have this query that finds all tables and views that matches my column name of a certain database. I am using SQL SERVER 2008

SELECT table         


        
6条回答
  •  执念已碎
    2021-01-05 12:56

    Finding code across databases, the easiest way I've found is to sling a view with the below SQL in each DB then put in a master view that UNIONs them across the DBs. Bit of a pain if you've got loads of DBs or some you can't touch, but works well otherwise for me.

    select
        s.name as SchemaName,
        objs.name as ObjectName,
        objs.ObjectType,
        OBJECT_DEFINITION(objs.object_id) AS ObjectDefinition
    from
        (SELECT object_id, name, 'Proc' as ObjectType
        FROM    sys.procedures p
            UNION
        select  object_id, name, 'View'
        from    sys.views 
            UNION
        SELECT  object_id, Name, type
        FROM    sys.objects o
        WHERE   o.[type] IN ('fn', 'fs', 'ft', 'if', 'tf')) objs
        inner join sys.objects o
            on objs.object_id=o.object_id
        inner join sys.schemas s
            on o.schema_id=s.schema_id
    

    Columns (as in find a column in an abstract table on any database on the server), you could do the same thing and just UNION the information schemas, but that won't work for what I want so I'm on the hunt for a more general solution - will add it if I find it!

提交回复
热议问题