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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:20:09+00:00 2026-05-25T14:20:09+00:00

I have table which contains columns like SiteID (identity_Col) SiteName POrderID Location Address Cluster

  • 0

I have table which contains columns like

  • SiteID (identity_Col)
  • SiteName
  • POrderID
  • Location
  • Address
  • Cluster
  • VenderName

I want to use xml string to insert/update/delete data in this table. Apart from this column, XML string contains one more column viz. RowInfo. This column will have values like “Unchanged”,”Update”,”New”,”Delete”. Based on this values the rows in the table should be inserted,updated,deleted.

My XML string is as below:

<NewDataSet>
  <DataTable>
     <SiteID>2</SiteID>
     <SiteName>NIZAMPURA</SiteName>
     <POrderID>7</POrderID>
     <Location>NIZAMPURA</Location>
     <SiteAddress>Vadodara</SiteAddress>
     <Cluster>002</Cluster>
     <SubVendorName>Test Vender-1</SubVendorName>
     <RowInfo>UNCHANGED</RowInfo>
  </DataTable>
  <DataTable>
     <SiteID>16</SiteID>
     <SiteName>Site-1</SiteName>
     <POrderID>7</POrderID>
     <Location>Alkapuri</Location>
     <SiteAddress>test</SiteAddress>
     <Cluster>Test Cluster</Cluster>
     <SubVendorName>Test Vender12</SubVendorName>
     <RowInfo>UNCHANGED</RowInfo>
  </DataTable>
  <DataTable>
     <SiteID>17</SiteID>
     <SiteName>Site-3</SiteName>
     <POrderID>7</POrderID>
     <Location>Alkapuri123</Location>
     <SiteAddress>test123</SiteAddress>
     <Cluster>Test Cluster123</Cluster>
     <SubVendorName>Test Vender123</SubVendorName>
     <RowInfo>DELETE</RowInfo>
  </DataTable>
</NewDataSet>'

This is the code that I have written to insert data in table if RowInfo = “NEW”

IF len(ISNULL(@xmlString, '')) > 0
    BEGIN
    DECLARE @docHandle1 int = 0;
    EXEC sp_xml_preparedocument @docHandle1 OUTPUT, @xmlString

    INSERT INTO [SiteTRS] (
                [SiteName],
                [POrderID],
                [Location],
                [SiteAddress],
                [Cluster],
                [SubVendorName])
    SELECT SiteName,POrderID,Location,SiteAddress,Cluster,SubVendorName
        FROM OPENXML (@docHandle1, '/NewDataSet/DataTable')

        WITH (SiteName varchar(50) './SiteName',
          POrderID varchar(50) './PorderID',
          Location varchar(50) './Location',
          SiteAddress varchar(max) './SiteAddress',
          Cluster varchar(50) './Cluster',
          SubVendorName varchar(50) './SubVendorName',
          RowInfo varchar(30) './RowInfo')   
    WHERE RowInfo='NEW' 

