2012-11-01 65 views
0

此查詢可以從任何SQL Server數據庫的所有表/視圖中搜索任何字符串值(表名,表數據等),當您將任何字符串置於「正文這裏「它會從所有的表中找出您的指定字符串/視圖在數據庫中存在,如何搜索SQL Server數據庫中的字符串

DECLARE @SearchStr nvarchar(100) 
    SET @SearchStr = 'your text here' 

DECLARE @Results TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630)) 

SET NOCOUNT ON 

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110) 
SET @TableName = '' 
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''') 

WHILE @TableName IS NOT NULL 
BEGIN 
    SET @ColumnName = '' 
    SET @TableName = 
    (
     SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)) 
     FROM INFORMATION_SCHEMA.TABLES 
     WHERE  TABLE_TYPE = 'BASE TABLE' 
      AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName 
      AND OBJECTPROPERTY(
        OBJECT_ID(
         QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) 
         ), 'IsMSShipped' 
          ) = 0 
    ) 

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) 
    BEGIN 
     SET @ColumnName = 
     (
      SELECT MIN(QUOTENAME(COLUMN_NAME)) 
      FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE  TABLE_SCHEMA = PARSENAME(@TableName, 2) 
       AND TABLE_NAME = PARSENAME(@TableName, 1) 
       AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar') 
       AND QUOTENAME(COLUMN_NAME) > @ColumnName 
     ) 

     IF @ColumnName IS NOT NULL 
     BEGIN 
      INSERT INTO @Results 
      EXEC 
      (
       'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
       FROM ' + @TableName + ' (NOLOCK) ' + 
       ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2 
      ) 
     END 
    END 
END 

SELECT ColumnName, ColumnValue FROM @Results 

但我喜歡搜索定字符串中的procedures/functions/triggers以及是否有任何疑問存在於SQL服務器由於此查詢不在這些區域進行搜索。

回答

4

搜索給定的字符串中的過程/函數/觸發

這實際上是容易得多。使用

SELECT OBJECT_NAME(object_id), definition 
    FROM sys.sql_modules 
WHERE definition LIKE '%'[email protected]+'%' 

一種方法是將其添加到您的TSQL代碼結束,即修改最後一個SELECT:

SELECT ColumnName, ColumnValue 
    FROM @Results 
UNION ALL 
SELECT OBJECT_NAME(object_id), definition 
    FROM sys.sql_modules 
WHERE definition LIKE '%'[email protected]+'%' 

就個人而言,我只是運行它們分開一前一後作爲單獨的陳述。

+1

解釋更新瞭解釋 – RichardTheKiwi

3

查看視圖sys.sql_modules

或者,在SQL Server的前期版本2005,syscomments

+0

syscomments嚴重缺乏,尤其是,將文本分解爲多個記錄。這會導致在長模塊上搜索到的文本有時會丟失。 – RichardTheKiwi

0

這也是一個有用的查詢,從msdb中找到像proc等東西意味着這將在SQL Server代理的作業中找到表/函數/ proc參考。

USE [msdb] 
GO 
SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled 
FROM dbo.sysjobs j 
JOIN dbo.sysjobsteps js ON js.job_id = j.job_id 
JOIN master.dbo.sysservers s  ON s.srvid = j.originating_server_id 
WHERE js.command LIKE N'%YOUR TEXT HERE%' 

如果上面的查詢中接受的答案

SELECT OBJECT_NAME(object_id), definition 
FROM sys.sql_modules 
WHERE definition LIKE '%'[email protected]+'%' 

只有努力找到在過程/函數在給定的文本/從選擇DB觸發。

下面的查詢工作,以檢查SQL Server代理作業的作業狀態:

SELECT sj.name 
    , sja.* 
FROM msdb.dbo.sysjobactivity AS sja 
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id 

SELECT TOP 10 * FROM msdb.dbo.sysjobhistory 

查詢找到數據庫表的約束的詳細信息。

SELECT 
    FK_Table = FK.TABLE_NAME, 
    FK_Column = CU.COLUMN_NAME, 
    PK_Table = PK.TABLE_NAME, 
    PK_Column = PT.COLUMN_NAME, 
    Constraint_Name = C.CONSTRAINT_NAME 
FROM 
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
    ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
    ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
    ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
INNER JOIN (
      SELECT 
       i1.TABLE_NAME, 
       i2.COLUMN_NAME 
      FROM 
       INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 
      INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 
       ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME 
      WHERE 
       i1.CONSTRAINT_TYPE = 'PRIMARY KEY' 
      ) PT 
    ON PT.TABLE_NAME = PK.TABLE_NAME 
    WHERE PT.table_name = 'Table Name' 

要檢查SQL作業代理運行作業狀態:

use msdb       
go       
SELECT j.name JobName    
, STUFF(STUFF(h.run_date,5,0,'-'),8,0,'-') as Date 
, CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST (h.run_time AS VARCHAR(6)), 6),5, 0, ':'), 3, 0, ':') AS DATETIME) AS StartDate 
, STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(h.run_duration as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') AS DURATION 
, CASE h.run_status    
      WHEN 0 THEN 'failed'   
      WHEN 1 THEN 'Succeded'   
      WHEN 2 THEN 'Retry'    
      WHEN 3 THEN 'Cancelled'   
      WHEN 4 THEN 'In Progress'  
     END AS ExecutionStatus   

FROM sysjobhistory h   
     INNER JOIN sysjobs j    
      ON j.job_id = h.job_id  
        WHERE STEP_ID = 0 
        AND CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST (h.run_time AS VARCHAR(6)), 6),5, 0, ':'), 3, 0, ':') AS DATETIME)>=getdate()-90 
        AND (j.name Like 'DW%' 
        ) 
ORDER BY j.name, STUFF(STUFF(h.run_date,5,0,'-'),8,0,'-') 

所以這可能是有人在搜索DB一些文本幫助。

相關問題