Syntax check all stored procedures?

前端 未结 9 1950
情话喂你
情话喂你 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 23:07

    There is no way to do it from T-SQL, or Enterprise Manager, so i had to write something from client code. i won't post all the code here, but the trick is to:

    1) Get a list of all stored procedures

     SELECT ROUTINE_NAME AS StoredProcedureName
     FROM INFORMATION_SCHEMA.ROUTINES
     WHERE ROUTINE_TYPE = 'PROCEDURE' --as opposed to a function
     ORDER BY ROUTINE_NAME
    

    2) Get the stored procedure create T-SQL:

    select
       c.text
    from dbo.syscomments c
    where c.id = object_id(N'StoredProcedureName')
    order by c.number, c.colid
    option(robust plan)
    

    3) Run the create statement with NOEXEC on, so that the syntax is checked, but it doesn't actually try to create the stored procedure:

    connection("SET NOEXEC ON", ExecuteNoRecords);
    connection(StoredProcedureCreateSQL, ExecuteNoRecords);
    connection("SET NOEXEC ON", ExecuteNoRecords);
    

提交回复
热议问题