--2008查找某数据库中的列是否存在某个值 create proc spFind_Column_In_DB ( @type int,--类型:1为文字类型、2为数值类型 @str nvarchar(100)--需要搜索的名字 ) as --创建临时表存放结果 create table #tbl(PK int identity primary key ,tbl sysname,col sysname) declare @tbl nvarchar(300),@col sysname,@sql nvarchar(1000) if @type=1 begin declare curTable cursor fast_forward for select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id where o.type_desc='user_table' and user_type_id in (167,175,231,239,35,99) end else begin declare curTable cursor fast_forward for select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id where o.type_desc='user_table' and user_type_id in (56,48,52,59,60,62,106,108,122) end open curtable fetch next from curtable into @tbl,@col while @@FETCH_STATUS=0 begin set @sql='if exists (select * from '+@tbl+' where ' if @type=1 begin set @sql += @col + ' like ''%'+@str +'%'')' end else begin set @sql +=@col + ' in ('+@str+'))' end set @sql += ' INSERT #TBL(tbl,col) VALUES('''+@tbl+''','''+@col+''')' --print @sql exec (@sql) fetch next from curtable into @tbl,@col end close curtable deallocate curtable select * from #tbl--使用例子,查询库中存在aaa这个值的列: exec spFind_Column_In_DB 1,'aaa'
declare @name nvarchar(100) declare cur cursor for select name from sysobjects where type = 'U' open cur fetch next from cur into @name WHILE @@FETCH_STATUS = 0 begin declare @sql nvarchar(500),@s varchar(500) set @s ='' set @sql='select @s=isnull(@s+''+'','''')+'''''',''''''+''+cast(''+name+'' as varchar)'' from syscolumns where id=object_id('''+@name+''') and xtype in(175,239,99,231,35,167) ' exec sp_executesql @sql,N'@s varchar(500) out',@s out if len(@s) > 0 exec ('if exists(select 1 from (select '+ @s+' as col from ['+@name+']) b where charindex(''aa'',col)>0) print '''+@name+'''') fetch next from cur into @name end close cur DEALLOCATE cur
修改一下,应该能用 CREATE TABLE tb(id int,col varchar(20)) CREATE TABLE tb2(id int,data varchar(20))INSERT tb VALUES(1,'中国'); INSERT tb VALUES(2,'liangck')INSERT tb2 VALUES(1,'China') GOCREATE PROCEDURE dbo.FindString @string NVARCHAR(100) AS DECLARE @SQL NVARCHAR(4000); SET @SQL = N' DECLARE @str NVARCHAR(4000); SELECT @str = ISNULL(@str + N'' OR '' + c.name + N'' LIKE N''''%' + @string + ' %'''''', c.name + N'' LIKE N''''%' + @string +'%'''''') FROM syscolumns AS c JOIN systypes AS t ON c.id=OBJECT_ID(''?'') AND c.xtype=t.xtype AND t.name IN(''varchar'',''char'',''nvarchar'',''nchar''); SET @str = ''SELECT TOP 1 1 FROM ? WHERE ''+@str; CREATE TABLE #tb(a int); INSERT #tb(a) EXEC(@str); IF EXISTS(SELECT * FROM #tb) PRINT ''?'' '; EXEC sp_MsforeachTable @SQL; GOEXEC dbo.FindString N'中国'GO DROP PROCEDURE dbo.FindString DROP TABLE tb,tb2
create proc spFind_Column_In_DB
(
@type int,--类型:1为文字类型、2为数值类型
@str nvarchar(100)--需要搜索的名字
)
as
--创建临时表存放结果
create table #tbl(PK int identity primary key ,tbl sysname,col sysname)
declare @tbl nvarchar(300),@col sysname,@sql nvarchar(1000)
if @type=1
begin
declare curTable cursor fast_forward
for
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (167,175,231,239,35,99)
end
else
begin
declare curTable cursor fast_forward
for
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (56,48,52,59,60,62,106,108,122)
end
open curtable
fetch next from curtable into @tbl,@col
while @@FETCH_STATUS=0
begin
set @sql='if exists (select * from '+@tbl+' where '
if @type=1
begin
set @sql += @col + ' like ''%'+@str +'%'')'
end
else
begin
set @sql +=@col + ' in ('+@str+'))'
end set @sql += ' INSERT #TBL(tbl,col) VALUES('''+@tbl+''','''+@col+''')'
--print @sql
exec (@sql)
fetch next from curtable into @tbl,@col
end
close curtable
deallocate curtable
select * from #tbl--使用例子,查询库中存在aaa这个值的列:
exec spFind_Column_In_DB 1,'aaa'
declare cur cursor for select name from sysobjects where type = 'U'
open cur
fetch next from cur into @name
WHILE @@FETCH_STATUS = 0
begin declare @sql nvarchar(500),@s varchar(500)
set @s =''
set @sql='select @s=isnull(@s+''+'','''')+'''''',''''''+''+cast(''+name+'' as varchar)'' from syscolumns where id=object_id('''+@name+''') and xtype in(175,239,99,231,35,167) '
exec sp_executesql @sql,N'@s varchar(500) out',@s out
if len(@s) > 0
exec ('if exists(select 1 from (select '+ @s+' as col from ['+@name+']) b where charindex(''aa'',col)>0) print '''+@name+'''')
fetch next from cur into @name
end
close cur
DEALLOCATE cur
CREATE TABLE tb(id int,col varchar(20))
CREATE TABLE tb2(id int,data varchar(20))INSERT tb VALUES(1,'中国');
INSERT tb VALUES(2,'liangck')INSERT tb2 VALUES(1,'China')
GOCREATE PROCEDURE dbo.FindString
@string NVARCHAR(100)
AS DECLARE @SQL NVARCHAR(4000);
SET @SQL = N'
DECLARE @str NVARCHAR(4000);
SELECT
@str = ISNULL(@str + N'' OR '' + c.name + N'' LIKE N''''%'
+ @string + ' %'''''',
c.name + N'' LIKE N''''%' + @string +'%'''''') FROM syscolumns AS c JOIN systypes AS t ON c.id=OBJECT_ID(''?'')
AND c.xtype=t.xtype
AND t.name IN(''varchar'',''char'',''nvarchar'',''nchar''); SET @str = ''SELECT TOP 1 1 FROM ? WHERE ''+@str;
CREATE TABLE #tb(a int);
INSERT #tb(a) EXEC(@str);
IF EXISTS(SELECT * FROM #tb)
PRINT ''?''
';
EXEC sp_MsforeachTable @SQL;
GOEXEC dbo.FindString N'中国'GO
DROP PROCEDURE dbo.FindString
DROP TABLE tb,tb2