如何查一个表中所有字段是否包含某个关键词

本文介绍了一种使用T-SQL在SQL Server中进行数据检索的方法,通过定义游标来遍历表中的特定类型列,并检查这些列是否包含预先设定的关键字。此过程能够帮助开发者快速定位到含有特定字符串的数据行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

declare @n varchar(30)
declare @key varchar(20)
set @key='关键字'
declare cur cursor for
select a.name from syscolumns a Inner Join systypes b on a.id=b.id where id=object_id('表') and b.name in('varchar','char','nvarchar','nchar','sysname')
fetch next from cur into @n
while @@fetch_status=0
begin
if exists(select 1 from [表] where charindex(@key,@n)>0)
   print @n +'中存在!'
fetch next from cur into @n
end
close cur
deallocate