简体   繁体   中英

Bulk updating an sql server table using xml

How can i update bulk data in sql server 2005 by sending the data in XML form ? i am able to insert bulk data into the table, but i am not getting idea to update the data.

Insert into #TempTable
//Basically do bulk insert into temp table then...


Update MyTable
  Set Field1 = tt.Field1,
      Field2 = tt.Field2,
      ...
FROM #TempTable tt
where primaryKey = tt.PrimaryKey

Note this is kind suedo code. so replace fieldx with your field names and replace primaryKey with the primarykey field name or unique identifier field name for the table.

SQL Server 2005 and up has native support for XML data types, and also supports the XQuery language for shredding XML into relational data columns.

Check out the Introduction to XQuery in SQL Server 2005 to get a feel for how this works.

update requires you to determine some external logic.

for instance, if the primary key of the incoming record already exists, update the other columns, otherwise insert this record.

i might suggest you write some xslt to create update statements out of the incoming xml stream, then run that sql script.

Check out the OPENXML function. You map also want to load the XML data into a variable table so you can easily join or compose the rest of the query as you would between normal database tables.

For Updating values in a table use

CREATE PROCEDURE UpdEmpd

    @UpdEmpd xml
AS

BEGIN

  UPDATE HumanResources.EmployeeData

  SET    Salary=3000.00

  WHERE  EmployeeID='E15'

END

To Execute:

Exec UpdEmpd  '<Record xmlns:xsi="http://www.w3.org/2012/xmlschema-instance">

                  <HumanResources.EmployeeData>

                         <Salary>3000.00</Salary>

                         <EmployeeID>E15</EmployeeID>

                  </HumanResources.EmployeeData>

          </Record>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM