Se qualcuno dovesse richiedere qualcosa come questo per Sybase, quanto segue potrebbe aiutare.
Ho creato il seguente script, in cui il codice stampa tutti i TableNames, ColumnNames contenenti la stringa di ricerca.
Non prestazioni ottimizzate, utilizzando un cursore per scorrere le colonne DB, quindi potrebbe richiedere tempo eseguire questo su un grande DB (a seconda delle dimensioni, numero di tabelle/colli ecc)
Tuttavia, Penso che sia una buona utility per cercare una stringa in un DB.
-----------------------------------------------------------------------------------------------------
-- SYBASE - SCRIPT TO FIND STRING IN ANY COLUMN IN TABLE AND PRINT TableName/ColumnName TO RESULTS --
-----------------------------------------------------------------------------------------------------
-- tested on Sybase ASE 15.7
set nocount off
-- CREATE OBJECTS REQUIRED FOR SCRIPT
create table #SearchString (SearchString varchar(100))
go
-- SET SEARCH STRING
declare @search_string varchar(100)
set @search_string = 'SEARCH_STRING'
-- WRITE SEARCH STRING TO TEMP TABLE TO STORE IT AWAY AND BE ABLE TO READ IT IN NEXT BATCH
insert into #SearchString (SearchString)
values (@search_string)
-- GET ALL RELEVANT TABLES AND COLUMNS
insert #TabCol
select object_name(o.id) as TableName, c.name as ColumnName
from sysobjects o, syscolumns c
where o.type = 'U' -- ONLY USER TABLES
and c.usertype in (1,2,18,19,24,25,42) -- ONLY LOOK FOR CHAR, VARCHAR, ETC.
and c.id = o.id
and c.name is not null
and c.length >= datalength(@search_string)
go
-- GET TOTAL NUMBER OF RELEVANT COLUMNS
select count(*) as RelevantColumns from #TabCol
go
-- CREATE CURSOR TO LOOP THROUGH TABLES AND COLUMNS TO FIND COLUMNS CONTAINING THE SEARCH STRING
declare cur cursor for
select TableName, ColumnName from #TabCol order by TableName, ColumnName
for read only
go
-- VARIABLE DEFINITION
declare
@table_name SYSNAME,
@table_id int,
@column_name SYSNAME,
@sql_string varchar(2000),
@search_string varchar(100)
-- GET SEARCH STRING FROM TABLE
select @search_string = SearchString from #SearchString
-- CURSOR INIT
open cur
fetch cur into @table_name, @column_name
-- LOOP THROUGH TABLES AND COLUMNS SEARCHING FOR SEARCH STRING AND PRINT IF FOUND
while (@@sqlstatus != 2)
begin
set @sql_string = 'if exists (select * from ' + @table_name + ' where [' + @column_name + '] like ''%' + @search_string + '%'') print ''' + @table_name + ', ' + @column_name + ''''
execute(@sql_string)
fetch cur into @table_name, @column_name
end
go
-- CLEAN-UP
close cur
deallocate cur
drop table #SearchString
drop table #TabCol
go
Acclamazioni
+1 Bella risposta breve, grazie per averci esaminato. Aspetterò e vedrò se uno dei due verrà upvotato di più prima di selezionarne uno –
+1 Forse aggiungere (NOLOCK) o "impostare il livello di isolamento della transazione non eseguito" se si prevede di eseguirlo su un database di produzione – Andomar
cosa aggiungerebbe NOLOCK? Penserei che sys.objects non venga aggiornato spesso. – edosoft