Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:36:50+00:00 2026-06-01T20:36:50+00:00

I have a table with a complex primary key. Also I have a Service

  • 0

I have a table with a complex primary key. Also I have a Service Broker service which receives XML message which basically looks like the following:

  <TableName>
    <CHANGED key1="1" key2="2" key3="3" timestamp="00:00:01"/>
    <CHANGED key1="1" key2="2" key3="3" timestamp="00:00:02"/>
    <CHANGED key1="1" key2="2" key3="3" timestamp="00:00:03"/>
  </TableName>

My goal is to insert those values into a table.

I tried the following query:

INSERT INTO TableName (KEY1, KEY2, KEY3, TS)
  SELECT 
      Tbl.Col.value('@*[1]', 'int'),
      Tbl.Col.value('@*[2]', 'int'),
      Tbl.Col.value('@*[3]', 'int'),
      Tbl.Col.value('@*[4]', 'datetime')
  FROM   @MESSAGE.nodes('//CHANGED') Tbl(Col)

But in case when we have several records with the same complex key (key1, key2, key3) and different timestamp (value I need for my biz logic) this query fails with the following error message:

Violation of PRIMARY KEY constraint ‘TableName’. Cannot insert
duplicate key in object ‘dbo.TableName’. The duplicate key value is
(1, 2, 3).

Is there a way to insert only distinct values with the latest timestamp from that message?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:36:52+00:00Added an answer on June 1, 2026 at 8:36 pm

    Do a group by on the keys and aggregate on timestamp (min or max?).

    INSERT INTO TableName (KEY1, KEY2, KEY3, TS)
    SELECT key1, key2, key3, MIN(ts)
    FROM
      (
        SELECT 
          Tbl.Col.value('@key1', 'int') AS key1,
          Tbl.Col.value('@key2', 'int') AS key2,
          Tbl.Col.value('@key3', 'int') AS key3,
          Tbl.Col.value('@timestamp', 'datetime') as ts
        FROM   @MESSAGE.nodes('//CHANGED') Tbl(Col)
      ) AS M
    GROUP BY key1, key2, key3
    

    To be really safe you should use the attribute names instead of position(). According to Limitations of the xml Data Type the order of attributes is not guaranteed.

    The order of attributes in an XML instance is not preserved. When you
    query the XML instance stored in the xml type column, the order of
    attributes in the resulting XML may be different from the original XML
    instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file which looks like the below example <Ids> <Id> <set>ai</set>
I have a table with a complex trigger that eventually calls the Service Broker
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table with primary key in my MS SQL Server 2005 table.
I have a complex sorting problem with my SQL statement. I have a table
Hopefully this is less complex than I think. I have one table of companies,
I have a hash table where the keys are rather complex lists, with sublists
I have a complex html DOM tree of the following nature: <table> ... <tr>
I have following complex query which I need to use. When I run it,
I have a rather complex message queue system where in the end I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.