472,328 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Search All Tables and Replace

I'm looking for a stored procedure (or query) to search an entire
database for a specific string value and replace it with another. I'm
sure I saw an SP for this a while back by someone, but cannot find it
again. The SP took the search string and replace string as parameters
and did the rest. Any ideas where I can find this ?

Bear in mind, the idea is that this can be re-used and run on any
database, so it would have to find all tables and search through those.
Ta

Ryan

Jul 23 '05 #1
2 2539

"Ryan" <ry********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm looking for a stored procedure (or query) to search an entire
database for a specific string value and replace it with another. I'm
sure I saw an SP for this a while back by someone, but cannot find it
again. The SP took the search string and replace string as parameters
and did the rest. Any ideas where I can find this ?

Bear in mind, the idea is that this can be re-used and run on any
database, so it would have to find all tables and search through those.
Ta

Ryan


Google found this:

http://groups.google.ch/groups?hl=en...jomn%404ax.com

You should probably read the whole thread - there are some comments and
corrections to the original code in later posts.

Simon
Jul 23 '05 #2
"Ryan" <ry********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm looking for a stored procedure (or query) to search an entire
database for a specific string value and replace it with another. I'm
sure I saw an SP for this a while back by someone, but cannot find it
again. The SP took the search string and replace string as parameters
and did the rest. Any ideas where I can find this ?

Bear in mind, the idea is that this can be re-used and run on any
database, so it would have to find all tables and search through those.
Ta

Ryan


You might find this helpful. Here's a UDF that will generate SQL code that,
when executed, will search each character string column (defined for CHARs
and VARCHARs) of sufficient length in each table of the database and
return those columns that have an occurrence of the indicated string.

Say we have the following tables:

CREATE TABLE T1
(
c1 VARCHAR(10) NOT NULL
)

INSERT INTO T1 (c1)
VALUES ('hello')
INSERT INTO T1 (c1)
VALUES ('goodbye')

CREATE TABLE T2
(
c2 VARCHAR(4) NOT NULL
)

INSERT INTO T2 (c2)
VALUES ('1')
INSERT INTO T2 (c2)
VALUES ('2')

CREATE FUNCTION GenerateCodeForStringColumnCheck
(@string VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @code VARCHAR(8000)
SET @code = ''
SELECT @code =
@code +
'SELECT ''"' + TABLE_SCHEMA + '"'' AS table_schema, ''"' +
TABLE_NAME + '"'' AS table_name, ''"' +
COLUMN_NAME + '"'' AS column_name' +
' WHERE EXISTS (SELECT * FROM "' +
TABLE_SCHEMA + '"."' + TABLE_NAME + '" WHERE "' +
COLUMN_NAME + '" = ''' + @string + ''') UNION ALL '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE CHARACTER_MAXIMUM_LENGTH >= LEN(@string) AND
DATA_TYPE IN ('char', 'varchar') AND
TABLE_SCHEMA = 'dbo'
RETURN(SUBSTRING(@code, 1, LEN(@code) - LEN('UNION ALL ')))
END

DECLARE @code VARCHAR(8000)
DECLARE @searchString VARCHAR(10)
SET @searchString = 'hello'
SET @code = dbo.GenerateCodeForStringColumnCheck(@searchString )
EXEC(@code)

table_schema table_name column_name
"dbo" "T1" "c1"

--
JAG
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able...
3
by: Stu | last post by:
Hi, I've been looking through the archives but can't find what I'm looking for, or due to my limited experience with Access97, didn't recognize...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char*...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) ...
2
by: JP SIngh | last post by:
Hi All A tricky issue wonder if someone can help I have two tables like this TapeRecords TapeID - Integer Primary key Title - Text
2
by: TB | last post by:
Before displaying the result of a table called "people" in a datagrid called "mydatagrid", I need to modify the content of a column called...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a...
6
by: simon.robin.jackson | last post by:
Ok. I need to develop a macro/vba code to do the following. There are at least 300 corrections and its expected for this to happen a lot...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.