如何使用sql server在XML文档中获得包含具有给定值的子节点的节点?

时间:2016-05-30 09:41:35

标签: sql-server xml

您好我需要根据子节点值(而不是第一个子节点)获取Xml节点。仅当我使用第一个子值查询时,以下代码才有效。

SELECT A.B.value('(ENT_RPT_ID/text())[1]', 'numeric(10)') AS ReportID
FROM @l_runtime_xml.nodes('//SHEET/DRILLTHRU_PARAM/ENT_RPT[(DEFAULT=1)]') AS A (B)

示例XML如下:

<REPORT_RUNTIME_XML><USER_ID>AISHU</USER_ID><ROLE_ID>dw_rept_cnfg</ROLE_ID><OU_ID>8</OU_ID><LANG_ID>1</LANG_ID><JASPER_FILE/><OUTPUT_FILE>-1</OUTPUT_FILE><REPORT_ID>1516</REPORT_ID><REPORT_NAME>Reddy_ER_2705_RRR_Rpt</REPORT_NAME><REPORT_TYPE>4</REPORT_TYPE><REPORT_GEN_PATH/><REPORT_LAUNCH_PHYSICAL_PATH/><REPORT_LAUNCH_VIRTUAL_PATH>Report/</REPORT_LAUNCH_VIRTUAL_PATH><SHEET><SHEET_NO>1</SHEET_NO><SHEET_NAME>Reddy_ER_2705_RRR_Rpt</SHEET_NAME><SHEET_TYPE>0</SHEET_TYPE><FUAREA_ID>1504</FUAREA_ID><DATASOURCE_TYPE>1</DATASOURCE_TYPE><DATASOURCE><DATASOURCE_ID>-1</DATASOURCE_ID><DATASOURCE_NAME>-1</DATASOURCE_NAME><PROVIDER>-1</PROVIDER><SERVER>-1</SERVER><DATABASE>-1</DATABASE><USERID>-1</USERID><USER_PASSWORD>-1</USER_PASSWORD><PORT_NO>-1</PORT_NO></DATASOURCE><SP_PARAMS_LIST/><SP_COLUMNS_LIST/><PARAM_SEQUENCE/><DRILLTHRU_PARAM><ENT_RPT><ENT_RPT_ID>1513</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_RRR_2605_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1514</ENT_RPT_ID><ENT_RPT_NAME>Reddy_2605_ER_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1515</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_2605_AAA_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1516</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_2705_RRR_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>100</ENT_RPT_ID><ENT_RPT_NAME>Data profile report1</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT></DRILLTHRU_PARAM><ADVANCED_OPTIONS><PARAM_HTML/><PAGINATION>Y</PAGINATION><PRINTPREVIEW>N</PRINTPREVIEW><HDR_FREEZE>N</HDR_FREEZE><LAUNCH_RPT_IN_SAME_WINDOW>Y</LAUNCH_RPT_IN_SAME_WINDOW><DIGITAL_SIGNATURE><DIGITAL_SIGNATURE_FLAG>N</DIGITAL_SIGNATURE_FLAG><DS_FILE_NAME/><RPT_BASED_DS_YN>N</RPT_BASED_DS_YN><PAGE_POSITION>fp</PAGE_POSITION><X_POSITION>1</X_POSITION><Y_POSITION>2</Y_POSITION><DS_WIDTH>3</DS_WIDTH><DS_HEIGHT>4</DS_HEIGHT></DIGITAL_SIGNATURE><DIRECT_PRINT>N</DIRECT_PRINT><PRINT_DIALOG>N</PRINT_DIALOG><PASSWORD_PROT><PASSWORD>N</PASSWORD><MAIL_ENCRYPT>N</MAIL_ENCRYPT><COLUMN_NAME/></PASSWORD_PROT><FORMAT_INFO><ENABLE_USER_PREFERENCES>N</ENABLE_USER_PREFERENCES><NUMBER_FORMAT>MILLION</NUMBER_FORMAT><NUMBER_PATTERN>(1.1)</NUMBER_PATTERN><TIME_FORMAT>HH.mm</TIME_FORMAT><DATE_FORMAT>MM/dd/yyyy</DATE_FORMAT></FORMAT_INFO><ISONEPAGEPERSHEET>N</ISONEPAGEPERSHEET><XPATH_QUERY_STRING>RECORDSET/ROW</XPATH_QUERY_STRING><GENERATE>N</GENERATE><GENERATE_VALUE/><FIELD_DELIMITER/></ADVANCED_OPTIONS><REPORT_TYPE><ROW><REPORT_TYPE_NAME>XLS</REPORT_TYPE_NAME><DEFAULT_TYPE>0</DEFAULT_TYPE></ROW><ROW><REPORT_TYPE_NAME>PDF</REPORT_TYPE_NAME><DEFAULT_TYPE>Def</DEFAULT_TYPE></ROW></REPORT_TYPE></SHEET><PAGE_NUMBER_TOC_START>1</PAGE_NUMBER_TOC_START><ENABLE_TOC>N</ENABLE_TOC><DIGITAL_SIGNATURE/><DIGITAL_SIGNATURE_ALL_FLAG>N</DIGITAL_SIGNATURE_ALL_FLAG><SUBREPORTS/><CATEGORY_NAME/><MODE>1</MODE></REPORT_RUNTIME_XML>