But I don’t know how to use XML to update/delete records in the table. Please guide
I am novice in the XML so dont have any idea. Please forgive me if I am making something childish.

  • 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-05-25T14:20:10+00:00Added an answer on May 25, 2026 at 2:20 pm

    I would strongly recommend not to use the old, legacy OPENXML stuff anymore – with XML support in SQL Server, it’s much easier to use the built-in XPath/XQuery methods.

    In your case, I would use a CTE (Common Table Expression) to break up the XML into an “inline” table of rows and columns:

    DECLARE @input XML = '<NewDataSet>
      <DataTable>
        <SiteID>2</SiteID>
        <SiteName>NIZAMPURA</SiteName>
        <POrderID>7</POrderID>
        <Location>NIZAMPURA</Location>
        <SiteAddress>Vadodara</SiteAddress>
        <Cluster>002</Cluster>
        <SubVendorName>Vender-1</SubVendorName>
        <RowInfo>UPDATE</RowInfo>
      </DataTable>
      <DataTable>
        <SiteName>Site-1</SiteName>
        <POrderID>7</POrderID>
        <Location>Alkapuri</Location>
        <SiteAddress>test</SiteAddress>
        <Cluster>Cluster-1</Cluster>
        <SubVendorName>Test Vender</SubVendorName>
        <RowInfo>NEW</RowInfo>
      </DataTable>
    </NewDataSet>'
    
    ;WITH XMLData AS
    (
        SELECT
            NDS.DT.value('(SiteID)[1]', 'int') AS 'SiteID',
            NDS.DT.value('(SiteName)[1]', 'varchar(50)') AS 'SiteName',
            NDS.DT.value('(POrderID)[1]', 'int') AS 'POrderID',
            NDS.DT.value('(Location)[1]', 'varchar(100)') AS 'Location',
            NDS.DT.value('(SiteAddress)[1]', 'varchar(100)') AS 'SiteAddress',
            NDS.DT.value('(Cluster)[1]', 'varchar(100)') AS 'Cluster',
            NDS.DT.value('(SubVendorName)[1]', 'varchar(100)') AS 'SubVendorName',
            NDS.DT.value('(RowInfo)[1]', 'varchar(20)') AS 'RowInfo'
        FROM 
            @input.nodes('/NewDataSet/DataTable') AS NDS(DT)
    )
    SELECT *
    FROM XMLDATA
    

    This gives you rows and columns which you can work with.

    SiteID  SiteName   POrderID Location   SiteAddress  Cluster    SubVendorName   RowInfo
      2     NIZAMPURA     7     NIZAMPURA  Vadodara     002        Vender-1        UPDATE
     NULL   Site-1        7     Alkapuri   test         Cluster-1  Test Vender     NEW
    

    Now if you’re on SQL Server 2008 or newer, you could combine this with the MERGE command to do your INSERT/UPDATE in a single statement, basically.

    If you’re on 2005, you will need to either store this information into a temporary table / table variable inside your stored proc, or you need to do the select multiple times; the CTE allows only one single command to follow it.

    Update: with this CTE, you can then combine it with a MERGE:

    ;WITH XmlData AS 
    (
        SELECT
            NDS.DT.value('(SiteID)[1]', 'int') AS 'SiteID',
            NDS.DT.value('(SiteName)[1]', 'varchar(50)') AS 'SiteName',
            NDS.DT.value('(POrderID)[1]', 'int') AS 'POrderID',
            NDS.DT.value('(Location)[1]', 'varchar(100)') AS 'Location',
            NDS.DT.value('(SiteAddress)[1]', 'varchar(100)') AS 'SiteAddress',
            NDS.DT.value('(Cluster)[1]', 'varchar(100)') AS 'Cluster',
            NDS.DT.value('(SubVendorName)[1]', 'varchar(100)') AS 'SubVendorName',
            NDS.DT.value('(RowInfo)[1]', 'varchar(20)') AS 'RowInfo'
        FROM 
            @input.nodes('/NewDataSet/DataTable') AS NDS(DT)
    )
    MERGE INTO dbo.SiteTRS t
    USING XmlData x ON t.SiteID = x.SiteID
    WHEN MATCHED AND x.RowInfo = 'UPDATE'
       THEN 
         UPDATE SET 
            t.SiteName = x.SiteName,
            t.POrderID = x.POrderID,
            t.Location = x.Location,
            t.SiteAddress = x.SiteAddress,
            t.Cluster = x.Cluster,
            t.SubVendorName = x.SubVendorName
    
    WHEN MATCHED AND x.RowInfo = 'DELETE'
       THEN DELETE 
    
    WHEN NOT MATCHED AND x.RowInfo = 'NEW'
       THEN 
          INSERT(SiteID, SiteName, POrderID, Location, SiteAddress, Cluster, SubVendorName)
          VALUES(x.SiteID, x.SiteName, x.POrderID, x.Location, x.SiteAddress, x.Cluster, x.SubVendorName)
    ;
    

    See some more resources:

    • SQL SERVER – 2008 – Introduction to Merge Statement – One Statement for INSERT, UPDATE, DELETE
    • Using SQL Server 2008’s MERGE statement
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which contains three columns: ID Reference Description I would like,
I have a table which contains the following columns: UniqueID remote_ip_addr platform I want
I have one database table which contains 8 columns. One of the columns is
I have a table called order which contains columns id , user_id , price
We have a table T which contains a several char(8) columns (implicitly) which under
I have a table of surveys which contains (amongst others) the following columns survey_id
I have a user table in my database which contains two columns FirstName and
For example, I have a table which contains the columns: english_description french_description italian_description When
I have a table which has id and statement columns, statement column contains statement
in sql I have a table like: PersonOrganisationRole which has the columns: PersonId, OrganisationId

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.