我知道如何在存储过程中使用条件。 但我目前的问题就在这里。
BEGIN
SELECT *
FROM [dbo].[Talents]
WHERE
((DATEDIFF(YEAR,Talents.DOB,GETDATE()) Between
(
SELECT dbo.AgeRange.AgeFrom
FROM dbo.AgeRange
WHERE
((@age_id is null) or (@age_id='') or ([dbo].AgeRange.ID IN
(
SELECT CAST (Item as int) FROM dbo.SplitString(@age_id,',')
)
))
)
AND
(
SELECT dbo.AgeRange.AgeTo
FROM dbo.AgeRange
WHERE
((@age_id is null) or (@age_id='') or ([dbo].AgeRange.ID IN
(
SELECT CAST (Item as int) FROM dbo.SplitString(@age_id,',')
)
))
)
)
)
END
如果有1个值,它将返回正确的结果。我的意思是
Between 0 And 9
如果条款
之间有很多,我该怎么办?Between 0 and 9
OR
Between 20 and 30
我知道在这种情况下使用OR。但我不知道如何使用。
感谢。
答案 0 :(得分:2)
这是怎么回事?
DECLARE @flds AS VARCHAR(MAX) --Multi-values
DECLARE @fld AS VARCHAR(MAX) --for One value
SET @flds = '13,67,89,105,203'--Multi-values
DECLARE Recs CURSOR FORWARD_ONLY FOR
SELECT VALUE FROM StringSplit(@flds, ',')
OPEN Recs
FETCH Recs INTO @fld
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF ((@fld IS NOT NULL)
IF ((@fld IS NOT NULL) AND (@fld between 10 and 20))
BEGIN
SELECT * FROM test WHERE (fld between 10 and 10) --For 13
END
IF ((@fld IS NOT NULL) AND (@fld between 60 and 70))
BEGIN
SELECT * FROM test WHERE (fld between 60 and 70) ... --For 67
END
IF ((@fld IS NOT NULL) AND (@fld between 80 and 90))
BEGIN
SELECT * FROM test WHERE (fld between 80 and 90) --For 89
END
IF ((@fld IS NOT NULL) AND (@fld between 100 and 200))
BEGIN
SELECT * FROM test WHERE (fld between 100 and 200) ... --For 105
END
IF ((@fld IS NOT NULL) AND (@fld between 200 and 300))
BEGIN
SELECT * FROM test WHERE (fld between 200 and 300) ... --For 203
END
FETCH Recs INTO @fld
END
END
CLOSE Recs
DEALLOCATE Recs
答案 1 :(得分:1)
SELECT * FROM test
WHERE (fld between 0 and 9) OR (fld between 20 and 30);