我改编了一个最初由Narayana Vyas Kondreddi in 2002编写的脚本。我更改了where子句以检查text / ntext字段,使用patindex而不是like。我也略微改变了结果表。不合理地,我改变了变量名称,并按照我的意愿调整(不尊重Kondretti先生)。用户可能想要更改搜索的数据类型。我使用全局表来查询中间处理,但永久表可能是更明智的方法。
/* original script by Narayana Vyas Kondreddi, 2002 */
/* adapted by Oliver Holloway, 2009 */
/* these lines can be replaced by use of input parameter for a proc */
declare @search_string varchar(1000);
set @search_string = 'what.you.are.searching.for';
/* create results table */
create table ##string_locations (
table_name varchar(1000),
field_name varchar(1000),
field_value varchar(8000)
)
;
/* special settings */
set nocount on
;
/* declare variables */
declare
@table_name varchar(1000),
@field_name varchar(1000)
;
/* variable settings */
set @table_name = ''
;
set @search_string = QUOTENAME('%' + @search_string + '%','''')
;
/* for each table */
while @table_name is not null
begin
set @field_name = ''
set @table_name = (
select MIN(QUOTENAME(table_schema) + '.' + QUOTENAME(table_name))
from INFORMATION_SCHEMA.TABLES
where
table_type = 'BASE TABLE' and
QUOTENAME(table_schema) + '.' + QUOTENAME(table_name) > @table_name and
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(table_schema) + '.' + QUOTENAME(table_name)), 'IsMSShipped') = 0
)
/* for each string-ish field */
while (@table_name is not null) and (@field_name is not null)
begin
set @field_name = (
select MIN(QUOTENAME(column_name))
from INFORMATION_SCHEMA.COLUMNS
where
table_schema = PARSENAME(@table_name, 2) and
table_name = PARSENAME(@table_name, 1) and
data_type in ('char', 'varchar', 'nchar', 'nvarchar', 'text', 'ntext') and
QUOTENAME(column_name) > @field_name
)
/* search that field for the string supplied */
if @field_name is not null
begin
insert into ##string_locations
exec(
'select ''' + @table_name + ''',''' + @field_name + ''',' + @field_name +
'from ' + @table_name + ' (nolock) ' +
'where patindex(' + @search_string + ',' + @field_name + ') > 0' /* patindex works with char & text */
)
end
;
end
;
end
;
/* return results */
select table_name, field_name, field_value from ##string_locations (nolock)
;
/* drop temp table */
--drop table ##string_locations
;
答案 0 :(得分:1)
将结果表定义修改为:
create table ##string_locations (
table_name varchar(1000),
field_name varchar(1000),
field_value varchar(8000),
id_field_name varchar(max),
id int
)
声明这些变量
@id_field_name varchar(max),
@qry varchar(max)
在此评论的正上方包含此声明'/ *搜索字符串提供的字段* /'
SELECT @id_field_name = (SELECT top 1 QUOTENAME(keys.COLUMN_NAME)
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE keys
WHERE keys.TABLE_NAME =PARSENAME(@table_name, 1)
and keys.TABLE_SCHEMA = PARSENAME(@table_name, 2)
and OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1)
将insert语句修改为:
SELECT @qry = 'select ''' + @table_name + ''',''' + @field_name + ''',''' + @field_name + ''',''' + ISNULL(@id_field_name, '') + ''',' + ISNULL(@id_field_name, 'NULL') +
' from ' + @table_name + ' (nolock) ' +
'where patindex(' + @search_string + ',' + @field_name + ') > 0';
insert into ##string_locations(table_name,
field_name,
field_value,
id_field_name,
id)
exec(
@qry /* patindex works with char & text */
)
然后修改结果显示:
select * from ##string_locations;
完整的代码是:
/* original script by Narayana Vyas Kondreddi, 2002 */
/* adapted by Oliver Holloway, 2009 */
/* these lines can be replaced by use of input parameter for a proc */
declare @search_string varchar(1000);
set @search_string = 'Alpha';
/* create results table */
drop table ##string_locations;
create table ##string_locations (
table_name varchar(1000),
field_name varchar(1000),
field_value varchar(8000),
id_field_name varchar(max),
id int
)
;
/* special settings */
set nocount on
;
/* declare variables */
declare
@table_name varchar(1000),
@field_name varchar(1000),
@id_field_name varchar(max),
@qry varchar(max)
;
/* variable settings */
set @table_name = ''
;
set @search_string = QUOTENAME('%' + @search_string + '%','''')
;
/* for each table */
while @table_name is not null
begin
set @field_name = ''
set @table_name = (
select MIN(QUOTENAME(table_schema) + '.' + QUOTENAME(table_name))
from INFORMATION_SCHEMA.TABLES
where
table_type = 'BASE TABLE' and
QUOTENAME(table_schema) + '.' + QUOTENAME(table_name) > @table_name and
OBJECTPROPERTY(OBJECT_ID(QUOTENAME(table_schema) + '.' + QUOTENAME(table_name)), 'IsMSShipped') = 0
)
/* for each string-ish field */
while (@table_name is not null) and (@field_name is not null)
begin
SELECT @field_name = MIN(QUOTENAME(cols.column_name))
from INFORMATION_SCHEMA.COLUMNS cols
where
cols.table_schema = PARSENAME(@table_name, 2) and
cols.table_name = PARSENAME(@table_name, 1) and
cols.data_type in ('char', 'varchar', 'nchar', 'nvarchar', 'text', 'ntext') and
QUOTENAME(cols.column_name) > @field_name
SELECT @id_field_name = (SELECT top 1 QUOTENAME(keys.COLUMN_NAME) FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE keys
WHERE keys.TABLE_NAME =PARSENAME(@table_name, 1)
and keys.TABLE_SCHEMA = PARSENAME(@table_name, 2)
and OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1)
/* search that field for the string supplied */
if @field_name is not null
begin
SELECT @qry = 'select ''' + @table_name + ''',''' + @field_name + ''',''' + @field_name + ''',''' + ISNULL(@id_field_name, 'NULL') + ''',' + ISNULL(@id_field_name, 'NULL') +
' from ' + @table_name + ' (nolock) ' +
'where patindex(' + @search_string + ',' + @field_name + ') > 0';
insert into ##string_locations(table_name, field_name, field_value, id_field_name, id)
exec(
@qry
)
end
;
end
;
end
;
select * from ##string_locations;
;
答案 1 :(得分:0)
我想要实现的示例结果:
table_name [ProjectDescription]
field_name [dbo]。[ProjectProposals]
field_value Sencha项目
ID 23