我将序列化集合(XML)传递给存储过程。我的XML结构是 -
<ArrayofDepartmentEntity>
<Department>
<id>1004</id>
<budget>2500.oo</budget>
</Department>
<Department>
<id>1080</id>
<budget>3500.oo</budget>
</Department>
<Department>
<id>1029</id>
<budget>4500.00</budget>
</Department>
</ArrayofDepartmentEntity>
如何更新相应的预算列,其中部门ID在上面的XML中?可以任何正文记下sql语法。
我的SP语法 -
ALTER PROCEDURE [dbo].[usp_SaveDepartentBudget] (
@departmentBudgetXML ntext = NULL
)
AS
BEGIN
DECLARE @ptrHandle int
EXEC sp_xml_preparedocument @ptrHandle OUTPUT, @departmentBudgetXML
....Here I want to update Budget foreach departmentID in @departmentBudgetXML
答案 0 :(得分:0)
试试这段代码。
DECLARE @XMLString XML
SET @XMLString = '<ArrayofDepartmentEntity>
<Department>
<id>1004</id>
<budget>2500.oo</budget>
</Department>
<Department>
<id>1080</id>
<budget>3500.oo</budget>
</Department>
<Department>
<id>1029</id>
<budget>4500.00</budget>
</Department>
</ArrayofDepartmentEntity>'
SELECT
Department.col.value('id[1]','VARCHAR(20)')AS Id,
Department.col.value('budget[1]','VARCHAR(20)')AS Budget
FROM @XMLString.nodes('//Department') Department(col)
编辑:以下是更新的代码
UPDATE tblInfo SET Budget = Department.col.value('budget[1]','MONEY')
FROM @XMLString.nodes('//Department') Department(col)
WHERE tblInfo.Id = Department.col.value('id[1]','INT')