1. 程式人生 > >SQL Server中搜索特定的物件

SQL Server中搜索特定的物件

一、註釋中帶某關鍵字的物件

主要用到 sys.tables 、sys.columns 、sys.procedures  系統物件表以及sys.extended_properties 擴充套件屬性表

--查詢列
SELECT  A.name AS table_name ,
         B.name AS column_name ,
         C.value AS column_description
FROM    sys.tables A
         INNER JOIN sys.columns B ON B.object_id = A.object_id
         LEFT
JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id WHERE CAST(C.[value] AS VARCHAR(1000)) LIKE '%年假%'; --查詢表 SELECT A.name AS table_name , C.value AS column_description FROM sys.tables A INNER
JOIN sys.extended_properties C ON C.major_id = A.object_id AND C.minor_id = 0 WHERE CAST(C.[value] AS VARCHAR(1000)) LIKE '%請假%' --查詢儲存過程 SELECT A.name AS table_name , C.value AS column_description FROM sys.procedures A INNER JOIN
sys.extended_properties C ON C.major_id = A.object_id AND C.minor_id = 0 WHERE CAST(C.[value] AS VARCHAR(1000)) LIKE '%年假%'

二、定義語句中帶某關鍵字的物件

主要用到 dbo.sysobjects 系統物件表以及sys.all_sql_modules 物件定義語句表

--老方式
SELECT   DISTINCT b.name, b.xtype
FROM     dbo.syscomments a, dbo.sysobjects b
WHERE    a.id = b.id
         AND b.xtype = 'p'
         AND a.text LIKE '%LotMax%'
ORDER BY name;

--從 2008 開始,新方式
SELECT   name, type_desc
FROM     sys.all_sql_modules s
         INNER JOIN sys.all_objects o ON s.object_id = o.object_id
WHERE    definition LIKE '%LotMax%'
ORDER BY type_desc, name;