1

此查询可以从任何 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/triggersSQL Server 2005 中是否存在任何查询。因为此查询不在这些区域中搜索。

4

3 回答 3

7

在程序/函数/触发器中搜索给定的字符串

这实际上要容易得多。

SELECT OBJECT_NAME(object_id), definition
  FROM sys.sql_modules
 WHERE definition LIKE '%'+@SearchStr+'%'

使用它的一种方法是将其添加到 TSQL 代码的末尾,即修改最后一个 SELECT:

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

就个人而言,我只是将它们作为单独的语句一个接一个地单独运行。

于 2012-11-01T11:04:22.100 回答
3

查看视图sys.sql_modules

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

于 2012-11-01T11:03:17.093 回答
0

这也是从 msDB 中查找 proc 等内容的有用查询,这意味着这将在 SQL Server 代理的作业中找到 table/function/proc ref。

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 '%'+@SearchStr+'%'

只能在选定数据库的过程/函数/触发器中找到给定的文本。

下面的查询用于检查 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 

查询以查找 DB 中表的约束详细信息。

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 中搜索某些文本的人会有所帮助。

这是在 sql server 中查找 SP/view/trigger 等定义的查询。

SELECT definition   
FROM sys.sql_modules  
WHERE object_id = OBJECT_ID(N'Object Name Here'); 

要查看表模式,我们可以使用 F1 并获取完整的详细信息,我们也可以使用以下查询

SELECT DISTINCT C.COLUMN_NAME, C.IS_NULLABLE, C.DATA_TYPE, TC.CONSTRAINT_TYPE, C.COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS AS C
FULL JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CC ON C.COLUMN_NAME = CC.COLUMN_NAME 
FULL JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON CC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME WHERE C.TABLE_NAME = 'Table Name Here';
于 2014-04-04T05:21:46.460 回答