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 8481461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:33:48+00:00 2026-06-10T19:33:48+00:00

I have a table in SQL Server 2008 that stores XML data in an

  • 0

I have a table in SQL Server 2008 that stores XML data in an XML column. A typical XML fragment is:

<validation>
    <Field1 author="56234" date="20120101" />
    <Field2 author="23232" date="20120101" />
        [...etc...]
</validation>

What I’m trying to work out – and I can’t see how to do it – is to select this data with an INNER JOIN to another table and modify the XML data in the result set, i.e., so I get this:

<validation>
    <Field1 author="56234" date="20120101" authorFullName="Bob Smith" />
    <Field2 author="23232" date="20120101" authorFullName="Jane Hill" />
        [...etc...]
</validation>

Now I know I can do a CROSS APPLY to pull the XML data into a recordset directly and inner join – for example with:

select xmldata.a, people.personname
    from xmldata
        cross apply xmldata.x.nodes('/validation/node()') vdata(fielddata)
        inner join people 
            on people.personid = vdata.fielddata.value('@author','NVARCHAR(20)')

But what I actually want to do is return the original XML but with a new attribute added, mapping people.PersonName into the new @authorFullName attribute.

I can’t quite work out the syntax (or even if it’s indeed possible). I’m assuming I’d cross apply to do a modify with insert attribute – something based on

select xmldata.a, xmldata.x
    from xmldata
        cross apply xmldata.x.modify('insert attribute authorFullName {sql:column("people.personfullname")} into /validation/node()')
        inner join people 
            on people.personid = [...what goes here?...]

but getting the syntax right is escaping me. I’m increasingly thinking it’s not possible, and I’m better off doing this in two queries and merging the results in non-SQL business logic.

  • 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-10T19:33:49+00:00Added an answer on June 10, 2026 at 7:33 pm

    You can not use modify in a select statement.

    From modify() Method (xml Data Type)

    The modify() method of the xml data type can only be used in the SET
    clause of an UPDATE statement.

    I think you have two options.

    1. Shred the XML and use for xml path to rebuild the XML document the way you want and insert person name in the appropriate place.
    2. Extract the XML to a variable and use set @XML.modify(insert... to insert person name.

    If you go for the second option you have to use a while loop because expression2 in insert (XML DML) has to be a single node.

    The code for option 2 could look something like this.

    declare @XML xml
    declare @C int
    declare @PersonName varchar(50)
    declare @PersonID int
    
    -- Store your XML in a XML variable
    set @XML = 
    '<validation>
        <Field1 author="56234" date="20120101" />
        <Field2 author="23232" date="20120101" />
    </validation>'
    
    -- Get number of nodes to be modified
    set @C = @XML.value('count(/validation/*)', 'int')
    
    -- For each node
    while @C > 0
    begin
      -- Get person id from XML
      set @PersonID = @XML.value('(/validation/*[sql:variable("@C")]/@author)[1]', 'int')
    
      -- Get person name
      select @PersonName = personname
      from people
      where personid = @PersonID 
    
      if @@rowcount = 1
      begin
        -- add person name to XML
        set @XML.modify('insert attribute authorFullName {sql:variable("@PersonName")} 
                            into (/validation/*[sql:variable("@C")])[1]')
      end
    
      -- next node
      set @C = @C - 1
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2008 R2 table with nvarchar(4000) field. Data that stores
I have an SQL Server 2008 database with a table that has a column
I am using SQL Server 2008. I have a table (TBL_FILE) that stores user
I have a table that stores stock ticks in sql server 2008. It is
I have a table in SQL Server 2008 that stores date/time values as varchar(max)
I use SQL Server 2008 R2 and have a table that I want no
I'm using SQL Server 2008 and I have a table that has a 'Status'
I have a table which contains my ads that can be searched in sql-server-2008.
I have a Microsoft SQL Server 2008 query that returns data from three tables
I have a database table in Sql Server 2008 R2 which contains data stored

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.