查找某数据库中的列是否存在某个值 alter 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
只要是跟我输入的值有相符合的就可以,我写了一个不过查询出来的都是数据集而不是一个表 DECLARE @sqlVal nvarchar(800) SET @sqlVal=N'100' DECLARE MyTable CURSOR LOCAL FOR SELECT ('SELECT '+c.NAME+' FROM ['+o.name+'] where '+c.name+' LIKE ''%'+@sqlVal+'%''') FROM sys.syscolumns c JOIN sys.objects o ON c.id=o.object_id JOIN sys.schemas s ON s.schema_id=o.schema_id WHERE c.status>0 AND c.xusertype IN(167,175, 239, 231)and o.name='table' OPEN MyTable FETCH NEXT FROM MyTable INTO @sqlVal WHILE @@fetch_status=0 begin EXEC (@sqlVal) FETCH NEXT FROM MyTable INTO @sqlVal END CLOSE MyTable DEALLOCATE MyTable
alter 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
DECLARE @sqlVal nvarchar(800)
SET @sqlVal=N'100'
DECLARE MyTable CURSOR LOCAL FOR SELECT
('SELECT '+c.NAME+' FROM ['+o.name+'] where '+c.name+' LIKE ''%'+@sqlVal+'%''')
FROM sys.syscolumns c JOIN sys.objects o ON c.id=o.object_id JOIN sys.schemas s ON s.schema_id=o.schema_id
WHERE c.status>0 AND c.xusertype IN(167,175, 239, 231)and o.name='table'
OPEN MyTable FETCH NEXT FROM MyTable INTO @sqlVal
WHILE @@fetch_status=0
begin EXEC (@sqlVal)
FETCH NEXT FROM MyTable INTO @sqlVal
END
CLOSE MyTable
DEALLOCATE MyTable
他提供的是整个数据库的查询,对于你的需要来说是扩大了搜索范围。不过值得借鉴