0

当存储过程在服务器上的任何数据库中引用特定数据库中的列(假设数据库是AA)时,我希望这 5 列作为输出。

Column_name  Table_Name   Schema_Name   Procedure_Name   Database_Name
4

2 回答 2

1

其中四列非常容易获得 - 对于您所在的当前数据库:

SELECT
    TableName = t.Name,
    SchemaName = s.Name,
    ColumnName = c.Name,
    DatabaseName = DB_NAME()
FROM 
    sys.tables t
INNER JOIN
    sys.schemas s ON [t].[schema_id] = [s].[schema_id]
INNER JOIN 
    sys.columns c ON [t].[object_id] = [c].[object_id]

您不能轻易获得的是所有数据库中所有表中的所有列。此外:确定每列在哪个程序中使用也相当棘手(或几乎不可能)。

于 2013-08-12T13:55:50.657 回答
0

From marc_s answer i generate a query which gives comma separated Procedure_Name depended by that table

 SELECT
    TableName = t.Name,
    SchemaName = s.Name,
    ColumnName = c.Name,
    DatabaseName = DB_NAME(),
    STUFF((SELECT  ',' +  name 
         FROM sys.procedures
         WHERE object_id in (SELECT distinct id from sys.sysdepends
                                WHERE depid = (SELECT object_id FROM sys.tables
                                                WHERE name=t.Name)
                              )
        FOR XML PATH('')), 1, 0, '') AS sps
FROM 
    sys.tables t
INNER JOIN
    sys.schemas s ON [t].[schema_id] = [s].[schema_id]
INNER JOIN 
    sys.columns c ON [t].[object_id] = [c].[object_id]
于 2013-08-12T14:11:40.310 回答