Return 来自存储过程的拒绝 table

Return a reject table from a stored procedure

我希望我的存储过程为我的暂存行中的行填充 'reject' table,并且不能将其注入我的目标 table(例如没有描述的行这在我的目标中不是 NULL)。

我不知道拒绝的结构 table。

如果您需要更多信息,我可以编辑!

有我的table分期

CREATE TABLE [staging].[B]
(
    [ID] [varchar](250)  NULL,
    [Address] [varchar](250) NULL,
    [Approved] [bit]  NULL,
    [Description] [varchar](1000) NULL;
)

这是我的目标table:

CREATE TABLE [dbo].[A]
(
    [ID] [varchar](250) NOT NULL,
    [Address] [varchar](250) NULL,
    [Approved] [tinyint] NULL,
    [Description] [varchar](1000) NOT NULL;
)

这是我的存储过程代码:

  ALTER PROCEDURE [dbo].[StoredProcedure]
    (
        @processType Varchar(4),
        @stagingTable Varchar(64),
        @destinationTable Varchar(64),
        @errorMessage NVarchar(MAX) = null OUTPUT
    )
AS
BEGIN

   SET NOCOUNT ON;

   DECLARE @isLocaltran BIT = 0

   BEGIN TRY
   
    IF @@TRANCOUNT = 0
    BEGIN
        BEGIN TRANSACTION
        SET @isLocaltran = 1
    END
    DELETE 
    FROM  staging.B
    WHERE staging.B.[Description] IS NULL 
    OUTPUT 
    deleted.* 
    INTO [dbo].[RejectTable]

    MERGE dbo.A AS TARGET
    USING staging.B AS SOURCE ON (TARGET.ID = SOURCE.ID)
       SELECT 
           TARGET.[Address],
           TARGET.[Approved],
           TARGET.[Description]

       EXCEPT

       SELECT  
           SOURCE.[Address],
           SOURCE.[Approved],
           SOURCE.[Description]
                                )
         THEN
            UPDATE 
                SET TARGET.[Address] = SOURCE.[Address],
                    TARGET.[Approved] = SOURCE.[Approved],
                    TARGET.[Description] = SOURCE.[Description] 
               
    WHEN NOT MATCHED 
        THEN
            INSERT (,[ID], [Address], [Approved], [Description])
            VALUES (...)

)

非常感谢@Dan Guzman 的评论。对我帮助很大。

DELETE FROM Staging.b OUTPUT deleted.* INTO RejectTable WHERE..