Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Temukan nama tabel di semua objek dari semua database

DECLARE @table_name SYSNAME = N'%';



DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += 'SELECT DISTINCT Database_Name = ''' + QUOTENAME(name) + ''',
        COALESCE(sd.referenced_schema_name +''.'', '''')+ o.name AS TableName,
        r.name AS Ref_Object,
        r.type_desc AS Ref_Object_Name
FROM    ' + QUOTENAME(name) + '.sys.sql_expression_dependencies AS sd
        INNER JOIN ' + QUOTENAME(name) + '.sys.objects AS o
        ON o.object_id = sd.referenced_id
        INNER JOIN ' + QUOTENAME(name) + '.sys.objects AS r
        ON sd.referencing_id = r.object_id
WHERE   o.is_ms_shipped =  0
        and referenced_id = o.object_id
        AND o.type_desc = ''USER_TABLE''
        AND o.name LIKE ''' + @table_name + '''
UNION ALL
'
 FROM sys.databases 
 WHERE database_id BETWEEN 5 AND 32766;

SET @sql = LEFT(@sql, LEN(@sql)-11) 
    + 'ORDER BY Database_Name, TableName,Ref_Object,Ref_Object_Name';

EXEC sp_executesql @sql;

EDIT

Di atas akan menemukan semua referensi dalam setiap basis data, tetapi tidak akan menemukan referensi lintas basis data. Butuh sedikit waktu bermain, dan hasilnya tidak persis seperti yang Anda inginkan, tapi menurut saya itu membuatnya lebih jelas:

DECLARE @table_name SYSNAME = N'%'; -- find all



CREATE TABLE #d
(
    db SYSNAME, 
    [object_id] INT, 
    sch SYSNAME,
    obj SYSNAME,
    ref_db NVARCHAR(128),
    ref_sch NVARCHAR(128), 
    ref_obj NVARCHAR(128),
    ref_object_id INT,
    type_desc SYSNAME
);

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += 'SELECT ''' + QUOTENAME(name) + ''',
    d.referencing_id,
    QUOTENAME(s.name),
    QUOTENAME(o.name),
    QUOTENAME(d.referenced_database_name),
    QUOTENAME(d.referenced_schema_name),
    QUOTENAME(d.referenced_entity_name),
    d.referenced_id,
    o.type_desc
FROM ' + QUOTENAME(name) 
    + '.sys.sql_expression_dependencies AS d
INNER JOIN ' + QUOTENAME(name) 
    + '.sys.objects AS o
    ON d.referencing_id = o.[object_id]
INNER JOIN ' 
    + QUOTENAME(name) + '.sys.schemas AS s
    ON o.[schema_id] = s.[schema_id]
WHERE d.referenced_entity_name LIKE ''' + @table_name + '''
UNION ALL
'
FROM sys.databases WHERE database_id BETWEEN 5 AND 32766;

SET @sql = LEFT(@sql, LEN(@sql)-11);

INSERT #d EXEC sp_executesql @sql;

SELECT 
    db+'.'+sch+'.'+obj, 
    ' (' + type_desc + ') references => ',
    COALESCE(ref_db, db)+'.'+ref_sch+'.'+ref_obj
    FROM #d;
GO

DROP TABLE #d;
GO

Contoh keluaran:

[db1].[dbo].[foo]  (SQL_STORED_PROCEDURE) references =>   [db2].[dbo].[bar]
[db1].[dbo].[xyz]  (SQL_STORED_PROCEDURE) references =>   [db1].[dbo].[table_xyz]


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Penginstalan dan Konfigurasi SQL Server Log Pengiriman &Pemulihan Bencana -2

  2. Manfaat Menggunakan Notasi Posisi Ordinal SQL?

  3. Batasan pemeriksaan tumpang tindih rentang tanggal

  4. Mengimpor Excel ke Database SQL menggunakan vb.net dan asp.net

  5. Kolom Dinamis Pivot, tanpa Agregasi