Updated....Okay I had way too much fun with this
THe Proc接受两项参数,即搜查和搜查表;适用标准。 您可以基本上通过第二参数的条款。 我写道,用两点文字来解释。
GO
/****** Object: StoredProcedure [dbo].[SearchAllTables] Script Date: 05/04/2011 14:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROC [dbo].[SearchAllTables2] ( @SEARCH_TABLE NVARCHAR(255), @CONDITION AS NVARCHAR(MAX) ) AS
BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi -- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @COND_STR NVARCHAR(MAX)
SET @TableName =
--SET @SearchStr2 = QUOTENAME( % + @SearchStr + % , )
WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName =
SET @CONDITION = REPLACE(@CONDITION, " , )
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 AND TABLE_NAME = @SEARCH_TABLE
) 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 QUOTENAME(COLUMN_NAME) > @ColumnName )
IF @ColumnName IS NOT NULL
BEGIN
SET @COND_STR = REPLACE(@CONDITION, , " )
INSERT INTO #Results
EXEC ( SELECT DISTINCT + @TableName + . + @ColumnName + , + @COND_STR + AS CONDITION FROM + @TableName + (NOLOCK) + WHERE + @ColumnName + + @CONDITION)
PRINT ( SELECT DISTINCT + @TableName + . + @ColumnName + , + @COND_STR + AS CONDITION FROM + @TableName + (NOLOCK) + WHERE + @ColumnName + + @CONDITION)
END
END
END
SELECT ColumnName, ColumnValue
FROM #Results
END
GO
-- to execute
exec [SearchAllTables2] TABLENAME , LIKE "%DOUG%" -- double quotes are automatically escaped to single quotes...
原始代码从版权下修改,只使用部分。
GO
/****** Object: StoredProcedure [dbo].[SearchAllTables] Script Date: 05/04/2011 14:29:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROC [dbo].[SearchAllTables2] ( @TABLE_NAME NVARCHAR(255) ) AS
BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi -- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
CREATE TABLE #Results (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 AND TABLE_NAME = @TABLE_NAME
) 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 QUOTENAME(COLUMN_NAME) > @ColumnName )
IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC ( SELECT DISTINCT + @TableName + . + @ColumnName + , IS NULL FROM + @TableName + (NOLOCK) + WHERE + @ColumnName + IS NULL )--LIKE + @SearchStr2 )
--PRINT ( SELECT DISTINCT + @TableName + . + @ColumnName + , IS NOT NULL FROM + @TableName + (NOLOCK) + WHERE + @ColumnName + IS NOT NULL )--LIKE + @SearchStr2 )
END
END
END
SELECT ColumnName, ColumnValue
FROM #Results
END
GO
-- to execute
exec [SearchAllTables2] Master