检查数字@num是否在行T-SQL的val 1和val2之间

时间:2016-08-11 21:01:48

标签: sql-server tsql

我想编写一个T-SQL脚本,检查下面表格中行的@numval1之间的数字val2是否正确:

tblIntervals

 id  val1   val2   points
 1   0.1    0.5    1
 2   0.6    0.7    2
 3   0.8    1      3
 4   1.1    1.3    4
 5   1.5    1.7    5
 ..  ....   ....   ....

例如:If @num=0.3 then points=1; if @num=1.2 then points=4 这是我的一段不起作用的代码:

declare @num float
set @num = 0.5

select * from tblIntervals 
where @num between 

DECLARE @String NVARCHAR(2)
DECLARE @c NVARCHAR(50)
DECLARE @ID INT
WHILE EXISTS (select * from tblIntervals )
 BEGIN 
      WHILE LEN(@c) > 0
        BEGIN
           -- code
        END
 END

2 个答案:

答案 0 :(得分:0)

你可以这样做:

SELECT points
FROM tblIntervals
WHERE @num BETWEEN val1 AND val2

答案 1 :(得分:0)

SELECT * FROM tblIntervals WHERE @num>val1 AND @num<val2;