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

  • Home
  • SEARCH
  • 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 8098227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:01:02+00:00 2026-06-05T22:01:02+00:00

I have two table. One table hold key column and xml column. Other table

  • 0

I have two table. One table hold key column and xml column. Other table hold data. When I try to form xml from second table and update first table. It is updating only one node, it ignores second node. Please refer this example.

    declare @mytable table
    (
    col1 int,
    col2 varchar(100)
    )
    declare @mytable1 table
    (
    col1 int,
    col2 xml
    )
    insert into @mytable1 values(1,null)
    insert into @mytable1 values(2,null)

    insert into @mytable values(1,'abc')
    insert into @mytable values(1,'dcf')
    insert into @mytable values(2,'efg')
    insert into @mytable values(2,'hhh')

    update x set col2=  d.XMLCol from @mytable1 x
    inner join
    (select a.col1,  node.XMLCol from @mytable a join @mytable1 b on a.col1=b.col1 cross apply (select a.col1,a.col2 for xml path('record') , root ('myroot'), type
    ) as node(XMLCol)  )d
    on x.col1=x.col1

    select * from @mytable1

The actual result is

1   <myroot><record><col1>1</col1><col2>abc</col2></record></myroot>
2   <myroot><record><col1>2</col1><col2>hhh</col2></record></myroot>

Expected result is

1   <myroot><record><col1>1</col1><col2>abc</col2></record><record><col1>1</col1><col2>dcf</col2></record></myroot>
2   <myroot><record><col1>2</col1><col2>hhh</col2></record><record><col1>2</col1><col2>efg</col2></record></myroot>

Can someone please point out what am I doing wrong?

Thanks,

Esen.

  • 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-05T22:01:04+00:00Added an answer on June 5, 2026 at 10:01 pm

    Can someone please point out what am I doing wrong?

    1. You are self joining @mytable1 on the same column producing a Cartesian product.on x.col1=x.col1.
    2. Your derived table returns two rows for each row in @mytable1. The first row with the first node and the second row with the second node. The update statement will not combine those rows to one, it will pick one of them.

    Do it like this instead:

    update x set 
      col2 = (
              select a.col1,
                     a.col2
              from @mytable as a
              where x.col1 = a.col1
              for xml path('record'), root('myroot'), type
             )
    from @mytable1 x
    

    SE-Data

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

Sidebar

Related Questions

I have two tables : one table it matchs and the other is teams.
Is it a bad thing to have two xml columns in one table? +
I want to have a SQL table with two columns. One is a key
I'm new to MySQL and PHP. I have two tables, one to hold all
I have two table one table containing station id and station name, and another
In SQL Server 2008 I have two tables. One table for users: id_user -
In a MySQL database I have two tables linked in a join. One table
I have two tables, content and images (and a ContentImages table for the one
I have two columns in a table that need to be added together. One
I have two tables, one is a table of forum threads. It has a

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.