Check duplicates records while bulk inserting in to Table

  • Hello,

    I want to insert the record in to a table using bulk insert method fro that I use the below

    EXEC sp_xml_preparedocument @index OUTPUT, @xmlReviewComments

    INSERT INTO PC_Tbl_FooterTemplate_Position

    (

    TemplateId,

    Position,

    [Content]

    )

    SELECT @FooterId ,Position,Content

    FROM OPENXML (@index, '/NewDataSet/Table1')

    WITH (

    Templateid bigint 'Templateid',

    Position nvarchar(50) 'Position',

    Content nvarchar(500) 'Content'

    )

    EXEC sp_xml_removedocument @index

    above is an Example

    Before insert How can I check for a duplication whether the insertion record already present in the table.

    I don't want to insert duplicate records to table.

    Please help me to avoid duplicate insert while bulk upload.

    Thanks

    Regards,

    Mohanaraj.S

  • Try this:

    INSERT INTO PC_Tbl_FooterTemplate_Position

    (

    TemplateId,

    Position,

    [Content]

    )

    SELECT @FooterId, Position, Content

    FROM OPENXML (@index, '/NewDataSet/Table1')

    WITH (

    Templateid bigint 'Templateid',

    Position nvarchar(50) 'Position',

    Content nvarchar(500) 'Content'

    ) indx

    LEFT JOIN PC_Tbl_FooterTemplate_Position pp

    ON pp.TemplateId = indx.Templateid

    -- ? what are the keys of PC_Tbl_FooterTemplate_Position table?

    -- add them all into join here! (don't need to change where clause, it is enough to check one key column for null)

    WHERE pp.TemplateId IS NULL

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • mohanaraj (6/28/2010)


    ...I want to insert the record in to a table using bulk insert method fro that I use the below...

    It's good practice to use a staging table when importing data. Specify column types to maximise data capture. Run TSQL validation / dedupe scripts against it. Flag up rows for eyeball validation.

    Everything in your database is valuable, everything outside is garbage. A staging table serves as a firewall between the two.

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Chris Morris-439714 (6/28/2010)


    Everything in your database is valuable, everything outside is garbage. A staging table serves as a firewall between the two.

    VERY well stated, Mr Morris! ๐Ÿ™‚

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (6/28/2010)


    Chris Morris-439714 (6/28/2010)


    Everything in your database is valuable, everything outside is garbage. A staging table serves as a firewall between the two.

    VERY well stated, Mr Morris! ๐Ÿ™‚

    Thanks Jeff! It never ceases to amaze me how resistant some folks can be to something so obvious.

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply