Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest/Easiest way to use Search/Replace through all stored procedures

Actually, this is a 2 part question.

  1. Is it possible to use some sort of functionality to search through every stored procedure for a string and possibly replace it, like a standard Find/Replace function?

  2. If you have all your stored procedure code include the full database path like this [db1].[dbo].[table1] and you change the database name to [db2] is there a way for SQL Server to automatically update all the code from [db1] tables to [db2]? Or does it have to be done manually?

like image 243
Control Freak Avatar asked Apr 22 '12 15:04

Control Freak


People also ask

Are stored procedures faster than queries?

Conclusion. Overall, stored procedures outperform dynamic SQL. They are faster, easier to maintain, and require less network traffic. The rule of thumb would suggest using stored procedures in scenarios where you don't have to modify queries, and those queries are not very complex.


3 Answers

From the Object Explorer Details window in SSMS, open the stored procedures folder. Select all the objects (you can multi-select from this window, which is pretty much the only purpose of the Object Explorer Details window) and right click, choosing to script as DROP and CREATE. You can now do a search/replace on this, replacing all you need in one go before executing it.

Edit: I've blogged about this solution.

like image 67
David Atkinson Avatar answered Oct 24 '22 03:10

David Atkinson


Late one but hopefully useful.

There is a free search tool from ApexSQL that can find and rename objects in database.

They say it has a smart rename option that will find/replace all occurrences of some object such as table, function or stored procedure.

I have to add that I haven’t used the rename functionality but I can confirm that search is working quite well.

Also I’m not affiliated with ApexSQL but I do use their tools.

like image 22
James Martin Avatar answered Oct 24 '22 04:10

James Martin


To search: if you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).

enter image description here

enter image description here

It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use?

This tool doesn't support replacing text, however - but even just being able to find all the relevant stored procedures (or other DB objects) is very helpful indeed!

like image 14
marc_s Avatar answered Oct 24 '22 03:10

marc_s