Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to avoid duplicates records while bulk inserting in to Table in SQL. I am passing XML as input parameter and inserting using OpenXml

What I have tried:

SQL
EXEC sp_xml_preparedocument @docHandle OUTPUT, @ip_XML                 
BEGIN TRY               
BEGIN TRAN                     
SET @op_error_code = 0;              

IF(@ip_Status ='I')            
BEGIN            

	INSERT INTO ACH_OUTWARD_Files                
	(   
	            
	FileName                 
	, FileType                 
	, Status                 
	, CreatedDate              
	   
	)           
	 
	SELECT indx.FileName                 
	, '102_OUTWARD_ACH'                 
	,  indx.Mode              
	, GETDATE()                 
 
	FROM OPENXML(@docHandle, '/WPS/MDL_ACH_Outward_Details',2)WITH                                  
	(   
	FileName varchar(100)  
	,FileType varchar(50)                 
	, Mode  varchar(1)    
	, CreatedDate DateTime             
	) 

EXEC sp_xml_removedocument @docHandle   

END
Posted
Updated 15-May-17 0:28am
v2
Comments
Bryian Tan 12-May-17 0:16am    
Not sure how large is the table and performance wise, but you can try add a where clause: WHERE indx.FileName NOT IN (SELECT FileName FROM ACH_OUTWARD_Files )

1 solution

To prevent inserts of duplicate records that exist in the XML change your SELECT to SELECT DISTINCT
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900