• <i id='JFPwN'><tr id='JFPwN'><dt id='JFPwN'><q id='JFPwN'><span id='JFPwN'><b id='JFPwN'><form id='JFPwN'><ins id='JFPwN'></ins><ul id='JFPwN'></ul><sub id='JFPwN'></sub></form><legend id='JFPwN'></legend><bdo id='JFPwN'><pre id='JFPwN'><center id='JFPwN'></center></pre></bdo></b><th id='JFPwN'></th></span></q></dt></tr></i><div id='JFPwN'><tfoot id='JFPwN'></tfoot><dl id='JFPwN'><fieldset id='JFPwN'></fieldset></dl></div>

      <bdo id='JFPwN'></bdo><ul id='JFPwN'></ul>
    <legend id='JFPwN'><style id='JFPwN'><dir id='JFPwN'><q id='JFPwN'></q></dir></style></legend>
  • <small id='JFPwN'></small><noframes id='JFPwN'>

  • <tfoot id='JFPwN'></tfoot>

        SQL Server 存储过程返回码奇怪

        时间:2023-10-26

          <legend id='QTToQ'><style id='QTToQ'><dir id='QTToQ'><q id='QTToQ'></q></dir></style></legend>
            <i id='QTToQ'><tr id='QTToQ'><dt id='QTToQ'><q id='QTToQ'><span id='QTToQ'><b id='QTToQ'><form id='QTToQ'><ins id='QTToQ'></ins><ul id='QTToQ'></ul><sub id='QTToQ'></sub></form><legend id='QTToQ'></legend><bdo id='QTToQ'><pre id='QTToQ'><center id='QTToQ'></center></pre></bdo></b><th id='QTToQ'></th></span></q></dt></tr></i><div id='QTToQ'><tfoot id='QTToQ'></tfoot><dl id='QTToQ'><fieldset id='QTToQ'></fieldset></dl></div>
              1. <tfoot id='QTToQ'></tfoot>

                <small id='QTToQ'></small><noframes id='QTToQ'>

                  <bdo id='QTToQ'></bdo><ul id='QTToQ'></ul>
                    <tbody id='QTToQ'></tbody>
                  本文介绍了SQL Server 存储过程返回码奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  调用此代码的客户端受到限制,只能处理来自存储过程的返回代码.因此,我们将通常的合约修改为 RETURN -1 出错,default 修改为 RETURN 0 如果没有错误

                  The client that calls this code is restricted and can only deal with return codes from stored procs. So, we modified our usual contract to RETURN -1 on error and default to RETURN 0 if no error

                  如果代码命中内部 catch 块,则返回代码默认为 -4 而不是 0

                  If the code hits the inner catch block, then the RETURN code default is -4 rather then 0

                  请问有大佬知道出处吗?参考

                  Does anyone know where this comes from please? With reference

                  干杯英镑

                  IF OBJECT_ID('dbo.foo') IS NOT NULL DROP TABLE dbo.foo
                  GO
                  CREATE TABLE dbo.foo (
                      KeyCol  char(12) NOT NULL,
                      ValueCol xml NOT NULL,
                      Comment varchar(1000) NULL,
                      CONSTRAINT PK_foo PRIMARY KEY CLUSTERED (KeyCol)
                  )
                  GO
                  
                  IF OBJECT_ID('dbo.bar') IS NOT NULL DROP PROCEDURE dbo.bar
                  GO
                  CREATE PROCEDURE dbo.bar
                      @Key char(12),
                      @Value xml,
                      @Comment varchar(1000)
                  AS
                  SET NOCOUNT ON
                  DECLARE @StartTranCount tinyint;
                  BEGIN TRY
                      SELECT @StartTranCount = @@TRANCOUNT;
                  
                      IF @StartTranCount = 0 BEGIN TRAN;
                  
                      BEGIN TRY
                          --SELECT @StartTranCount = 'fish' --generates an error and goes to outer CATCH
                          INSERT dbo.foo (KeyCol, ValueCol, Comment) VALUES (@Key, @Value, @Comment);
                      END TRY
                      BEGIN CATCH
                          IF ERROR_NUMBER() = 2627    --PK violation
                              UPDATE
                                  dbo.foo
                              SET
                                  ValueCol = @Value, Comment = @Comment
                              WHERE
                                  KeyCol = @Key;
                          ELSE
                              RAISERROR ('Tits up', 16, 1);
                      END CATCH
                  
                      IF @StartTranCount = 0 COMMIT TRAN;
                  END TRY
                  BEGIN CATCH
                      IF @StartTranCount = 0 AND XACT_STATE() <> 0 ROLLBACK TRAN;
                      RETURN -1
                  END CATCH
                  --Without this, we'll send -4 if we hit the UPDATE CATCH block above
                  --RETURN 0
                  GO
                  
                  --please run these **separately**
                  
                  --Run with RETURN 0 and fish line commented out
                  DECLARE @rtn int
                  EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing'
                  SELECT @rtn; SELECT * FROM dbo.foo
                  GO
                  
                  DECLARE @rtn int
                  EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar2 />', 'testing2'
                  --updated OK but we get @rtn = -4
                  SELECT @rtn; SELECT * FROM dbo.foo
                  GO
                  
                  --uncomment fish line
                  DECLARE @rtn int
                  EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing'
                  --Hit outer CATCH, @rtn = -1 as expected
                  SELECT @rtn; SELECT * FROM dbo.foo
                  

                  推荐答案

                  在玩弄这个过程时,如果我在 foo.KeyCol 中插入一个 null 并删除内部 catch 中的 RAISERROR,我可以得到一个返回值 -6.这是 SQL Server 正在做的事情,并记录在此处:Return Values from Stored Procedures.

                  In playing around with the procedure, I can get a a return -6, if I insert a null into foo.KeyCol and remove the RAISERROR in the inner catch. This is something SQL Server is doing, and is documented here: Return Values from Stored Procedures.

                  这篇关于SQL Server 存储过程返回码奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何声明弱类型 SYS_REFCURSOR 变量的 %ROWTYPE? 下一篇:如何在 SQL Server 程序/触发器中查找文本?

                  相关文章

                    <small id='ueQwZ'></small><noframes id='ueQwZ'>

                  1. <tfoot id='ueQwZ'></tfoot>
                      <bdo id='ueQwZ'></bdo><ul id='ueQwZ'></ul>

                    <legend id='ueQwZ'><style id='ueQwZ'><dir id='ueQwZ'><q id='ueQwZ'></q></dir></style></legend>
                  2. <i id='ueQwZ'><tr id='ueQwZ'><dt id='ueQwZ'><q id='ueQwZ'><span id='ueQwZ'><b id='ueQwZ'><form id='ueQwZ'><ins id='ueQwZ'></ins><ul id='ueQwZ'></ul><sub id='ueQwZ'></sub></form><legend id='ueQwZ'></legend><bdo id='ueQwZ'><pre id='ueQwZ'><center id='ueQwZ'></center></pre></bdo></b><th id='ueQwZ'></th></span></q></dt></tr></i><div id='ueQwZ'><tfoot id='ueQwZ'></tfoot><dl id='ueQwZ'><fieldset id='ueQwZ'></fieldset></dl></div>