extract a string from a stored proc definition

泪湿孤枕 提交于 2020-01-03 05:29:06

问题


I need to check the library used by several stored procs that extract data from a remote server.

I have (with SO help, see SO 21708681) built the below code:

DECLARE @tProcs TABLE
    (
        procID int IDENTITY,
        procObjectID nvarchar(100),
        procName nvarchar(100)
    );

insert into @tProcs     
SELECT object_id, name 
    FROM sys.objects 
        WHERE  type in (N'P', N'PC') and name like '%_Extract'

declare @countProcs int, @I int=0

select @countProcs=COUNT(*) from @tProcs

while @I<@countProcs
    Begin
        declare @source_code nvarchar(max)
        declare @objectID nvarchar(50)
        declare @proc_Name nvarchar(200)

        select @objectID=procObjectID from @tProcs where procID=@I
        select @proc_Name=procName from @tProcs where procID=@I

        select @source_code = definition
            from sys.sql_modules
            where object_id = @objectID 

        SELECT PATINDEX('BOCTEST.%', @proc_Name) as Pos, @proc_Name 

              -- or SELECT charindex(@source_code, '%BOCTEST%') 

        set @I=@I+1
    End

Inside each of the target stored procs there is a line like this:

DECLARE YP040P_cursor CURSOR FOR SELECT * FROM BOCTEST.S653C36C.LIVEBOC_A.YP040P

I need to know for each of the stored procs the part 'LIVEBOC_A' (which can either be 'LIVEBOC_A' or LIVEBOC_B)

I tried to use PATINDEX and CHARINDEX to get the location of the start opf that string in the definition from sysmodules but all I get back is either zero or an error that string or binary data would be truncated.


回答1:


try

SELECT 
    name, 
    table_name = CASE WHEN OBJECT_DEFINITION(OBJECT_ID) LIKE '%BOCTEST.S653C36C.LIVEBOC_A.YP040P%' THEN 'LIVEBOC_A'
                      WHEN OBJECT_DEFINITION(OBJECT_ID) LIKE '%BOCTEST.S653C36C.LIVEBOC_B.YP040P%' THEN 'LIVEBOC_B' END
FROM sys.objects o
WHERE o.[type] IN ('P', 'PC')
AND name like '%_Extract'



回答2:


You can do what you want with a query like this one:

select name         = s.name + '.' + p.name ,
       dt_created   = p.create_date ,
       dt_modified  = p.modify_date ,
       livboc_usage = case
                        when m.definition like '%declare%cursor%boctext.[^.].LIVEBOC_A.%' then 'A'
                        when m.definition like '%declare%cursor%boctext.[^.].LIVEBOC_B.%' then 'B'
                        else null
                      end
from sys.schemas     s
join sys.procedures  p on p.schema_id = s.schema_id
join sys.sql_modules m on m.object_id = p.object_id

But since what you're looking for are cross-server dependencies of a table, you should be able to get that simply by querying the system view sys.sql_expression_dependencies, which

Contains one row for each by-name dependency on a user-defined entity in the current database. A dependency between two entities is created when one entity, called the referenced entity, appears by name in a persisted SQL expression of another entity, called the referencing entity. For example, when a table is referenced in the definition of a view, the view, as the referencing entity, depends on the table, the referenced entity. If the table is dropped, the view is unusable.

You can use this catalog view to report dependency information for the following entities:

  • Schema-bound entities.
  • Non-schema-bound entities.
  • Cross-database and cross-server entities. Entity names are reported; however, entity IDs are not resolved.
  • Column-level dependencies on schema-bound entities. Column-level dependencies for non-schema-bound objects can be returned by using sys.dm_sql_referenced_entities.
  • Server-level DDL triggers when in the context of the master database.

To that end, running a query like this in the database the referencing stored procedures live should do you:

select name              = o.name      ,
       type              = o.type_desc ,
       liveboc_usage     = case d.referenced_schema_name
                             when 'liveboc_a' then 'A'
                             when 'liveboc_b' then 'B'
                             else                  null
                           end ,
       has_dependency_on =       d.referenced_server_name
                         + '.' + d.referenced_database_name
                         + '.' + d.referenced_schema_name
                         + '.' + d.referenced_entity_name
from sys.sql_expression_dependencies d
join sys.objects                     o on o.object_id = d.referenced_id
join sys.schemas                     s on s.schema_id = o.schema_id
where d.referenced_server_name   =    'BOCTEST'
  and d.referenced_database_name =    'S653C36C'
  and d.referenced_schema_name   like 'LIVEBOC_[AB]'
  and d.referenced_entity_name   =    'YP040P'


来源:https://stackoverflow.com/questions/21724490/extract-a-string-from-a-stored-proc-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!