Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Query :- In this query it works only If the entire XML is parsed again, than it will not insert duplicate records , But If you change the 1 record out of 3 records in XML than it will insert all there records again, that means again duplicate records in table, Could any one please help me how can I avoid this problem.

Code :-
SQL
ALTER PROCEDURE [dbo].[SP_XMLtoTABLEInsert]
(
    @xmlData XML ,
    @retValue varchar(20) OUTPUT
)
AS

--SET @retValue =NULL;

DECLARE @PolicyNumber VARCHAR(20)
DECLARE @CheckNumber VARCHAR(20)
DECLARE @Amount INT

SELECT
    @PolicyNumber=ISNULL(nref.value('PolicyNumber[1]','VARCHAR(50)'),''),
    @CheckNumber=ISNULL(nref.value('CheckNumber[1]','VARCHAR(50)'),''),
    @Amount=ISNULL(nref.value('Amount[1]','int'),'')
FROM @xmlData.nodes('tma/ tma_body / branchoperator') as R(nref)

BEGIN
    IF EXISTS (SELECT 1 from [Employee2] where PolicyNumber = @PolicyNumber and CheckNumber = @CheckNumber and Amount = @Amount)
        BEGIN
             SET @retValue='Record Already Exist';
        END

    ELSE IF NOT EXISTS (SELECT 1 from [Employee2] where PolicyNumber = @PolicyNumber and CheckNumber = @CheckNumber and Amount = @Amount)
        BEGIN
            INSERT INTO [Employee2](PolicyNumber,CheckNumber,Amount,Collectiondate,NavDate,VendorName)
            SELECT
            (R.ref.value('PolicyNumber[1]', 'varchar(20)')) as 'PolicyNumber',
            R.ref.value('CheckNumber [1]', 'varchar(20)') as ' CheckNumber ',
            R.ref.value('Amount[1]', 'int') as ' Amount',
            R.ref.value('Collectiondate [1]', 'date') as ' Collectiondate',
            R.ref.value('NavDate [1]', 'date') as ' NavDate',
            R.ref.value('VendorName [1]', 'varchar(100)') as ' VendorName'
            FROM @xmlData.nodes('tma/ tma_body / branchoperator') as R(ref)

            IF(@@ROWCOUNT > 0 )
                SET @retValue='SUCCESS';
        END
END


my InPut XML:-
SQL
Declare @retValue1 varchar(50);
Declare @XmlStr XML;
SET @XmlStr='<tma>
  <tma_header>
    <conversationid />
    <transaction>BIGXML</transaction>
    <sourcecountry>India</sourcecountry>
    <sourcecompany>MLIN</sourcecompany>
    <sourcesystem>METWS</sourcesystem>
    <uniquekey>544010844</uniquekey>
    
  </tma_header>
  <tma_body>
    <branchoperator>
      <policynumber>123456</policynumber>
      <checknumber>544010844</checknumber>
      <amount>17000</amount>
      <collectiondate>03/07/2014</collectiondate>
      <navdate>03/07/2014</navdate>
      <vendorname>Bhadri</vendorname>
    </branchoperator>
    <branchoperator>
      <policynumber>789101</policynumber>
      <checknumber>5477866876</checknumber>
      <amount>17000</amount>
      <collectiondate>04/07/2014</collectiondate>
      <navdate>04/07/2014</navdate>
      <vendorname>Sandeep</vendorname>
    </branchoperator>
    <branchoperator>
      <policynumber>5678866</policynumber>
      <checknumber>54325668786</checknumber>
      <amount>17000</amount>
      <collectiondate>07/07/2014</collectiondate>
      <navdate>07/07/2014</navdate>
      <vendorname>Suman</vendorname>
    </branchoperator>
  </tma_body>
</tma>';
EXEC [SP_XMLtoTABLEInsert] @xmlData=@XmlStr,@retValue=@retValue1 OUTPUT
print @retValue1
Posted
Updated 13-Dec-14 2:19am
v2

1 solution

hi,

It's better to use the new t-sql enhancement feature 'MERGE'.
'MERGE' statement insert if the condition is false and delete or update if the condition is verified.

T-SQL is became more and more well-rounded, but PL/SQL still so fare a way

Your stored procedure should look like this :

ALTER PROCEDURE [dbo].[SP_XMLtoTABLEInsert]
(
    @xmlData XML ,
    @retValue varchar(20) OUTPUT
)
AS
BEGIN
	BEGIN TRY

		--Target table to be inserted 
		MERGE [Employee2] AS TARGET
		USING 
		(
			--Select data from xml variable and returned as table
			SELECT
				isnull(R.nref.value('policynumber[1]','VARCHAR(50)'),'') as PolicyNumber,
				isnull(R.nref.value('checknumber[1]','VARCHAR(50)'),'') as CheckNumber,
				isnull(R.nref.value('amount[1]','int'),'') as Amount,
				R.ref.value('Collectiondate [1]', 'date') as Collectiondate,
				R.ref.value('NavDate [1]', 'date') as NavDate,
				R.ref.value('VendorName [1]', 'varchar(100)') as VendorName
			FROM @xmlData.nodes('tma/tma_body/branchoperator') as R(nref)
		)AS SOURCE
		--Condition of Match
		on 
		(
			TARGET.PolicyNumber = SOURCE.PolicyNumber 
		and TARGET.CheckNumber = SOURCE.CheckNumber 
		and TARGET.Amount = SOURCE.Amount
		)
		--Action to do
		WHEN NOT MATCHED THEN 
			INSERT (TARGET.PolicyNumber,TARGET.CheckNumber,TARGET.Amount,TARGET.Collectiondate,TARGET.NavDate,TARGET.VendorName)
			VALUES (SOURCE.PolicyNumber,SOURCE.CheckNumber,SOURCE.Amount,SOURCE.Collectiondate,SOURCE.NavDate,SOURCE.VendorName)
		;
		set @retValue = 'SUCCESS';
	END TRY
	BEGIN CATCH
		set @retValue = 'FAILURE';
	END CATCH
END
 
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