如何在存储过程中引发自定义错误

时间:2012-12-13 15:05:31

标签: sql-server stored-procedures

我想在引用键丢失时抛出错误,但是我没有通过完整性检查失败,而是想列出丢失的密钥。我创建了下面的工作。但是我希望有一种方法可以优化它并减少代码行数。

DECLARE @NonRefKeys INT

SELECT @NonRefKeys = SUM(1)
FROM staging.Sale sa
WHERE NOT EXISTS (
    SELECT cu.Customer_Shipping_ID
    FROM staging.Customer cu
    WHERE LTRIM(RTRIM(sa.Customer_Shipping_ID)) = LTRIM(RTRIM(cu.Customer_Shipping_ID)))

IF @NonRefKeys IS NOT NULL
BEGIN

IF OBJECT_ID('tempdb..#Missing_Ref') IS NOT NULL
DROP TABLE #Missing_Ref;

SELECT sa.Customer_Shipping_ID AS ID
INTO #Missing_Ref
FROM staging.Sale sa
WHERE NOT EXISTS (
        SELECT cu.Customer_Shipping_ID
        FROM staging.Customer cu
        WHERE LTRIM(RTRIM(sa.Customer_Shipping_ID)) = LTRIM(RTRIM(cu.Customer_Shipping_ID)))

DECLARE @Current_ID VARCHAR(50);
DECLARE @Missing_ID VARCHAR(MAX) = '';
DECLARE @Output_Error VARCHAR(MAX);

DECLARE id_cursor CURSOR FOR
SELECT ID
FROM #Missing_Ref;

OPEN id_cursor
FETCH NEXT FROM id_cursor INTO @Current_ID

WHILE @@FETCH_STATUS = 0
BEGIN
    IF (@Missing_ID != '')
        SET @Missing_ID = @Missing_ID + ', ';

    SET @Missing_ID = @Missing_ID + @Current_ID;

    FETCH NEXT FROM id_cursor INTO @Current_ID
END
CLOSE id_cursor
DEALLOCATE id_cursor

SET @Output_Error = 'ERROR: Key/s ' + @Missing_ID + ' for Customer Shipping ID missing from Customer table';

RAISERROR (@Output_Error,16,1)
END

1 个答案:

答案 0 :(得分:1)

检查一下......你可以更新变量而无需创建临时表

有一个尾随","你必须删除。

Declare @MissingIDList VarChar (Max) = ''

DROP Table #MyTableOfMissingIds

Select *
Into #MyTableOfMissingIds
From 
(   Select 1 MissingId
    Union Select 2
    Union Select 3
    Union Select 4
    Union Select 5
    Union Select 6
) xx

Update #MyTableOfMissingIds
Set @MissingIDList = @MissingIDList + Cast (MissingId as VarChar) + ','

Declare @Output_Error VarChar (Max)
SET @Output_Error = 'ERROR: Key/s ' + @MissingIDList + ' for Customer Shipping ID missing from Customer table';

RAISERROR (@Output_Error,16,1)

我的示例中的Update语句可以替换为您的一个表(OTTOMH,因此可能需要验证语法)

Declare @MissingIDList VarChar (Max) = ''
Declare @Output_Error VarChar (Max)

UPDATE staging.Sale SET 
Set @MissingIDList = @MissingIDList + Cast (Customer_Shipping_ID as VarChar) + ','
WHERE NOT EXISTS (
        SELECT cu.Customer_Shipping_ID
        FROM staging.Customer cu
        WHERE LTRIM(RTRIM(sa.Customer_Shipping_ID)) = LTRIM(RTRIM(cu.Customer_Shipping_ID)))

SET @Output_Error = 'ERROR: Key/s ' + @MissingIDList + ' for Customer Shipping ID missing from Customer table';

RAISERROR (@Output_Error,16,1)

现在,如果您熟练使用XML,这是另一种很酷的方法:

Declare @MissingIDList VarChar (Max) = ''
Declare @Output_Error VarChar (Max)

DROP Table #MyTableOfMissingIds

Select *
Into #MyTableOfMissingIds
From 
(   Select 1 MissingId
    Union Select 2
    Union Select 3
    Union Select 4
    Union Select 5
    Union Select 6
) xx

SELECT @MissingIDList = 
    SUBSTRING(
                (   SELECT ',' + Cast (MissingId as VarChar)
                    FROM #MyTableOfMissingIds s
                    ORDER BY 1
                    FOR XML PATH('')
                )
                ,2,200000
            )

SET @Output_Error = 'ERROR: Key/s ' + @MissingIDList + ' for Customer Shipping ID missing from Customer table';

RAISERROR (@Output_Error,16,1)