我想比较Actual table和@TableType_TVP IF Duplicate Row Exist
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
答案 0 :(得分:1)
试试这个。 注意:我没有在SSMS中测试以下查询。所以plz指出是否存在语法错误。
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