使用TSQL读取XML值

时间:2017-03-31 06:43:01

标签: xml tsql

我想使用TSQL从此Web服务响应中检索CUSTOMERNO。请注意,这是我得到的确切xml文件。提前致谢。

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
  <CreateCustomerResponse xmlns="http://tempuri.org/">  
  <CreateCustomerResult>&lt;RESPONSEINFO&gt;&lt;STATUS&gt;&lt;TRANSACTIONNO&gt;60878693&lt;/TRANSACTIONNO&gt;&lt;ERRORNO&gt;1002&lt;/ERRORNO&gt;&lt;MESSAGE&gt;Success&lt;/MESSAGE&gt;&lt;/STATUS&gt;&lt;CUSTOMERNO&gt;9021325146&lt;/CUSTOMERNO&gt;&lt;/RESPONSEINFO&gt;
 </CreateCustomerResult></CreateCustomerResponse>
 </soap:Body></soap:Envelope>

2 个答案:

答案 0 :(得分:0)

假设您的XML被读入xml类型变量,可以像这样完成:

DECLARE @xml XML=
N'<RESPONSEINFO>
  <STATUS>
    <TRANSACTIONNO>60878693</TRANSACTIONNO>
    <ERRORNO>1002</ERRORNO>
    <MESSAGE>Success</MESSAGE>
  </STATUS>
  <CUSTOMERNO>9021325146</CUSTOMERNO>
</RESPONSEINFO>';

SELECT @xml.value(N'(/RESPONSEINFO/CUSTOMERNO)[1]',N'bigint')

更新您的问题完全改变了......(请避免变色龙!)

您必须首先阅读包装的XML并从那里阅读:

DECLARE @xml XML=
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CreateCustomerResponse xmlns="http://tempuri.org/">
      <CreateCustomerResult>&lt;RESPONSEINFO&gt;&lt;STATUS&gt;&lt;TRANSACTIONNO&gt;60878693&lt;/TRANSACTIONNO&gt;&lt;ERRORNO&gt;1002&lt;/ERRORNO&gt;&lt;MESSAGE&gt;Success&lt;/MESSAGE&gt;&lt;/STATUS&gt;&lt;CUSTOMERNO&gt;9021325146&lt;/CUSTOMERNO&gt;&lt;/RESPONSEINFO&gt;
 </CreateCustomerResult>
    </CreateCustomerResponse>
  </soap:Body>
</soap:Envelope>';

SELECT CAST(@xml.value(N'(/*:Envelope/*:Body/*:CreateCustomerResponse/*:CreateCustomerResult/text())[1]',N'nvarchar(max)') AS XML).value(N'(/RESPONSEINFO/CUSTOMERNO)[1]',N'bigint')

答案 1 :(得分:0)

一种方法是使用下面的代码片段 -

onSelectRow: function (id) {
    var rid = jQuery('#tblJQGridBereitschaft').jqGrid("getGridParam", "selrow");
    if (rid) {
        var row = jQuery('#tblJQGridBereitschaft').jqGrid("getRowData", rid);
    }
    var bs_id = row.ID;
    function seturl(bs_id){
        jQuery("#tblJQGridBereitschaftFahrtkosten").setGridParam({ loadonce: false })
        jQuery("#tblJQGridBereitschaftFahrtkosten").setGridParam({ datatype: "json" })
        jQuery("#tblJQGridBereitschaftFahrtkosten").setGridParam({ url: '@Url.Action("GetFahrtkosten", "Bereitschaft")?BS_ID=' + bs_id +'', page: 1 }).trigger('reloadGrid');
        jQuery("#tblJQGridBereitschaftFahrtkosten").setGridParam({ editurl: '@Url.Action("ModifyFahrtkosten", "Bereitschaft")?BS_ID=' + bs_id, page: 1 }).trigger('reloadGrid');
        // Here I need the "addurl"
        jQuery("#tblJQGridBereitschaftFahrtkosten").setGridParam({ loadonce: true })
    }
    seturl(bs_id);
    $("#headerFahrtkosten").show();
    $("#jqgrid_containerfahrtkosten").show();
},

修改:根据更改后的问题,修改了代码段 -

DECLARE @xmltext XML = N'<RESPONSEINFO><STATUS><TRANSACTIONNO>60878693</TRANSACTIONNO><ERRORNO>1002</ERRORNO><MESSAGE>Success</MESSAGE></STATUS><CUSTOMERNO>9021325146</CUSTOMERNO></RESPONSEINFO>'     
SELECT T.C.value('CUSTOMERNO[1]', 'varchar(255)') FROM @xmltext.nodes('/RESPONSEINFO') T(C)