Query the contents of stored procedures on SQL Server

后端 未结 4 832
时光取名叫无心
时光取名叫无心 2021-02-03 21:10

I am exploring a legacy database system and have very little knowledge of its internals. I would like to find all the stored procedures that invoke another stored procedure

相关标签:
4条回答
  • 2021-02-03 21:10

    Try This only one statement can solve your problem..

    SELECT OBJECT_DEFINITION(OBJECT_ID(N'dbo.myStoredProc'))
    

    or

    SELECT @objname= OBJECT_DEFINITION(OBJECT_ID(N'dbo.myStoredProc'))
    print @objname
    
    0 讨论(0)
  • 2021-02-03 21:23

    This query will retrieve the textual definition of stored procedures and filter using a simple wildcard.

    For 2000 (untested, but IIRC it's the right table):

    select p.[type]
          ,p.[name]
          ,c.[text]
      from sysobjects p
      join syscomments c
        on p.object_id = c.id
     where p.[type] = 'P'
       and c.[text] like '%foo%'
    

    For 2005:

    select p.[type]
          ,p.[name]
          ,c.[text]
      from sys.objects p
      join sys.syscomments c
        on p.object_id = c.id
     where p.[type] = 'P'
       and c.[text] like '%foo%'
    

    For 2005 and 2008+

    select p.[type]
          ,p.[name]
          ,c.[definition]
      from sys.objects p
      join sys.sql_modules c
        on p.object_id = c.object_id
     where p.[type] = 'P'
       and c.[definition] like '%foo%'
    
    0 讨论(0)
  • 2021-02-03 21:27
    SELECT OBJECT_NAME(object_id),
           definition
    FROM sys.sql_modules
    WHERE objectproperty(object_id,'IsProcedure') = 1
      AND definition    like '%Foo%' 
    
    0 讨论(0)
  • 2021-02-03 21:32

    For SQL Server 2005/2008:

    SELECT  s.name SchemaName
            ,o.name RoutineName
            ,o.[type] RoutineType
            ,procs.*
    FROM    sys.sql_modules procs
    INNER JOIN sys.objects o ON procs.object_id = o.object_id 
    INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
    WHERE   procs.[definition] LIKE '%A%'
    --AND       o.[type] = 'P' --'P' for stored procedures
    
    0 讨论(0)
提交回复
热议问题