Is there a way to have SQL server validate object references in Stored Procs?

后端 未结 7 2152
半阙折子戏
半阙折子戏 2021-01-13 20:12

The following code executes fine in SQL Server

create proc IamBrokenAndDontKnowIt as
select * from tablewhichdoesnotexist

Of course if I tr

7条回答
  •  醉话见心
    2021-01-13 20:53

    In SQL 2005 or higher, you can test a stored procedure using transactions and try/catch:

    BEGIN TRANSACTION
    
    BEGIN TRY
      EXEC (@storedproc)
      ROLLBACK TRANSACTION
    END TRY
    BEGIN CATCH
      WHILE @@TRANCOUNT > 0
        ROLLBACK
    END CATCH
    

    The algorithm to test all stored procedures in a database is a bit more complicated, as you need to workaround SSMS restrictions if you have many SPs which return many result sets. See my blog for complete solution.

提交回复
热议问题