我接受xml类型的输入参数,以便我可以接受交易的保险计划列表。我需要获取xml参数并将其粉碎成一个故事变量,以便我可以处理一些逻辑。如何将XML分解为我的表变量。
这是一个用于创建包含数据的示例表的脚本;
/*
CREATE TABLE PlanData(PlanPos INT IDENTITY, PayerDX INT, PlanDX INT, PayPct DECIMAL(6,2))
INSERT PlanData(PayerDX, PlanDX, PayPct)
VALUES(10, 20, 80)
INSERT PlanData(PayerDX, PlanDX, PayPct)
VALUES(25, 50, 10)
drop table PlanData
*/
DECLARE @xmlPlans XML
SET @xmlPlans = (SELECT PlanPos, PayerDX, PlanDX, PayPct
FROM PlanData
ORDER BY PlanPos
FOR XML RAW('plan'), ROOT('plans')
)
print CAST(@xmlPlans AS NVARCHAR(max))
/* We must convert the XML passed in containing all of the insurance payer plan
data into a usable format for the current scoping. */
DECLARE @tblPlans TABLE(PlanPos INT, IPDX INT, IPPDX INT, PayPct decimal(6, 2))
-- /* Note that the table is built in order from Primary through the last plan */
-- INSERT @tblPlans (PlanPos, IPDX, IPPDX, PayPct)
-- SELECT x.item.value('@PlanPos[1]', 'INT') AS PlanPos,
-- x.item.VALUE('@IPDX[1]', 'INT') AS IPDX,
-- x.item.VALUE('@IPPDX[1]', 'INT') AS IPPDX,
-- x.item.VALUE('@PayPct[1]', 'decimal(6, 2)') AS PayPct
-- FROM @xmlPlans.nodes('//items/item') AS x(item)
INSERT @tblPlans (PlanPos, IPDX, IPPDX, PayPct)
SELECT T.plann.value('@PlanPos', 'int') AS PlanPos
,T.plann.VALUE('@PayerDX', 'INT') AS IPDX
,T.plann.VALUE('@PlanDX', 'INT') AS IPPDX
,T.plann.VALUE('@PayPct', 'decimal(6, 2)') AS PayPct
FROM @xmlPlans.nodes('plans/plan') as T(plann)
---- Attribute-centered XML
--DECLARE @data XML
--SET @data = '<data><customer id="1" name="Allied Industries"/><customer id="2" name="Trades International"/></data>';
--
--DECLARE @tblCust TABLE(id INT, [name] VARCHAR(20))
--
--
---- Using the value() method
--INSERT @tblCust (id, [name])
--SELECT T.customer.value('@id', 'INT') AS customer_id,
-- T.customer.value('@name', 'VARCHAR(20)') AS customer_name
--FROM @data.nodes('data/customer') AS T(customer);
--
--SELECT id AS dx, name AS CustName FROM @tblCust
-- SELECT x.item.value('@PlanPos[1]', 'INT') AS PlanPos,
-- x.item.VALUE('@IPDX[1]', 'INT') AS IPDX,
-- x.item.VALUE('@IPPDX[1]', 'INT') AS IPPDX,
-- x.item.VALUE('@PayPct[1]', 'decimal(6, 2)') AS PayPct
-- FROM @xmlPlans.nodes('//items/item') AS x
SELECT
p.PlanPos AS PlanPos,
p.IPDX AS IPDX,
p.IPPDX AS IPPDX,
p.PayPct AS PayPct
FROM @tblPlans p
你也会看到我尝试失败。
谢谢,
布赖恩
答案 0 :(得分:2)
好的,我尝试了你的表格,我从你的FOR XML RAW.....
声明中获得了以下输出:
<plans>
<plan PlanPos="1" PayerDX="10" PlanDX="20" PayPct="80.00" />
<plan PlanPos="2" PayerDX="25" PlanDX="50" PayPct="10.00" />
</plans>
现在要将哪些属性放入表变量的哪些列? x.item.value(...)
语句中使用的属性名称与FOR XML
语句的XML结果中的实际名称不匹配。
试试这个:
DECLARE @tblPlans TABLE(PlanPos INT, IPDX INT, IPPDX INT, PayPct decimal(6, 2))
INSERT @tblPlans (PlanPos, IPDX, IPPDX, PayPct)
SELECT x.item.value('@PlanPos[1]', 'INT') AS PlanPos,
x.item.value('@PayerDX[1]', 'INT') AS IPDX,
x.item.value('@PlanDX[1]', 'INT') AS IPPDX,
x.item.value('@PayPct[1]', 'decimal(6, 2)') AS PayPct
FROM @xmlPlans.nodes('//plans/plan') AS x(item)
SELECT * FROM @tblPlans
我得到了这个输出:
PlanPos IPDX IPPDX PayPct
1 10 20 80.00
2 25 50 10.00
或者你真正想要的是什么?