I have a table as below:
|Bookname|BDate|Description
and Bookname is my Primary Key I need to read a file regularly to update my table. I have writen a Stored Procedure to update the file as below:
Create PROCEDURE [dbo].[UpdateMyTable]
-- Add the parameters for the stored procedure here
@SourceTable AS DVTable READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
MERGE dbo.MyTarget AS T
USING @SourceTable AS S ON (T.BookName=S.BookName)
WHEN NOT MATCHED BY Target
THEN INSERT(BookName,BDate,Description)
VALUES(S.BookName,S.BDate,S.Description)
WHEN MATCHED
THEN UPDATE SET T.BookName=S.BookName,T.BDate=S.BDate,T.Description=S.Description;
END
There is a problem that inside the text file some records repeated twice, so the merge function inserts the file at first and for the second time throw exception: [Violation of PRIMARY KEY constraint]
I was wondering if there is any way to set MERGE to ignore it if find that Bookname for the second time.