简体   繁体   English

使用XML批量更新SQL Server表

[英]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 ? 如何通过以XML格式发送数据来更新sql server 2005中的批量数据? 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. 请注意,这是一种suedo代码。 so replace fieldx with your field names and replace primaryKey with the primarykey field name or unique identifier field name for the table. 因此,请将fieldx替换为您的字段名称,并将primaryKey替换为该表的primarykey字段名称或唯一标识符字段名称。

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. SQL Server 2005及更高版本具有对XML数据类型的本机支持,并且还支持XQuery语言以将XML分解为关系数据列。

Check out the Introduction to XQuery in SQL Server 2005 to get a feel for how this works. 查看SQL Server 2005中的XQuery简介,以了解其工作原理。

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. 我可能建议您编写一些xslt以从传入的xml流中创建更新语句,然后运行该sql脚本。

Check out the OPENXML function. OPENXML函数。 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. 您映射还希望将XML数据加载到变量表中,以便像在普通数据库表之间一样轻松地连接或组成其余的查询。

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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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