如何编写存储过程以选择,删除然后插入?

时间:2017-09-11 21:02:17

标签: sql sql-server database stored-procedures

我正在为我的UI测试加载示例数据,为此我试图检查数据是否存在。如果是,则删除它并插入新的。我有大约9个插入,我不确定是否需要检查每一行是否存在然后删除每一行然后插入。

这是我尝试使用SP加载的示例数据。

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP', getDate(), getDate(), 1); 

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP1', getDate(), getDate(), 1); 

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP2', getDate(), getDate(), 2);  

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP3', getDate(), getDate(), 3); 

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP4', getDate(), getDate(), 4); 

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP5', getDate(), getDate(), 5); ; 

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP6', getDate(), getDate(), 6);  

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP7', getDate(), getDate(), 7);  

INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE) VALUES 
(212019, 'SOURCE', 'COMP8', getDate(), getDate(), 8); 

存储过程

CREATE PROCEDURE TRANSACTION
@TRANSACTION_ID int,
@MDATA_ATTRB varchar,
@MDATA_VALUE varchar,
@ISACTIVE bit

AS
BEGIN
IF EXISTS (SELECT * FROM TRANS_MDATA WHERE TRANSACTION_ID = @TRANSACTION_ID)

BEGIN
DELETE FROM TRANS_MDATA WHERE TRANSACTION_ID = @TRANSACTION_ID
END

ELSE
INSERT INTO TRANS_MDATA (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE)
VALUES (@TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, getdate(), getDate(), @ISACTIVE)
END 
END

EXEC [TRANSACTION]
@TRANSACTION_ID = 123456,
@MDATA_ATTRB = 'Source',
@MDATA_VALUE = 'Backend',
@ISACTIVE bit = 1

1 个答案:

答案 0 :(得分:0)

您可以在插入中使用EXCEPT来检查多列之间的数据匹配:

--  Set up a CTE pseudo-table of your test data:
; WITH TestValues
AS (
    SELECT DISTINCT TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE
    FROM (
    VALUES (212019, 'SOURCE', 'COMP', getDate(), getDate(), 1)
        , (212019, 'SOURCE', 'COMP1', getDate(), getDate(), 1)
        ,(212019, 'SOURCE', 'COMP2', getDate(), getDate(), 2)
        ,(212019, 'SOURCE', 'COMP3', getDate(), getDate(), 3)
        ,(212019, 'SOURCE', 'COMP4', getDate(), getDate(), 4)
        ,(212019, 'SOURCE', 'COMP5', getDate(), getDate(), 5)
        ,(212019, 'SOURCE', 'COMP6', getDate(), getDate(), 6)
        ,(212019, 'SOURCE', 'COMP7', getDate(), getDate(), 7)
        ,(212019, 'SOURCE', 'COMP8', getDate(), GETDATE(), 8) 
    ) t1 (TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE)
    )
    --  Insert anything not already in the target table (EXCEPT):
    INSERT INTO TRANS_MDATA(TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE)
    SELECT TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE
    FROM TestValues
    EXCEPT
    SELECT TRANSACTION_ID, MDATA_ATTRB, MDATA_VALUE, CREATED_TIME, LAST_MOD_TIME, ISACTIVE
    FROM TRANS_MDATA