根据字段值,查找该值在该表的哪个字段?

你会修电脑吗 2014-08-22 05:21:55
表名已知,
字段内容知道一部分

如何查询,改内容在该表哪个字段?
...全文
121 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZxhLIIT 2014-08-22
  • 打赏
  • 举报
回复
if object_id('search_db') is not null drop proc search_dbgocreate proc search_db @table nvarchar(100), @cond nvarchar(512)asdeclare hCForEach cursor global forselect sqlstmt ='if exists (' + stmt + ') print ''' + replace(stmt, '''', '''''') + ''''from( select stmt='select * from [' + TABLE_NAME + '] where convert(nvarchar, [' + COLUMN_NAME + ']) like ''' + @cond + '''' from INFORMATION_SCHEMA.COLUMNS A where (IsNull(@table, '') = '' or TABLE_NAME like @table) and DATA_TYPE <> 'image' and (SELECT TABLE_TYPE from INFORMATION_SCHEMA.TABLES B where A.TABLE_NAME = B.TABLE_NAME) = 'BASE TABLE') Texec sp_msforeach_worker @command1 = N'?'goexec search_db '', '%a1%'
發糞塗牆 2014-08-22
  • 打赏
  • 举报
回复
你可以自己改啊
你会修电脑吗 2014-08-22
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
--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'
我已经知道表名了,这样会不会有点麻烦啊?
發糞塗牆 2014-08-22
  • 打赏
  • 举报
回复
--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'

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