我该如何以XML格式发送数据以批量更新SQL Server 2005中的数据?我能够将批量数据插入表中,但我不知道如何更新数据。
我该如何以XML格式发送数据以批量更新SQL Server 2005中的数据?我能够将批量数据插入表中,但我不知道如何更新数据。
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
SQL Server 2005及以上版本具有原生XML数据类型支持,同时支持XQuery语言将XML剖分为关系型数据列。
查看SQL Server 2005中的XQuery介绍,了解其工作原理。
更新需要您确定一些外部逻辑。
例如,如果传入记录的主键已经存在,则更新其他列,否则插入此记录。
我建议您编写一些XSLT来从传入的XML流创建更新语句,然后运行该SQL脚本。
要更新表格中的值,请使用
CREATE PROCEDURE UpdEmpd
@UpdEmpd xml
AS
BEGIN
UPDATE HumanResources.EmployeeData
SET Salary=3000.00
WHERE EmployeeID='E15'
END
执行:
Exec UpdEmpd '<Record xmlns:xsi="http://www.w3.org/2012/xmlschema-instance">
<HumanResources.EmployeeData>
<Salary>3000.00</Salary>
<EmployeeID>E15</EmployeeID>
</HumanResources.EmployeeData>
</Record>'