2 个答案:

答案 0 :(得分:1)

Xml数据:

DECLARE @x XML = N'
<REPORT_RUNTIME_XML>
  <USER_ID>AISHU</USER_ID>
  <SHEET>
    <DRILLTHRU_PARAM>
      <ENT_RPT>
        <ENT_RPT_ID>1513</ENT_RPT_ID>
        <ENT_RPT_NAME>Reddy_ER_RRR_2605_Rpt</ENT_RPT_NAME>
        <DEFAULT>0</DEFAULT>
        <CRITERIA>
          <DISPLAY />
          <HIDDEN />
        </CRITERIA>
        <COLUMN_HEADER>N</COLUMN_HEADER>
        <DISPLAY_BUTTON>N</DISPLAY_BUTTON>
      </ENT_RPT>
      <ENT_RPT>
        <ENT_RPT_ID>1514</ENT_RPT_ID>
        <ENT_RPT_NAME>Reddy_2605_ER_Rpt</ENT_RPT_NAME>
        <DEFAULT>0</DEFAULT>
        <CRITERIA>
          <DISPLAY />
          <HIDDEN />
        </CRITERIA>
        <COLUMN_HEADER>N</COLUMN_HEADER>
        <DISPLAY_BUTTON>N</DISPLAY_BUTTON>
      </ENT_RPT>
      <ENT_RPT>
        <ENT_RPT_ID>1515</ENT_RPT_ID>
        <ENT_RPT_NAME>Reddy_ER_2605_AAA_Rpt</ENT_RPT_NAME>
        <DEFAULT>1</DEFAULT>
        <CRITERIA>
          <DISPLAY />
          <HIDDEN />
        </CRITERIA>
        <COLUMN_HEADER>N</COLUMN_HEADER>
        <DISPLAY_BUTTON>N</DISPLAY_BUTTON>
      </ENT_RPT>
      <ENT_RPT>
        <ENT_RPT_ID>1516</ENT_RPT_ID>
        <ENT_RPT_NAME>Reddy_ER_2705_RRR_Rpt</ENT_RPT_NAME>
        <DEFAULT>0</DEFAULT>
        <CRITERIA>
          <DISPLAY />
          <HIDDEN />
        </CRITERIA>
        <COLUMN_HEADER>N</COLUMN_HEADER>
        <DISPLAY_BUTTON>N</DISPLAY_BUTTON>
      </ENT_RPT>
      <ENT_RPT>
        <ENT_RPT_ID>100</ENT_RPT_ID>
        <ENT_RPT_NAME>Data profile report1</ENT_RPT_NAME>
        <DEFAULT>0</DEFAULT>
        <CRITERIA>
          <DISPLAY />
          <HIDDEN />
        </CRITERIA>
        <COLUMN_HEADER>N</COLUMN_HEADER>
        <DISPLAY_BUTTON>N</DISPLAY_BUTTON>
      </ENT_RPT>
    </DRILLTHRU_PARAM>
  </SHEET>
</REPORT_RUNTIME_XML>'

查询:

SELECT ReportID = t.c.value('(ENT_RPT_ID/text())[1]', 'INT')
FROM @x.nodes('/REPORT_RUNTIME_XML/SHEET/DRILLTHRU_PARAM/ENT_RPT[DEFAULT = "1"]') t(c)

答案 1 :(得分:1)

您只需运行此查询即可获取节点'ENT_RPT_ID'的所有值。

