2014-10-11 25 views
0

我想在我的存儲過程中添加一些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 
+0

可不可以給樣本數據給你錯誤的'DEVREDFLAG'? – 2014-10-11 06:59:38

+0

「dbo.tblManagementZone.SiteValueCurrentScore」,「EECID」或「dbo.tblVegetationZone.PercentageCleared」中的任何一個都可以是「NULL」? – 2014-10-11 07:06:01

+0

是的,它可能是NULL,哎呀糾正我什麼是最好的辦法,如果我有一個空值呢? – Usher 2014-10-11 07:25:11

回答

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