在存储过程中卡住条件语句

时间:2014-10-11 06:23:22

标签: sql sql-server

我试图在我的存储过程中添加一些case语句或IF,但它没有返回正确的值

我的情况如下

IF ((SiteValueCurrentScore <=34)&& ((EECID > 0)|| (dbo.tblVegetationZone.PercentageCleared >70))
     DEVREDFLAG= 0
 Else
     DEVREDFLAG=1

我试图在我的存储过程中以相同的方式编写,但它给了我一个错误的值

SELECT dbo.tblVegetationZone.VegetationZoneID, dbo.tblAssessmentCircle.AssessmentCircleID, dbo.tblAssessmentCircle.CircleName, 
    VegetationZoneNumber, VegetationZoneName, VegetationZoneStatusID, VegetationFormationID,
    dbo.tblVegetationZone.VegTypeID, ConditionID, VegetationZoneArea, 
    COALESCE(EECID,0) AS EECID , 
    COALESCE(dbo.tblVegetationZone.PercentageCleared,0) AS PercentageCleared,
    COALESCE((select CommonName from tblSpecies where SpeciesID=EECID),'not an EEC') as EEC,
    COALESCE(dbo.tblManagementZone.SiteValueCurrentScore,0) AS SiteValueCurrentScore , 
    CASE WHEN SiteValueCurrentScore<=34 and (EECID > 0 OR dbo.tblVegetationZone.PercentageCleared >70) THEN 0
    ELSE 1
    END AS DEVREDFLAG,
    RedFlag, MinimumTransectPlot, COALESCE(TransectComplete,0) as TransectComplete, ManagementScoreComplete, dbo.tblVegetationZone.PlantSpeciesMaxBM, 
    dbo.tblVegetationZone.OverStoreyCoverMinBM, dbo.tblVegetationZone.OverStoreyCoverMaxBM, 
    dbo.tblVegetationZone.MidStoreyCoverMinBM, dbo.tblVegetationZone.MidStoreyCoverMaxBM, 
    dbo.tblVegetationZone.GroundCoverGrassesMinBM, dbo.tblVegetationZone.GroundCoverGrassesMaxBM, 
    dbo.tblVegetationZone.GroundCoverShrubsMinBM, dbo.tblVegetationZone.GroundCoverShrubsMaxBM, 
    dbo.tblVegetationZone.GroundCoverOthersMinBM, dbo.tblVegetationZone.GroundCoverOthersMaxBM, 
    dbo.tblVegetationZone.ExoticPlantsCoverMinBM, dbo.tblVegetationZone.ExoticPlantsCoverMaxBM, 
    dbo.tblVegetationZone.TreesWithHollowsMaxBM, dbo.tblVegetationZone.OverStoreyRegenMaxBM, 
    dbo.tblVegetationZone.TotalLengthFallenLogsMaxBM,   
    coalesce(dbo.tblVegetationZone.RemoveMultipliers,0) as RemoveMultipliers,
    dbo.tblVegetationZone.DateCreated, dbo.tblVegetationZone.CreatedBySystemUser, 
    dbo.tblVegetationZone.DateUpdated, dbo.tblVegetationZone.UpdatedBySystemUser,
    5 AS SaveType,  dbo.ufn_varbintohexstr(dbo.tblVegetationZone.RowTimestamp) AS RowTimestamp,
    dbo.tblVegetationType.VegTypeCode, dbo.tblVegetationType.VegTypeName, 
    (SELECT Name         FROM dbo.tblClassification    WHERE ClassificationID = VegetationZoneStatusID) AS VegetationZoneStatus,
    (SELECT Name         FROM dbo.tblClassification    WHERE ClassificationID = VegetationFormationID) AS VegetationFormation,
    (SELECT Name         FROM dbo.tblClassification    WHERE ClassificationID = ConditionID) AS Condition,
    (SELECT SubregionName FROM dbo.vwBIMSCMASubregion        WHERE ObjectID =  dbo.tblAssessmentCircle.CMASubregionID) AS CMASubregion
FROM dbo.tblVegetationZone 
LEFT JOIN dbo.tblManagementZone
    ON dbo.tblManagementZone.VegetationZoneID= dbo.tblVegetationZone.VegTypeID
INNER JOIN dbo.tblAssessmentCircle  
    ON dbo.tblVegetationZone.AssessmentCircleID = dbo.tblAssessmentCircle.AssessmentCircleID
INNER JOIN dbo.tblVegetationType
    ON dbo.tblVegetationZone.VegTypeID = dbo.tblVegetationType.VegTypeID 
WHERE dbo.tblAssessmentCircle.AssessmentVersionID

1 个答案:

答案 0 :(得分:0)

CASE WHEN COALESCE(SiteValueCurrentScore,0)<=34 and (EECID > 0 OR COALESCE(dbo.tblVegetationZone.PercentageCleared,0)>70)
  THEN 0
  ELSE 1
END AS DEVREDFLAG