DECLARE @x XML = N'
<REPORT_RUNTIME_XML><USER_ID>AISHU</USER_ID><ROLE_ID>dw_rept_cnfg</ROLE_ID><OU_ID>8</OU_ID><LANG_ID>1</LANG_ID><JASPER_FILE/><OUTPUT_FILE>-1</OUTPUT_FILE><REPORT_ID>1516</REPORT_ID><REPORT_NAME>Reddy_ER_2705_RRR_Rpt</REPORT_NAME><REPORT_TYPE>4</REPORT_TYPE><REPORT_GEN_PATH/><REPORT_LAUNCH_PHYSICAL_PATH/><REPORT_LAUNCH_VIRTUAL_PATH>Report/</REPORT_LAUNCH_VIRTUAL_PATH><SHEET><SHEET_NO>1</SHEET_NO><SHEET_NAME>Reddy_ER_2705_RRR_Rpt</SHEET_NAME><SHEET_TYPE>0</SHEET_TYPE><FUAREA_ID>1504</FUAREA_ID><DATASOURCE_TYPE>1</DATASOURCE_TYPE><DATASOURCE><DATASOURCE_ID>-1</DATASOURCE_ID><DATASOURCE_NAME>-1</DATASOURCE_NAME><PROVIDER>-1</PROVIDER><SERVER>-1</SERVER><DATABASE>-1</DATABASE><USERID>-1</USERID><USER_PASSWORD>-1</USER_PASSWORD><PORT_NO>-1</PORT_NO></DATASOURCE><SP_PARAMS_LIST/><SP_COLUMNS_LIST/><PARAM_SEQUENCE/><DRILLTHRU_PARAM><ENT_RPT><ENT_RPT_ID>1513</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_RRR_2605_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1514</ENT_RPT_ID><ENT_RPT_NAME>Reddy_2605_ER_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1515</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_2605_AAA_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>1516</ENT_RPT_ID><ENT_RPT_NAME>Reddy_ER_2705_RRR_Rpt</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT><ENT_RPT><ENT_RPT_ID>100</ENT_RPT_ID><ENT_RPT_NAME>Data profile report1</ENT_RPT_NAME><DEFAULT>0</DEFAULT><CRITERIA><DISPLAY/><HIDDEN/></CRITERIA><COLUMN_HEADER>N</COLUMN_HEADER><DISPLAY_BUTTON>N</DISPLAY_BUTTON></ENT_RPT></DRILLTHRU_PARAM><ADVANCED_OPTIONS><PARAM_HTML/><PAGINATION>Y</PAGINATION><PRINTPREVIEW>N</PRINTPREVIEW><HDR_FREEZE>N</HDR_FREEZE><LAUNCH_RPT_IN_SAME_WINDOW>Y</LAUNCH_RPT_IN_SAME_WINDOW><DIGITAL_SIGNATURE><DIGITAL_SIGNATURE_FLAG>N</DIGITAL_SIGNATURE_FLAG><DS_FILE_NAME/><RPT_BASED_DS_YN>N</RPT_BASED_DS_YN><PAGE_POSITION>fp</PAGE_POSITION><X_POSITION>1</X_POSITION><Y_POSITION>2</Y_POSITION><DS_WIDTH>3</DS_WIDTH><DS_HEIGHT>4</DS_HEIGHT></DIGITAL_SIGNATURE><DIRECT_PRINT>N</DIRECT_PRINT><PRINT_DIALOG>N</PRINT_DIALOG><PASSWORD_PROT><PASSWORD>N</PASSWORD><MAIL_ENCRYPT>N</MAIL_ENCRYPT><COLUMN_NAME/></PASSWORD_PROT><FORMAT_INFO><ENABLE_USER_PREFERENCES>N</ENABLE_USER_PREFERENCES><NUMBER_FORMAT>MILLION</NUMBER_FORMAT><NUMBER_PATTERN>(1.1)</NUMBER_PATTERN><TIME_FORMAT>HH.mm</TIME_FORMAT><DATE_FORMAT>MM/dd/yyyy</DATE_FORMAT></FORMAT_INFO><ISONEPAGEPERSHEET>N</ISONEPAGEPERSHEET><XPATH_QUERY_STRING>RECORDSET/ROW</XPATH_QUERY_STRING><GENERATE>N</GENERATE><GENERATE_VALUE/><FIELD_DELIMITER/></ADVANCED_OPTIONS><REPORT_TYPE><ROW><REPORT_TYPE_NAME>XLS</REPORT_TYPE_NAME><DEFAULT_TYPE>0</DEFAULT_TYPE></ROW><ROW><REPORT_TYPE_NAME>PDF</REPORT_TYPE_NAME><DEFAULT_TYPE>Def</DEFAULT_TYPE></ROW></REPORT_TYPE></SHEET><PAGE_NUMBER_TOC_START>1</PAGE_NUMBER_TOC_START><ENABLE_TOC>N</ENABLE_TOC><DIGITAL_SIGNATURE/><DIGITAL_SIGNATURE_ALL_FLAG>N</DIGITAL_SIGNATURE_ALL_FLAG><SUBREPORTS/><CATEGORY_NAME/><MODE>1</MODE></REPORT_RUNTIME_XML>'

select @x

SELECT A.B.value('.', 'numeric(10)') AS ReportID
FROM @x.nodes('//SHEET/DRILLTHRU_PARAM/ENT_RPT/ENT_RPT_ID') AS A(B)