开发者

Bulk updating an sql server table using xml

Ho开发者_运维问答w 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>'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