在sql server存储过程中使用游标解析xml文档
我有一个xml文档,希望将其作为参数传递给存储过程 xml如下所示在sql server存储过程中使用游标解析xml文档,sql,sql-server-2005,Sql,Sql Server 2005,我有一个xml文档,希望将其作为参数传递给存储过程 xml如下所示 <root> <EMPLOYEE ID= 100> <PERIOD>AUG-2010</PERIOD> <earnings> <title>BASIC</title> <amount>2000</amount> <t
<root>
<EMPLOYEE ID= 100>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>1000</amount>
<title>CONVEYANCE</title>
<amount>500</amount>
</earnings>
</EMPLOYEE>
<EMPLOYEE ID= 101>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>400</amount>
<title>CONVEYANCE</title>
<amount>500</amount>
</earnings>
</EMPLOYEE>
<EMPLOYEE ID= 102>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>800</amount>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earnings>
</EMPLOYEE>
</root>
然后我转到外部循环,获取下一个员工id,并重复相同的操作
如何以openxml等任何方式访问内部子xml…首先,这是无效的xml:
<EMPLOYEE ID= 102>
<PERIOD>AUG-2010</PERIOD>
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>800</amount>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earnings>
</EMPLOYEE>
下一步:在
标记中有多个..
标记对,但周围没有容器,这一事实使得解析几乎不可能(或非常混乱)
您将得到如下输出:
<earnings>
<earning>
<title>BASIC</title>
<amount>2000</amount>
</earning>
<earning>
<title>HRA</title>
<amount>800</amount>
</earning>
<earning>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earning>
</earnings>
EmployeeID Title Amount
100 BASIC 2000.0000
100 HRA 1000.0000
100 CONVEYANCE 500.0000
101 BASIC 2000.0000
101 HRA 400.0000
101 CONVEYANCE 500.0000
102 BASIC 2000.0000
102 HRA 800.0000
102 CONVEYANCE 5000.0000
<earnings>
<title>BASIC</title>
<amount>2000</amount>
<title>HRA</title>
<amount>800</amount>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earnings>
<earnings>
<earning>
<title>BASIC</title>
<amount>2000</amount>
</earning>
<earning>
<title>HRA</title>
<amount>800</amount>
</earning>
<earning>
<title>CONVEYANCE</title>
<amount>5000</amount>
</earning>
</earnings>
SELECT
RootData.Employee.value('(@ID)[1]', 'int') AS 'EmployeeID',
E.E2.value('(title)[1]', 'varchar(50)') AS 'Title',
E.E2.value('(amount)[1]', 'decimal(18,4)') AS 'Amount'
from
(your XML column).nodes('/root/EMPLOYEE') AS RootData(Employee)
CROSS APPLY
RootData.Employee.nodes('earnings/earning') AS E(E2)
EmployeeID Title Amount
100 BASIC 2000.0000
100 HRA 1000.0000
100 CONVEYANCE 500.0000
101 BASIC 2000.0000
101 HRA 400.0000
101 CONVEYANCE 500.0000
102 BASIC 2000.0000
102 HRA 800.0000
102 CONVEYANCE 5000.0000