34,590
社区成员
发帖
与我相关
我的任务
分享
--drop table #
declare @t varchar(255),@c varchar(255)
--name是满足条件的表 cols是满足条件的列
create table # (name varchar(256),cols varchar(4000))
declare table_cursor cursor for
select a.name,b.name from sysobjects a,syscolumns b
where a.id=b.id and a.xtype='u'
open table_cursor fetch next from table_cursor
into @t,@c
while(@@fetch_status=0)
begin
exec('
set nocount on
if exists(select top 1 '+@c+' from [' + @t + '] where [' + @c + '] like ''%123%'')
begin
if not exists(select 1 from # where name='''+@t+''')
insert into # select '''+@t+''','''+@c+'''
else
update # set cols=cols+'','+@c+''' where name='''+@t+'''
--select '+@c+' from [' + @t + '] where [' + @c + '] like ''%123%''
end
')
fetch next from table_cursor into @t,@c
end
close table_cursor deallocate table_cursor;
select * from #
declare @Condition nvarchar(100)
set @Condition=123
declare T_cursor cursor local for
select top 100 ID,Name from sysobjects where xtype='U' --取一100個表
declare @object_id int,@TabName sysname,@SQL nvarchar(1000)
open T_cursor
fetch next from T_cursor into @object_id,@TabName
while @@fetch_status=0
begin
select @SQL=isnull(@SQL+' and ','')+'rtrim('+quotename(Name)+')='+quotename(@Condition,'''') from syscolumns where ID=@object_id and
xuserType in(select xuserType from systypes where Name in('tinyint','smallint','int','bigint','varchar','char','nvarchar','nchar'))
set @SQL=N' select Tab='''+@TabName+''',* from '+@TabName+' where '+@SQL
exec(@SQL)
fetch next from T_cursor into @object_id,@TabName
set @SQL=null
end
close T_cursor
deallocate T_cursor
declare @t varchar(255),@c varchar(255)
declare table_cursor cursor for
select a.name,b.name from sysobjects a,syscolumns b
where a.id=b.id and a.xtype='u'
open table_cursor fetch next from table_cursor
into @t,@c
while(@@fetch_status=0)
begin
exec(' if exists(select top 1 * from [' + @t + '] where [' + @c + '] like ''%123%'') select * from [' + @t + '] where [' + @c + '] like ''%123%''')
fetch next from table_cursor into @t,@c
end
close table_cursor deallocate table_cursor;