SQL过程未显示xml数据

时间:2015-07-21 16:52:15

标签: sql sql-server xml stored-procedures

我有简单的sql过程,它会检索xml数据并显示如下:

ALTER PROCEDURE [dbo].[PROC_UTS_RATERECOMM_RECOMMDATA_XML_SET] (
     @RETURN_CODE INT OUTPUT
    ,@RETURN_MESSAGE NVARCHAR(512) OUTPUT
    ,@XMLDATA xml
    ,@UTILITY_NAME NVARCHAR(50)
    ,@USER_CREATED_MODIFIED_BY NVARCHAR(50)


    )
AS


BEGIN TRY

print '1'

select x.value('@UtilityName','nvarchar(50)') as UtilityName,
x.value('@SuggestedRate','nvarchar(50)') as SuggestedRate,
x.value('@Client','nvarchar(50)') as Client,
x.value('@State','nvarchar(50)') as State,
x.value('@CurrentRate','nvarchar(50)') as CurrentRate,
x.value('@AccNo','nvarchar(50)') as AccNo,
x.value('@Savings','nvarchar(50)') as Savings,
x.value('@tempSuggestedRate','nvarchar(50)') as tempSuggestedRate,
x.value('@tempClient','nvarchar(50)') as tempClient,
x.value('@tempCurrentRateSort','nvarchar(50)') as tempCurrentRate
--into tempTable
 from 
@XMLDATA.nodes('/temp')AS TEMPTABLE(x)

print '2'

END TRY

BEGIN CATCH
    SET @RETURN_MESSAGE = CONVERT(NVARCHAR(2056), 'ERROR NUMBER :' + CONVERT(NVARCHAR,ERROR_NUMBER())  
        + ', ERROR ON LINE :' + CONVERT(NVARCHAR,ISNULL(ERROR_LINE(),'')) + ', ' +  ISNULL(ERROR_MESSAGE(),''))
    EXECUTE PROC_UTS_APPLICATION_LOG_SET '', 'ERROR', @RETURN_MESSAGE  ,NULL, NULL,''
    SELECT @RETURN_CODE = @@ERROR   
    SELECT @RETURN_MESSAGE = 'ERROR WHILE FETCHING THE STATE RECORD!'
    RETURN
END CATCH
--================================================================================--
-- RETURN SUCCESS TO THE CALLING OBJECT
--================================================================================--
SELECT  @RETURN_CODE = 0
        ,@RETURN_MESSAGE = 'Record Fetch successfully!'
--================================================================================--
-- THE END.

我使用以下语句执行此过程:

PROC_UTS_RATERECOMM_RECOMMDATA_XML_SET 0,'','<DocumentElement>
  <temp>
    <UtilityName>ALABAMA POWER</UtilityName>
    <SuggestedRate>LPS</SuggestedRate>
    <Client>PANTRY, THE</Client>
    <State>Alabama</State>
    <CurrentRate>LPS</CurrentRate>
    <AccNo>0239808014</AccNo>
    <Savings>-65</Savings>
    <tempSuggestedRate>LPS</tempSuggestedRate>
    <tempClient>PANTRY, THE</tempClient>
    <tempCurrentRate>LPS</tempCurrentRate>
    <tempCurrentRateSort>LPS-PANTRY, THE-ALABAMA POWER</tempCurrentRateSort>
    <SellRate />
  </temp>
  <temp>
    <UtilityName>ALABAMA POWER</UtilityName>
    <SuggestedRate>LPS</SuggestedRate>
    <Client>PANTRY, THE</Client>
    <State>Alabama</State>
    <CurrentRate>LPS</CurrentRate>
    <AccNo>7267305034</AccNo>
    <Savings>80</Savings>
    <tempSuggestedRate>LPS</tempSuggestedRate>
    <tempClient>PANTRY, THE</tempClient>
    <tempCurrentRate>LPS</tempCurrentRate>
    <tempCurrentRateSort>LPS-PANTRY, THE-ALABAMA POWER</tempCurrentRateSort>
    <SellRate />
  </temp>
</DocumentElement>','',''

但数据没有显示出来。

可能是什么问题?

请指导我。

程序中没有错误或异常。

1 个答案:

答案 0 :(得分:1)

您的查询未正确对应XML结构。请尝试这种方式:

select 
    x.value('UtilityName[1]','nvarchar(50)') as UtilityName,
    x.value('SuggestedRate[1]','nvarchar(50)') as SuggestedRate,
    x.value('Client[1]','nvarchar(50)') as Client,
    x.value('State[1]','nvarchar(50)') as State,
    x.value('CurrentRate[1]','nvarchar(50)') as CurrentRate,
    x.value('AccNo[1]','nvarchar(50)') as AccNo,
    x.value('Savings[1]','nvarchar(50)') as Savings,
    x.value('tempSuggestedRate[1]','nvarchar(50)') as tempSuggestedRate,
    x.value('tempClient[1]','nvarchar(50)') as tempClient,
    x.value('tempCurrentRateSort[1]','nvarchar(50)') as tempCurrentRate
FROM @XMLDATA.nodes('/DocumentElement/temp')AS TEMPTABLE(x)

<强> Sqlfiddle Demo

请注意,XPath / XQuery中的@用于引用XML属性,因为您要获取的只是XML元素,此处不需要@

<强> output

|   UtilityName | SuggestedRate |      Client |   State | CurrentRate |      AccNo | Savings | tempSuggestedRate |  tempClient |               tempCurrentRate |
|---------------|---------------|-------------|---------|-------------|------------|---------|-------------------|-------------|-------------------------------|
| ALABAMA POWER |           LPS | PANTRY, THE | Alabama |         LPS | 0239808014 |     -65 |               LPS | PANTRY, THE | LPS-PANTRY, THE-ALABAMA POWER |
| ALABAMA POWER |           LPS | PANTRY, THE | Alabama |         LPS | 7267305034 |      80 |               LPS | PANTRY, THE | LPS-PANTRY, THE-ALABAMA POWER |