Запрос ссылок на хранимую процедуру в MS SQL Server - PullRequest
0 голосов
/ 29 мая 2019

Мне нужно найти ссылки на все хранимые процедуры в недокументированной базе данных. Некоторые ссылки не работают, поэтому что-то вроде ниже будет выдавать ошибки

SELECT 
    DB_NAME()       AS REFERENCING_DB,
    t.SCHEMA_NAME   AS REFERENCING_SCHEMA,
    t.OBJECT_NAME   AS REFERENCING_OBJECT,
    t.type_desc     AS REFERENCING_TYPE_DESC,
    --,'' AS SEPERATOR1
    CASE 
       WHEN ISNULL(ref.referenced_database_name, DB_NAME()) = '' 
          THEN '?' 
          ELSE ISNULL(ref.referenced_database_name, DB_NAME()) 
    END AS [REFERENCED_DB],
    ref.referenced_schema_name  AS REFERENCED_SCHEMA,
    ref.referenced_entity_name  AS REFERENCED_OBJECT,
    ref.referenced_minor_name   AS REFERENCED_MINOR_OBJECT,
    obj.type_desc               AS REFERENCED_TYPE_DESC,
    ref.referenced_class_desc   AS REFERENCED_CLASS_DESC,
    ref.is_caller_dependent     AS REFERENCED_IS_CALLER_DEPENDENT
FROM 
    (SELECT 
         o.name AS OBJECT_NAME, o.TYPE_DESC, s.name AS SCHEMA_NAME
     FROM
         sys.objects o
     LEFT JOIN 
         sys.schemas s on o.schema_id = s.schema_id
    ) t 
CROSS APPLY 
    sys.dm_sql_referenced_entities('[' + t.SCHEMA_NAME + '].[' + t.OBJECT_NAME + ']', 'OBJECT') ref
LEFT JOIN 
    sys.objects obj ON ref.referenced_id = obj.object_id

Ошибка:

Сообщение 942, Уровень 14, Состояние 4, Строка 231
База данных «BlahBlahBlah» не может быть открыта, потому что она находится в автономном режиме.

Любая помощь в решении этой проблемы или идеи по инструментам для поиска зависимостей в SQL Server было бы полезно знать!

1 Ответ

0 голосов
/ 29 мая 2019

Текущее решение для быстрого поиска зависимостей ...

------------------------------------------------------------------
--because of the possibility of an object in this database reference a broken link... we have to batch through each object we wish to lookup it's references... 
------------------------------------------------------------------
SET NOCOUNT ON;  

if OBJECT_ID('tempdb..#ReferencingLookup') is not null 
    drop table #ReferencingLookup;
CREATE TABLE tempdb..#ReferencingLookup(
    [REFERENCING_DB]                    [nvarchar](1000) NULL,
    [REFERENCING_SCHEMA]                [nvarchar](1000) NULL,
    [REFERENCING_OBJECT]                [nvarchar](1000) NULL,
    [REFERENCING_TYPE_DESC]             [nvarchar](1000) NULL,
    [REFERENCED_DB]                     [nvarchar](1000) NULL,
    [REFERENCED_SCHEMA]                 [nvarchar](1000) NULL,
    [REFERENCED_OBJECT]                 [nvarchar](1000) NULL,
    [REFERENCED_MINOR_OBJECT]           [nvarchar](1000) NULL,
    [REFERENCED_TYPE_DESC]              [nvarchar](1000) NULL,
    [REFERENCED_CLASS_DESC]             [nvarchar](1000) NULL,
    [REFERENCED_IS_CALLER_DEPENDENT]    [bit] NOT NULL
) ON [PRIMARY]

DECLARE @REFERENCING_SCHEMA varchar(1000), @REFERENCING_OBJECT varchar(1000), @REFERENCING_TYPE_DESC nvarchar(1000);

DECLARE referencing_cursor CURSOR FOR  
select  distinct REFERENCING_SCHEMA,REFERENCING_OBJECT,REFERENCING_TYPE_DESC
from 
(
    select 
        t.SCHEMA_NAME       as REFERENCING_SCHEMA
        ,t.OBJECT_NAME      as REFERENCING_OBJECT
        ,t.type_desc        as REFERENCING_TYPE_DESC
    FROM 
    (
        select o.name as OBJECT_NAME,o.TYPE_DESC,s.name as SCHEMA_NAME
        from sys.objects        o
        left join sys.schemas   s   on  o.schema_id =   s.schema_id

    )   t
)   a

OPEN referencing_cursor  

FETCH NEXT FROM referencing_cursor   
INTO @REFERENCING_SCHEMA, @REFERENCING_OBJECT, @REFERENCING_TYPE_DESC

declare @ref_type as varchar(1000) = 'OBJECT'

WHILE @@FETCH_STATUS = 0  
BEGIN 

        declare @concat_refs as varchar(1000) = '[' + @REFERENCING_SCHEMA + '].[' + @REFERENCING_OBJECT + ']'

        begin try   

            print @concat_refs

            insert into #ReferencingLookup
            select 
             DB_NAME()          as REFERENCING_DB
            ,t.SCHEMA_NAME      as REFERENCING_SCHEMA
            ,t.OBJECT_NAME      as REFERENCING_OBJECT
            ,t.type_desc        as REFERENCING_TYPE_DESC

            --,'' as SEPERATOR1

            ,case when isnull(ref.referenced_database_name,DB_NAME()) = '' then '?' else isnull(ref.referenced_database_name,DB_NAME()) end     as [REFERENCED_DB]
            ,ref.referenced_schema_name         as REFERENCED_SCHEMA
            ,ref.referenced_entity_name         as REFERENCED_OBJECT
            ,ref.referenced_minor_name          as REFERENCED_MINOR_OBJECT

            ,obj.type_desc                      as REFERENCED_TYPE_DESC
            ,ref.referenced_class_desc          as REFERENCED_CLASS_DESC
            ,ref.is_caller_dependent            as REFERENCED_IS_CALLER_DEPENDENT

            FROM 
            (
                select o.name as OBJECT_NAME,o.TYPE_DESC,s.name as SCHEMA_NAME
                from sys.objects        o
                left join sys.schemas   s   on  o.schema_id =   s.schema_id
                where s.name = @REFERENCING_SCHEMA and o.name = @REFERENCING_OBJECT
            )                                                                                                   t 
            cross apply sys.dm_sql_referenced_entities(@concat_refs, @ref_type)     ref
            left join sys.objects                                                                               obj on  ref.referenced_id   =   obj.object_id


        end try
        begin catch
            select ERROR_MESSAGE();
        end catch

        FETCH NEXT FROM referencing_cursor   
        INTO @REFERENCING_SCHEMA, @REFERENCING_OBJECT, @REFERENCING_TYPE_DESC

END
CLOSE referencing_cursor;
DEALLOCATE referencing_cursor;

------------------------------------------------------------------
------------------------------------------------------------------

--peak a boo at the results...

select *
,'[' + REFERENCED_DB  +'].[' + REFERENCED_SCHEMA + '].[' + REFERENCED_OBJECT + ']' as REFERENCED_OBJECT_FULLY_SPECIFIED
from #ReferencingLookup
...