查询以根据列查找存储过程

时间:2012-03-09 20:01:20

标签: sql sql-server sql-server-2008 stored-procedures

我必须更改表的列的名称和数据类型。我在数据库中有大约150个存储过程,其中大约25个存储过程指向同一列。我需要一个查询,可以找到依赖于此列的所有存储过程的名称。

5 个答案:

答案 0 :(得分:19)

我使用此查询:

SELECT OBJECT_NAME(M.object_id), M.*
FROM sys.sql_modules M
JOIN sys.procedures P
ON M.object_id = P.object_id
WHERE M.definition LIKE '%blah%'

显然你必须用“blah”代替你的专栏名称。

答案 1 :(得分:4)

试试这个1 来自Sp

SELECT Name as [Stored Procedure Name]
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%getdate%' order by Name

从表

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name;

答案 2 :(得分:0)

<强>问题:

    如您所知,无法查询函数或存储过程引用的字段     我们能得到的最接近的是近似值     我们可以告诉哪些表被引用以及这些表可能引用哪些字段
    例如,如果“ Person ”表引用了“ CreatedDate ”并且您加入了“订单”表(也有一个“ CreatedDate ”字段),当你只是在寻找“假阳性”与“ Order.CreatedDate ”的匹配时“ Person.CreatedDate ”。

    不幸的是,我们现在可以做的最好的事情是搜索对象脚本的 Text 以获取字段名称。
    好消息是,它不会错过识别实际使用的字段     如果它可能比 所使用的更多(由于共享字段名称或注释掉的代码)。
     only 异常是动态SQL,因为如果它们嵌入在动态字符串中,则表格不会链接到Object Scripts。

解决方法:

CREATE FUNCTION [dbo].[ft_Schema_Column_Script]
(
    @ScriptName nVarChar(128) = '%',
    @TableName  nVarChar(128) = '%',
    @ColumnName nVarChar(128) = '%'
)
RETURNS TABLE
AS
RETURN
(
    SELECT @@SERVERNAME[ServerName], DB_NAME()[DatabaseName],
           SS.name[ScriptSchemaName], SO.name[ScriptName],
           SO.type_desc[ScriptType], 
           TS.name[TableSchemaName], T.name[TableName], C.name[ColumnName],
           UT.name[ColumnType], C.max_length[MaxLength],
           C.precision[NumericPrecision], C.scale[Scale],
           C.is_nullable[Nullable],
           C.is_identity[IsIdentity],
           C.column_id[Ordinal],
           EP.value[Description]
      FROM sys.sql_modules as M
      JOIN sys.objects     as SO--Script Object.
        ON M.object_id = SO.object_id
      JOIN sys.schemas as SS--Script Schema.
        ON SS.schema_id = SO.schema_id
      JOIN sys.sql_expression_dependencies D
        ON D.referencing_id = SO.object_id
      JOIN sys.tables as T
        ON T.object_id = D.referenced_id
      JOIN sys.schemas as TS--Table Schema.
        ON TS.schema_id = T.schema_id
      JOIN sys.columns as C
        ON C.object_id = T.object_id
      LEFT JOIN sys.types AS UT--Left Join because of user-defined/newer types.
        ON UT.user_type_id = C.user_type_id
      LEFT JOIN sys.extended_properties AS EP
        ON EP.major_id = C.object_id
       AND EP.minor_id = C.column_id
       AND EP.name = 'MS_Description'
     WHERE  T.name LIKE @TableName  ESCAPE '\'
       AND  C.name LIKE @ColumnName ESCAPE '\'
       AND SO.name LIKE @ScriptName ESCAPE '\'
       --Use RegEx to exclude false-posotives by from similar ColumnNames.
       --  (e.g. Ignore the "ModifiedBy" field when matching on "Modified").
       --  Use C.name instead of @ColumnName to further reduce false-positives.
       AND M.definition LIKE (  N'%[ ~`!@#$\%\^&*()+-=\[\]\\{}|;'''':",./<>?]'
                              + C.name
                              + N'[ ~`!@#$\%\^&*()+-=\[\]\\{}|;'''':",./<>?]%'
                             ) ESCAPE '\'
)
GO


测试:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
   SELECT * FROM dbo.ft_Schema_Column_Script('ScriptName', DEFAULT, DEFAULT) as C
   SELECT * FROM dbo.ft_Schema_Column_Script(DEFAULT, 'TableName',  DEFAULT) as C
   SELECT * FROM dbo.ft_Schema_Column_Script(DEFAULT, DEFAULT, 'ColumnName') as C
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

测试上面的示例,看看它是否足以满足您的需求
结果:

运行此功能时搜索列名“已创建”的输出示例 Example of running the function searching for the Column Name "Created".

它搜索存储过程(Sprocs),用户定义函数(UDF),触发器,但不是作业

很酷的是:
这不仅可以搜索脚本引用的列,,还可以搜索列(或表)引用的脚本!

答案 3 :(得分:0)

仅在存储过程中搜索:

SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'BusinessEntityID' + '%'

答案 4 :(得分:0)

- 在所有对象中搜索

SELECT OBJECT_NAME(OBJECT_ID), 定义 FROM sys.sql_modules 定义LIKE&#39;%&#39; +&#39; BusinessEntityID&#39; +&#39;%&#39;