Записи по mssql,oracle,windows и linux

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Поиск по всем таблицам в базе MSSQL

set nocount on
declare @name varchar(128), @substr nvarchar(4000), @column varchar(128)
set @substr = '%-549139392%' --фрагмент строки, который будем искать

create table #rslt 
(table_name varchar(128), field_name varchar(128), value ntext)

declare s cursor for select table_name as table_name from information_schema.tables where table_type = 'BASE TABLE' order by table_name
open s
fetch next from s into @name
while @@fetch_status = 0
 declare c cursor for 
	select quotename(column_name) as column_name from information_schema.columns 
	  where data_type in ('text', 'ntext', 'varchar', 'char', 'nvarchar', 'char', 'sysname') and table_name  = @name
 set @name = quotename(@name)
 open c
 fetch next from c into @column
 while @@fetch_status = 0
   print 'Processing table - ' + @name + ', column - ' + @column
   exec('insert into #rslt select ''' + @name + ''' as Table_name, ''' + @column + ''', ' + @column + 
	' from' + @name + ' where ' + @column + ' like ''' + @substr + '''')
   fetch next from c into @column
 close c
 deallocate c
 fetch next from s into @name
select table_name as [Table Name], field_name as [Field Name], count(*) as [Found Mathes] from #rslt
group by table_name, field_name
order by table_name, field_name
--Если нужно, можем отобразить все найденные значения
--select * from #rslt order by table_name, field_name
drop table #rslt
close s
deallocate s


Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>