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

前端 未结 13 1076
萌比男神i
萌比男神i 2020-12-30 03:05

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 rep

相关标签:
13条回答
  • 2020-12-30 03:29

    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.

    0 讨论(0)
  • 2020-12-30 03:31

    Hmm, dropping and rebuilding all procedures worked, unfortunately it crashed the SQL server upon which the SCADA for a rather large factory relied.

    It saved a bit of effort editing them individually and the factory was only stalled til I rebooted the server.

    But exercise some caution methinks. I was fair crapping myself for a moment there.

    0 讨论(0)
  • 2020-12-30 03:41
    1. Export all SPs to file. Use your favourite text editing tool to search/replace. Update database by executing the script (as long as you do not rename procedures).

    2. If you explicitly define the full database path, you need to manually (see above) update the stored procedures. If you do not include the database name, or use a linked server or similar, no changes are necessary.

    0 讨论(0)
  • 2020-12-30 03:43

    Stored procedures cannot be updated in place without first scripting them out as ALTER PROCEDURE statements (or DROP/CREATE, but I prefer ALTER PROCEDURE..more on that in a moment). The good news is, you can script all the procedures to a single file through SSMS. The DDL statements will initially be CREATE PROCEDURE, which you'll want to replace with ALTER PROCEDURE, along with your other changes.

    While you could alternatively script the procedures as DROP/CREATE, I don't like doing this for a large number of scripts because it tends to cause dependency errors.

    As for part 2 of your question, you'll need to edit any database path changes manually through the script.

    0 讨论(0)
  • 2020-12-30 03:44

    There's no way to do this with built-in functionality. While it doesn't help you today, I'd suggest changing all of your references to synonyms while you're in there. That way, when this happens again in the future (and it will happen again), all of your external references are in one place and easily updated. Incidentally, I have a blog post on the latter.

    0 讨论(0)
  • 2020-12-30 03:45

    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!

    0 讨论(0)
提交回复
热议问题