SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find and Replace a value across all tables in database


Find and Replace a value across all tables in database

Author
Message
Devendra Kumar Thakur
Devendra Kumar Thakur
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1238 Visits: 88
Comments posted to this topic are about the item Find and Replace a value across all tables in database
jimmy.white 20227
jimmy.white 20227
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 31
Nice code.

Be good to point out that it only works only on complete column values not partial.

Also I have one enhancement.
It didn't work on one of my tables and I found out it was because the column Name was "To" for email values.
Change the EXEC line in the code to this:

EXEC ( 'Update '+@TableName+ ' Set ['+ @ColumnName+'] = '+''''+@ReplaceWith+''''+ ' Where ['+@ColumnName+'] = '+''''+@SearchThis+'''')

And it will work!
Devendra Kumar Thakur
Devendra Kumar Thakur
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1238 Visits: 88
jimmy.white 20227 - Friday, April 28, 2017 9:45 AM
Nice code.

Be good to point out that it only works only on complete column values not partial.

Also I have one enhancement.
It didn't work on one of my tables and I found out it was because the column Name was "To" for email values.
Change the EXEC line in the code to this:

EXEC ( 'Update '+@TableName+ ' Set ['+ @ColumnName+'] = '+''''+@ReplaceWith+''''+ ' Where ['+@ColumnName+'] = '+''''+@SearchThis+'''')

And it will work!

Thanks for pointing​ out.

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203432 Visits: 41949
With absolutely no disrespect to the author, I cringe a bit when I see things like this. It generally means that the database wasn't properly designed to begin with. If it were, you'd likely only need to make a change in one column in a single row in a single table.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Perry Whittle
Perry Whittle
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50662 Visits: 17653
Time to re visit the database design, normalisation wouldn't go amiss here.

The script itself uses 4 variables of 256 each but the dynamic sql variable is only 999 and mixing non Unicode and Unicode values!!

Do yourself a favour and don't run the script, re design your database instead

-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43973 Visits: 10834
After the loop, the first thing I noticed was the data type mismatch. That found not be fun.

That said, what Jeff and Perry said holds true. All that duplicated data means it's time to invest in a redesign.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Devendra Kumar Thakur
Devendra Kumar Thakur
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1238 Visits: 88
Jeff Moden - Saturday, April 29, 2017 4:03 PM
With absolutely no disrespect to the author, I cringe a bit when I see things like this. It generally means that the database wasn't properly designed to begin with. If it were, you'd likely only need to make a change in one column in a single row in a single table.

Thanks for your thoughts but we don't live in an ideal world. I joined the project during maintenance and enhancement phase, so I have to fix problems​.

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203432 Visits: 41949
Devendra Thakur - Sunday, April 30, 2017 10:51 PM
Jeff Moden - Saturday, April 29, 2017 4:03 PM
With absolutely no disrespect to the author, I cringe a bit when I see things like this. It generally means that the database wasn't properly designed to begin with. If it were, you'd likely only need to make a change in one column in a single row in a single table.

Thanks for your thoughts but we don't live in an ideal world. I joined the project during maintenance and enhancement phase, so I have to fix problems​.


Yup. That's why I said "With absolutely no disrespect to the author". This reminded me of the (now 3) times I've had to go through 600 or so tables to find all combinations of a ClientID and LoanNumber to change all of the loan numbers for a given client. Not only did they NOT use the LoanID that's contained in the loan table but they spelled "ClientID" and LoanNumber each a half dozen different ways, used leading zeros on some and not on other columns, used dashes on some and not other columns, and had 2 different meanings to "ClientID" types of columns and 3 different meanings for "LoanNumber" types of columns. What should have been a 1 minute change turned into a week's worth of research and tweaking even after I'd written the discovery code.

And, just like the people that wrote your less (whole lot less) than ideal database, it's actually too late to do anything about it (which would actually have some serious performance and DRI benefits, as well) because it would require a total rewrite of all GUI code, Batch Code, and even simple C.R.U.D... all because someone decided to not get someone who knew about databases until they were in the "maintenance and enhancement" phase. Oddly enough, if they had done it correctly to begin with, they wouldn't need much of such a phase.

At that's why I cringe every time I see scripts that need to do what you've necessarily had to do. Again, none of my cringing was directed at you. Your good article/code serves as, yet again, another good example of why treating the database as "just a place to store data" as an "after thought" is absolutely the wrong thing to do. It's more proof positive of the adage "If you want it real bad... that's the way you'll get it". Wink

Thanks for taking the time to write the article and share your code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search