我们会收到一整段XML,其中包含各种细分。
需要帮助将XML段分离为单独的XML块到本地xml变量。
保存分离的XML段的本地xml变量将在参数上传递给另一个存储过程。
例如:
Declare @Message xml
set @Message =
'<Message>
<Procedure>sp_testProc</Procedure>
<Customer>
<row>
<CustID>111</CustID>
<CustName>TestName2</CustName>
</row>
<row>
<CustID>222</CustID>
<CustName>TestName2</CustName>
</row>
</Customer>
<Product>
<ProdCode>AA</ProdCode>
<ProdName>TestProdAA</ProdName>
</Product>
</Message>'
select @Message
Declare @Proc xml
Declare @Customer XML
Declare @Product xml
----Need query help to extract as below, from @Message.
set @Proc = '<Procedure>sp_testProc</Procedure>'
set @Customer =
'<Customer>
<row>
<CustID>111</CustID>
<CustName>TestName2</CustName>
</row>
<row>
<CustID>222</CustID>
<CustName>TestName2</CustName>
</row>
</Customer>'
set @Product =
'<Product>
<ProdCode>AA</ProdCode>
<ProdName>TestProdAA</ProdName>
</Product>'
答案 0 :(得分:2)
您的朋友被称为.query()
使用这些线条,您将获得分开的部分:
Declare @Message xml
set @Message =
'<Message>
<Procedure>sp_testProc</Procedure>
<Customer>
<row>
<CustID>111</CustID>
<CustName>TestName2</CustName>
</row>
<row>
<CustID>222</CustID>
<CustName>TestName2</CustName>
</row>
</Customer>
<Product>
<ProdCode>AA</ProdCode>
<ProdName>TestProdAA</ProdName>
</Product>
</Message>';
SELECT @Message.query('/Message/Procedure') AS TheProc
,@Message.query('/Message/Customer') AS TheCust
,@Message.query('/Message/Product') AS TheProd
这是一次填充三个变量的代码
Declare @Proc xml
Declare @Customer XML
Declare @Product xml
SELECT @Proc=@Message.query('/Message/Procedure')
,@Customer=@Message.query('/Message/Customer')
,@Product=@Message.query('/Message/Product');
SELECT @Proc;
SELECT @Customer;
SELECT @Product