473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2620

"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 search baker search charlie " into 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 it... I've created a database that is going to be...
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* search_and_replace(char* source,char search,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) or would you have to do something like: if...
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 "moreinfo" in the in-memory datatable (but not the...
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 of my search application. Lots of code is at the...
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 table using multiple field input from a form and...
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 more in the future. Therefore id like a nice...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.