Re: How to genericaly search the entire db for any text string in any stored procedure, function, table definition, agent job, etc. Any one command or anyone have a stored proc that does this wide search?



On Dec 5, 10:36 pm, "DR" <softwareengineer98...@xxxxxxxxx> wrote:
How to genericaly search the entire db for any text string in any stored
procedure, function, table definition, agent job, etc. Any one command or
anyone have a stored proc that does this wide search?

Hmmm... You really do NOT want to do that, this will be a big load and
take a while; assuming you have a sizable DB. That said here you go,
give it a try;

This script will quickly generate DML to search all the tables in the
current database for a given string. It could be modified to execute
the generated code if desired. If you are running this in a <2005
environment, convert the VARCHAR(MAX)s to VARCHAR(8000) and be careful
of overflowing the variable if you have very wide tables. To use this
script, simply set the value of @SearchItem to whatever string it is
you are searching for on line 2 then run in the database you wish to
search.



DECLARE @searchSQL AS VARCHAR(MAX)
DECLARE @SearchItem AS VARCHAR(MAX) ; SET @SearchItem = '%YOURSTRING%'

SELECT @searchSQL = COALESCE(@searchSQL + '+ CAST(COALESCE(MIN(CASE
WHEN IC.Ordinal_position = ' +
CAST(Ordinal_position AS VARCHAR(MAX)) +
' THEN '' OR '' + IC.Column_Name + '' LIKE ''''' +
@SearchItem + ''''''' END), '''') AS NVARCHAR(max))' +
CHAR(13), 'CAST(COALESCE(MIN(CASE WHEN IC.Ordinal_position = ' +
CAST(Ordinal_position AS VARCHAR(MAX)) +
' THEN IC.Column_Name + '' LIKE ''''' + @SearchItem +
''''''' END), '''') AS NVARCHAR(max))' + CHAR(13))
FROM INFORMATION_SCHEMA.Columns
GROUP BY Ordinal_Position
ORDER BY Ordinal_Position

EXEC('SELECT REPLACE(Query, ''WHERE OR'', ''WHERE'') FROM
(SELECT ''SELECT '''''' + CAST(TABLE_NAME AS VARCHAR(MAX)) + '''''' as
tbl, * FROM ['' +
CAST(TABLE_SCHEMA AS VARCHAR(MAX)) + ''].'' + CAST(TABLE_NAME AS
VARCHAR(MAX)) + '' WHERE '' + ' + @searchSQL + ' AS query
FROM INFORMATION_SCHEMA.Columns IC
WHERE DATA_TYPE IN (''char'', ''varchar'', ''nchar'', ''nvarchar'')
GROUP BY TABLE_NAME, TABLE_SCHEMA) S')

.



Relevant Pages

  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Renaming variables using strings
    ... I have a big database with several variables inside. ... i have a variable in database called F101 and the script ... makes a string 'F101'. ... I would like to rename the variable F101 as ...
    (comp.soft-sys.matlab)
  • Re: Renaming variables using strings
    ... > I have a big database with several variables inside. ... i have a variable in database called F101 and the script ... > makes a string 'F101'. ... I would like to rename the variable F101 as ...
    (comp.soft-sys.matlab)
  • Re: POST Params - getting unknown
    ... I want to modify my script to save the actual POST url into ... > the database so I can see all of the POST parameter names. ... echo 'POST variables in string: ...
    (comp.lang.php)
  • Re: A little Rolodex [revised]
    ... including alpha sort and searching for any embedded string, ... In this application, a database is a directory, ... you may optionally provide any alternate UCASE program, ... NN -> first store NN as key length ...
    (comp.sys.hp48)

Loading