views:

21

answers:

1

I have this stored procedure, that reads data from xml and make some inserts

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

the xml looks like this

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

Everything goes well. Sometimes C1 or C2 or C3 can be nulls and here are my questions:

In the procedure, when making the inserts if C1 is null or C2 is null or C3 is null skip that record and not make the insertion

A: 

You just need a WHERE clause I think.

   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  

Or you can do it in the XPath instead which I guess may be more efficient

   FROM OPENXML (@currRecord, 'ios/io[C1 and C2 and C3]', 1)
Martin Smith
Bingo. I tried that from the first place but with one mistake. For [C1] to be NULL the tag <C1> must be missing and i used <C1></C1> and <C1>null</C1>
ion