存储过程存档数据,16,1?

时间:2014-05-27 13:47:10

标签: sql

我有一些代码,我试图理解:

CREATE PROC dbo.ArchiveData
(
@CutOffDate datetime = NULL
)
AS
BEGIN
SET NOCOUNT ON

IF @CutOffDate IS NULL 
BEGIN
    SET @CutOffDate = DATEADD(mm, -6, CURRENT_TIMESTAMP)

END
ELSE
BEGIN
    IF @CutOffDate > DATEADD(mm, -3, CURRENT_TIMESTAMP)
    BEGIN
        RAISERROR ('Cannot delete orders from last three months', 16, 1)
        RETURN -1
    END
END

BEGIN TRAN

    INSERT INTO Archive.dbo.Orders
    SELECT * 
    FROM dbo.Orders
    WHERE OrderDate < @CutOffDate

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while copying data to Archive.dbo.Orders', 16, 1)
        RETURN -1
    END

    INSERT INTO Archive.dbo.OrderDetails
    SELECT *
    FROM dbo.OrderDetails
    WHERE OrderID IN
    (
        SELECT OrderID
        FROM dbo.Orders
        WHERE OrderDate < @CutOffDate
    )

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while copying data to Archive.dbo.OrderDetails', 16, 1)
        RETURN -1
    END

    DELETE dbo.OrderDetails
    WHERE OrderID IN
    (
        SELECT OrderID
        FROM dbo.Orders
        WHERE OrderDate < @CutOffDate
    )

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while deleting data from     dbo.OrderDetails', 16, 1)
        RETURN -1
    END

    DELETE dbo.Orders
    WHERE OrderDate < @CutOffDate

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while deleting data from dbo.Orders', 16,     1)
        RETURN -1
    END

IF @@TRANCOUNT > 0
BEGIN
    COMMIT TRAN
    RETURN 0
END

END

只是想知道16和o1,在这里看到:Error occured while deleting data from dbo.OrderDetails', 16, 1),是用于向日志报告错误,16是严重性,1是状态?如果确实如此,他们的意思是什么?

1 个答案:

答案 0 :(得分:0)

实际上,第二个参数(16)是严重性。

来自MSDN

  

用户定义的严重性级别是否与此消息相关联。 ...任何用户都可以指定从0到18的严重级别。

第三个参数(1)是状态。

  

如果在多个位置引发了相同的用户定义错误,则为每个位置使用唯一的状态编号可以帮助查找哪个代码段引发错误。