SQLServer查询所有表所有字段包含xx的信息

从系统表自动生成sql语句来运行得到结果

rel="File-List" href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

select a.name as columnname,object_name(a.id)as tablename into t from syscolumns a,

sysobjects b,

systypes c

where a.id=b.id  

and a.xtype=c.xtype

and b.xtype='u'

and c.name in('varchar','nvarchar','char','nchar','text','ntext')

and object_name(a.id)<>'t'

 

go

 

create function udf_genSQL(@tableName varchar(1000),@keyword varchar(1000))

returns varchar(8000)

as

begin

    declare @sql varchar(8000)

    set @sql='select * from '+@tableName +' where 1=1 '

    select @sql=@sql+' or '+

       columnname +' like ''%'+@keyword+'%''' from t

    where tablename=@tablename

    return @sql

end

go

 

select dbo.udf_genSQL(tableName,'a') from t group by tablename

 

drop table t

drop function dbo.udf_genSQL

 

 

 

 


你可能感兴趣的:(SQLServer)