问题描述
我希望我的存储过程为我的暂存行填充一个“拒绝”表,该表不能注入到我的目标表中(例如,没有描述的行在我的目标中不是 NULL)。
我不知道拒绝表的结构。
如果您需要更多信息,我可以编辑!
有我的餐桌
CREATE TABLE [staging].[B]
(
[ID] [varchar](250) NULL,[Address] [varchar](250) NULL,[Approved] [bit] NULL,[Description] [varchar](1000) NULL;
)
这是我的目标表:
CREATE TABLE [dbo].[A]
(
[ID] [varchar](250) NOT 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..