2015-07-21 59 views
1

我有簡單的SQL程序,該程序retrives XML數據,並顯示如下: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>','','' 

但數據不顯示。

可能是什麼問題?

請指導我。

程序中沒有錯誤或異常。

+0

待辦事項'1'和'2'打印? –

+0

@Samcd是的,它確實打印所有 –

+0

@Samcd是...但它沒用 –

回答

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 | 
+0

非常感謝,它的工作 –