How can I programmatically check (parse) the validity of a TSQL statement?

后端 未结 7 491
礼貌的吻别
礼貌的吻别 2020-12-04 10:25

I\'m trying to make my integration tests more idempotent. One idea was to execute rollback after every test, the other idea was to some how programatically parse the text,

7条回答
  •  攒了一身酷
    2020-12-04 10:57

    SQL Server 2012 can parse your syntax, procedures and tables with the following system procedures and functions:

    • sp_describe_first_result_set (Transact-SQL)
    • sp_describe_undeclared_parameters (Transact-SQL)
    • sys.dm_exec_describe_first_result_set (Transact-SQL)
    • sys.dm_exec_describe_first_result_set_for_object (Transact-SQL)

    They are supposedly replacing "SET FMTONLY".

    I have tested them and they work a lot better than "SET NOEXEC ON" and "SET PARSEONLY ON"

    Examples:

    Will not throw an error:

    sp_describe_undeclared_parameters
        @tsql = N'SELECT object_id, name, type_desc FROM sys.indexes;'
    

    Will correctly throw an error ("SET NOEXEC" and "SET PARSEONLY" do not throw an error in this case):

    sp_describe_undeclared_parameters 
      @tsql = N'SELECT object_id, name, type_desc FROM sys.indexes;SELECT object_id, name, type_desc FROM sys.NOTaTABLE;'
    

提交回复
热议问题