在数据库中查找某个字段的内容



-- ============================================================================
--  Author:      July
--  Create date: 2018-05-9
--  Description: 在整个数据库中查找字符串
============================================================================


CREATE PROCEDURE [dbo].[SeekString] 
-- Add the parameters for the stored procedure here
@str varchar(50),
@seek_type smallint     -- 0, 等于;  1, 包含;  2,以...开始;  3.以...结尾
AS
BEGIN
SET NOCOUNT ON;

DECLARE @table_name VARCHAR(255);
DECLARE @column_name VARCHAR(255);
DECLARE @columns_string VARCHAR(MAX);




IF OBJECT_ID('___rechard_all_tab_info') IS NOT NULL
DROP TABLE ___rechard_all_tab_info;

CREATE TABLE ___rechard_all_tab_info
(tab_name VARCHAR(255),
select_cols VARCHAR(MAX),
has_records BIT)


IF OBJECT_ID('tempdb..#all_cols') IS NOT NULL
DROP TABLE #all_cols;

SELECT tab.name AS tab_name, col.name AS col_name INTO #all_cols FROM sys.columns AS col 
INNER JOIN sys.tables AS tab ON col.object_id = tab.object_id 
INNER JOIN sys.types AS typ ON col.system_type_id = typ.system_type_id
WHERE (typ.name LIKE '%char%') -- OR (typ.name LIKE '%text%')
ORDER BY tab.name


DECLARE @temp_tab_name VARCHAR(255);
DECLARE @temp_col_string VARCHAR(MAX);
SET @temp_tab_name = '';
SET @temp_col_string = '';

DECLARE AllColumns CURSOR FOR
SELECT DISTINCT tab_name, [col_name] FROM #all_cols;

OPEN  AllColumns;

FETCH NEXT FROM AllColumns INTO @table_name, @column_name;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @table_name <> @temp_tab_name 
IF @temp_tab_name <> ''
BEGIN
INSERT INTO ___rechard_all_tab_info (tab_name,select_cols,has_records ) VALUES (@temp_tab_name,@temp_col_string,0);
SET @temp_col_string = '';
END
IF LEN(@temp_col_string) > 0 
SET @temp_col_string = @temp_col_string + ' OR ';

IF @seek_type = 1
SET @temp_col_string = @temp_col_string + ' ' + @column_name + ' LIKE ''%' + @str + '%''';
ELSE IF @seek_type = 2
SET @temp_col_string = @temp_col_string + ' ' + @column_name + ' LIKE ''' + @str + '%''';
ELSE IF @seek_type = 3
SET @temp_col_string = @temp_col_string + ' ' + @column_name + ' LIKE ''%' + @str + '''';
ELSE
SET @temp_col_string = @temp_col_string + ' ' + @column_name + ' = ''' + @str + '''';


SET @temp_tab_name = @table_name;
FETCH NEXT FROM AllColumns INTO @table_name, @column_name;
END
CLOSE AllColumns; 
DEALLOCATE AllColumns;

DECLARE @item INT;
DECLARE @select_string VARCHAR(MAX);
DECLARE @temp_select_string VARCHAR(MAX);
DECLARE @cnt int;

IF OBJECT_ID('___rechard_all_tab_info_1') IS NOT NULL
DROP TABLE ___rechard_all_tab_info_1;

SELECT * INTO ___rechard_all_tab_info_1 FROM ___rechard_all_tab_info;

DECLARE TableColumns CURSOR FOR 
SELECT tab_name, select_cols FROM ___rechard_all_tab_info

OPEN TableColumns;

FETCH NEXT FROM TableColumns INTO @table_name, @columns_string;

WHILE @@FETCH_STATUS = 0
BEGIN 
SET @select_string = 'DECLARE @cnt INT; ' +
'SELECT @cnt = COUNT(*) FROM ' + @table_name + ' WHERE ' + @columns_string +
'; IF @cnt > 0 ' +
'UPDATE ___rechard_all_tab_info_1 SET has_records = 1 WHERE tab_name = ''' + @table_name +'''; ' ;


EXECUTE(@select_string);

FETCH NEXT FROM TableColumns INTO @table_name, @columns_string;
END
CLOSE TableColumns;
DEALLOCATE TableColumns;

SELECT 'SELECT * FROM ' + tab_name + ' WHERE ' + select_cols FROM  ___rechard_all_tab_info_1 WHERE has_records = 1 ;
DROP TABLE ___rechard_all_tab_info_1;
DROP TABLE ___rechard_all_tab_info;
END


GO



展开阅读全文

没有更多推荐了,返回首页