Syntax check all stored procedures?

前端 未结 9 1947
情话喂你
情话喂你 2020-12-04 22:51

i want to ensure that all stored procedures are still syntactically valid. (This can happen if someone renames/deletes a table/column).

Right now my solution to chec

9条回答
  •  时光取名叫无心
    2020-12-04 22:55

    A bit of a drawn-out option:

    1. Create a copy of the database (backup and restore). You could do this on the target database, if your confidence level is high.
    2. Use SSMS to script out all the stored procedures into a single script file
    3. DROP all the procedures
    4. Run the script to recreate them. Any that can't be created will error out.

    Couple of fussy gotchas in here, such as:

    • You want to have the "if proc exists then drop proc GO create proc ... GO" syntax to separte each procedure.
    • Nested procedures will fail if they call a proc that has not yet been (re)created. Running the script several times should catch that (since ordering them properly can be a real pain).
    • Other and more obscure issues might crop up, so be wary.

    To quickly drop 10 or 1000 procedures, run

    SELECT 'DROP PROCEDURE ' + schema_name(schema_id) + '.' +  name
     from sys.procedures
    

    select the output, and run it.

    This assumes you're doing a very infrequent task. If you have to do this regularly (daily, weekly...), please let us know why!

提交回复
热议问题