存储过程如何满足NULL条件

时间:2015-03-12 22:33:23

标签: sql sql-server tsql stored-procedures

我正在读取一个文本文件作为输入来执行我的存储过程中的逻辑以下是我的文本文件的示例

<tblThreatenedSpeciesSubzone>
   <ThreatenedSpeciesZoneID>-1</ThreatenedSpeciesZoneID>
   <ManagementZoneID>0</ManagementZoneID>
   <TSSubZoneNumber>BR101_Moderate/Good_Medium_1</TSSubZoneNumber>
   <TSSubZoneArea>0</TSSubZoneArea>
   <AdjacentRemnantVegArea>23</AdjacentRemnantVegArea>
   <PatchArea>0</PatchArea>
   <CreatedBySystemUser>BBCC Training 1</CreatedBySystemUser>
   <UpdatedBySystemUser>BBCC Training 1</UpdatedBySystemUser>
   <SaveType>1</SaveType>
   <VegetationZoneID>-1</VegetationZoneID>
   <ManagementZoneName />
</tblThreatenedSpeciesSubzone>

如果您发现<ManagementZoneName />没有任何价值。在我的存储过程中,我试图找出Managementzone是否有价值但不确定是否正确的方法

IF @ManagementZoneIDInXML > 0 and @@ManagementZoneIDInXML <> NULL
BEGIN
    INSERT INTO @tblThreatenedSpeciesSubzone
                  (ThreatenedSpeciesZoneID,
                    ManagementZoneID,
                    VegetationZoneID,
                    TSSubZoneNumber,
                    TSSubZoneArea,
                    AdjacentRemnantVegArea,
                    PatchArea,
                    DateCreated,
                    CreatedBySystemUser,
                    DateUpdated,
                    UpdatedBySystemUser,
                    SaveType,
                    RowTimestamp)
    SELECT * 
    FROM OPENXML (@hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
    WITH (ThreatenedSpeciesZoneID INT,
          ManagementZoneID INT,
          VegetationZoneID INT,
          TSSubZoneNumber VARCHAR(50),
          TSSubZoneArea NUMERIC(9,2),
          AdjacentRemnantVegArea NUMERIC(9,2),
          PatchArea NUMERIC(9,2),
          DateCreated VARCHAR(50),
          CreatedBySystemUser VARCHAR(50),
          DateUpdated VARCHAR(50),
          UpdatedBySystemUser VARCHAR(50),
          SaveType INT,
          RowTimestamp VARCHAR(50)) XMLDATA
    WHERE 
        ManagementZoneID = @ManagementZoneIDInXML           --Only select the rows that belong to the supplied ManagementZoneID
END
ELSE 
BEGIN
    INSERT INTO @tblThreatenedSpeciesSubzone
                  ( ThreatenedSpeciesZoneID ,
                    ManagementZoneID    ,
                    VegetationZoneID    ,
                    TSSubZoneNumber         ,
                    TSSubZoneArea           ,
                    AdjacentRemnantVegArea  ,
                    PatchArea               ,
                    DateCreated             ,
                    CreatedBySystemUser     ,
                    DateUpdated             ,
                    UpdatedBySystemUser     ,
                    SaveType        ,
                    RowTimestamp            )
    SELECT * FROM OPENXML (@hDoc, '/NewDataSet/tblThreatenedSpeciesSubzone', 2)
    WITH            (   ThreatenedSpeciesZoneID INT         ,
                    ManagementZoneID    INT         ,
                    VegetationZoneID    INT     ,
                    TSSubZoneNumber         VARCHAR(50)     ,
                    TSSubZoneArea           NUMERIC(9,2)    ,
                    AdjacentRemnantVegArea  NUMERIC(9,2)    ,
                    PatchArea               NUMERIC(9,2)    ,
                    DateCreated             VARCHAR(50)     ,
                    CreatedBySystemUser     VARCHAR(50)     ,
                    DateUpdated             VARCHAR(50)     ,
                    UpdatedBySystemUser     VARCHAR(50) ,
                    SaveType        INT     ,
                    RowTimestamp            VARCHAR(50)     ) XMLDATA
    WHERE VegetationZoneID = @VegetationZoneIDInXML      --Only select the rows that belong to the supplied VegetationZoneID 
                                                         --And no Management zone assigned
END

0 个答案:

没有答案