Link to home
Start Free TrialLog in
Avatar of weinerk
weinerk

asked on

"grep" through SQL DB?

How can I "grep" through SQL DB?
Meaning looking for a string of data in one of the tables in one of the fields but not sure which ones.
One way to export the whole DB as a bunch of inserts and use grep.
But I am looking for a SQL code sort of like sp_grep,
but which will search through the actual data in DB.

Avatar of devsolns
devsolns

Not sure exactly what you are looking for......if you are searching a column for a specific string you could do something like:

SELECT mycol
FROM mytable

WHERE mycol like '%grep%'
hmm not sure whether there is a facility like that.. but if you wish to create the queries automagically for this kind of a job then you can do this., are you trying to reverse engineer from an thrid party application DB :)

select 'select * from ' + sysobjects.name + ' where ' + syscolumns.name +' like ''%stringtosearch%'' '
from syscolumns , sysobjects, systypes
where sysobjects.xtype='U' and sysobjects.id = syscolumns.id  and systypes.xtype=syscolumns.xtype
and systypes.name <> 'sysname'
and systypes.name  in ('varchar','char','nvarchar','nchar','text','text')
order by sysobjects.name,syscolumns.colid
Avatar of Guy Hengel [angelIII / a3]
IMHO, you should explain the reason why you try to "grep" the whole database. Usually, only a certain set of tables and columns should be searched for, anything else will be - if used/usable by users - a good performance and security killer.

You might create a stored procedure that explicitely searches this given set of tables and columns and returns the data to the user/application.
Avatar of weinerk

ASKER

So far user-dsine
is the closest to what I wanted.
Idealy would be nice if this is all wraped up together
in a storproc or a piece of code
to loop through all the statements and add descriptive output
so it will be obvious which tables/fields the results were found.

ASKER CERTIFIED SOLUTION
Avatar of dsine
dsine
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial