2010-07-14 71 views
1

我有這個存儲過程,從XML中讀取數據,並提出一些刀片存儲過程 - 傳遞參數的XML和讀取數據

ALTER procedure [dbo].[SP_InsertIOs] 
    @iosxml xml AS 

DECLARE @currRecord int  

-- parse the records from the XML 
    EXECUTE sp_xml_preparedocument @currRecord OUTPUT, @iosxml  
    BEGIN TRY  
    INSERT INTO SN_IO ([C1] ,[C2] ,[C3]) 
    SELECT [C1] ,[C2] ,[C3] 
    FROM OPENXML (@currRecord, 'ios/io', 1) 
    WITH ([C1] [varchar](25)  'C1', 
     [C2] [varchar](25)  'C2', 
     [C3] [varchar](20)  'C3' )                 
    END TRY 
    BEGIN CATCH 
     //SELECT SOME ERROR 
    END CATCH 
    EXECUTE sp_xml_removedocument @currRecord 

的XML看起來像這樣

<ios> 
    <io> 
    <C1>a</C1> 
    <C2>b</C2> 
    <C3>c</C3>  
    </io> 
    <io> 
    <C1>x</C1> 
    <C2>y</C2> 
    <C3>z</C3> 
    </io> 
</ios> 

一切進展順利。 有時C1或C2或C3可空,這裏是我的問題:

在此過程中,使插入時如果C1爲空或C2爲空或C3爲null跳過記錄,並沒有使插入

回答

0

你只需要一個WHERE條款我想。

INSERT INTO SN_IO ([C1] ,[C2] ,[C3]) 
    SELECT [C1] ,[C2] ,[C3] 
    FROM OPENXML (@currRecord, 'ios/io', 1) 
    WITH ([C1] [varchar](25)  'C1', 
     [C2] [varchar](25)  'C2', 
     [C3] [varchar](20)  'C3' )  
    WHERE [C1] IS NOT NULL AND [C2] IS NOT NULL AND [C3] IS NOT NULL 

或者你可以在XPath做到這一點,而不是我的猜測可能會更有效

FROM OPENXML (@currRecord, 'ios/io[C1 and C2 and C3]', 1) 
+0

賓果。我從第一個地方嘗試過,但有一個錯誤。對於[C1]爲NULL,標記必須丟失,並且我使用 null ion 2010-07-14 09:35:53

相關問題