我的存储过程中有以下查询
DECLARE @I INT
DECLARE @TenPercent int
DECLARE @RowsCount int
SET @TenPercent =10
SET @I = 1
SELECT
@I, dbo.tblVegetationType.VegTypeCode, dbo.tblVegetationType.VegTypeName
FROM
dbo.tblVegetationType
INNER JOIN
dbo.tblVegTypeVegFormationLink ON dbo.tblVegetationType.VegTypeID = dbo.tblVegTypeVegFormationLink.VegTypeID
INNER JOIN
tblCMAVegTypeLink ON dbo.tblVegetationType.VegTypeID = dbo.tblCMAVegTypeLink.VegTypeID
WHERE
dbo.tblVegetationType.PercentageCleared >= (@PercentCleared - @TenPercent)
AND dbo.tblVegTypeVegFormationLink.VegetationFormationID = @VegetationFormationID
AND dbo.tblCMAVegTypeLink.CMAID = @CMAID
我有以下条件
dbo.tblVegetationType.PercentageCleared >= (@PercentCleared - @TenPercent)
我在这里要做的是:如果PercentCleared
为60%,那么我想查询从50%中选择列表。
所以我只需添加SET @TenPercent = 10
并从条件中减去。
这是一种正确的方法吗?
答案 0 :(得分:1)
编写查询的另一种方法可以是:
SELECT @I,
VT.VegTypeCode,
VT.VegTypeName
FROM dbo.tblVegetationType VT
INNER JOIN dbo.tblVegTypeVegFormationLink VTVF
ON VT.VegTypeID = VTVF.VegTypeID
INNER JOIN tblCMAVegTypeLink CVT
ON VT.VegTypeID = CVT.VegTypeID
WHERE 1= case when @PercentCleared >= 60 and VT.PercentageCleared <= 50 then 1
-- you can add condition when @PercentCleared < 60 then what values of
-- VT.PercentageCleared are to be considered
when @PercentCleared < 60 and VT.PercentageCleared <= 50 then 1
end
AND VTVF.VegetationFormationID = @VegetationFormationID
AND CVT.CMAID = @CMAID