Compare Actual table with @TypeTable_TVP IF Duplicate Row Exist
本问题已经有最佳答案,请猛点这里访问。
我想将Actual table与@TableType_TVP IF Duplicate Row Exist进行比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | CREATE PROCEDURE sp_InsertintoTable @TableType_TVP TableType_TVP READONLY AS IF(Duplicate ROW NOT Exist THEN) BEGIN INSERT INTO ActualTable SELECT * FROM @TableType_TVP SELECT Inserted ROW COUNT also END ELSE BEGIN COUNT Duplicate ROW END |
试试这个。 注意:我没有在SSMS中测试以下查询。 所以plz指出是否存在任何语法错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | CREATE PROCEDURE proc_InsertintoTable @TableType_TVP TABLE READONLY AS BEGIN DECLARE @insertedCnt INT, @dupCnt INT -- FETCH DUPLICATE ROW SELECT @dupCnt = COUNT(*) FROM ActualTable atbl WHERE EXISTS ( SELECT 1 FROM @TableType_TVP tv WHERE atbl.ID = tv.ID ) -- INSERT NEW ROW INSERT INTO ActualTable SELECT * FROM @TableType_TVP tv WHERE NOT EXISTS ( SELECT 1 FROM ActualTable atbl WHERE atbl.ID = tv.ID ) SELECT @insertedCnt = @@Rowcount -- SELECT BOTH VARIABLE SELECT @insertedCnt, @dupCnt -- OR YOU CAN ALSO SET THIS 2 VARIABLE AS OUT VARIABLE END |