如何查一个表中所有字段是否包含某个关键词
如何查一个表中所有字段是否包含某个关键词
------解决方案--------------------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
------解决方案--------------------你把楼上的方法改改不就好了     
 写简单点需要两个参数,一个表名 一个关键字,你可以从前台程序传过来 
 declare @key varchar(50) 
 declare @tableName varchar(50)   
 select name from syscolumns where id = OBJECT_ID(@tableName) and name = @key   
 执行这个查询语句,你去接返回的结果集,如果有就是包含,没有就是不包含了 
------解决方案--------------------是你没说清楚 
 create table abc(a int,b bit,c numeric(8,2),d float,e datetime,f char(10),g nvarchar(10)) 
 insert abc select 123,1,123.45,1/3,getdate(), 'b12ab ', 'e512abc ' 
 insert abc select null,null,null,1/3,null, 'b12ab ', 'e512abc ' 
 ---------------------------- 
 declare @sql varchar(1000),@key varchar(50),@tb varchar(20) 
 select @sql= ' ',@key= '12 ',@tb= 'abc '---@tb表名,@key 关键 
 select @sql=@sql+ 'or charindex( ' ' '+@key+ ' ' ',rtrim( '+a.name+ '))> 0  '  
    from syscolumns a,systypes b where id=object_id( 'abc ')  
    and a.xtype=b.xusertype  
    and b.name in( 'char ', 'varchar ', 'nchar ', 'nchar ', 'int ', 'numeric ', 'float ') 
 ---这个and 在这里可以不要,你可以调整in中的内容 
 select @sql= 'select * from  '+@tb+ ' where  '+stuff(@sql,1,2, ' ') 
 exec (@sql) 
 ----------------------------- 
 123	1	123.45	0.0	2007-06-13 22:31:28.850	b12ab     	e512abc 
 NULL	NULL	NULL	0.0	NULL	b12ab     	e512abc