English 中文(简体)
页: 1 服务器存储程序返回代码奇数
原标题:SQL Server stored procedure return code oddity

要求使用该守则的客户受到限制,只能处理储存的代用品的返回代码。 因此,我们修改了我们关于错误和default<>>>至的通常合同,如果无错误,则将予以修改。

如果该守则击中了内层鱼群,那么ReTURN代码的缺省为4,而后为0。

是否有任何人知道这来自什么? 参考

Cheers gbn

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
最佳回答

在进行程序时,我可以回去——6,如果我把一个废墟插入.。 KeyCol andmov the RAISERROR in the inner Collection. 这是服务器正在做的,在此有文件记载:。 储存程序的收益价值

问题回答

暂无回答




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...