我正在编写一个存储过程,我想检查范围内的可用数字。存储过程将返回0或1作为位。
案例0表示该号码不可用
案例1表示该号码可用。
我写了这个查询
Declare @recivedNo As INT = 82
Declare @minSourceNo as INT
Declare @maxSourceNo as INT
Select @minSourceNo = min(Source_no) from Source
Select @maxSourceNo = max(Source_no) from Source
IF(Select COunt(1) from Source
WHere Source_no = @recivedNo AND @recivedNo Between @minSourceNo AND @maxSourceNo
)>0
begin
Select 0 as is_valid
end
else
Select 1 as is_valid
假设最小数为1且最大值为88.可用数字为5。 如果我检查数字10.它重新运行0这是正确的。如果我检查5号,则返回1,这很好。请注意,数字5和10都在该范围内。但如果我检查数字100.它也返回1,这对我来说是不正确的。它应该返回0,因为100不在范围内。有没有办法检查这个?
答案 0 :(得分:2)
您在“表格中以及最小值和最大值之间”的评估不正确。您对该号码是否可用感兴趣(即“不在表格中以及在最小值和最大值之间”)。
select case when @recivedNo between @minSourceNo and @maxSourceNo
and not exists (select * from source where source_no = @recivedNo)
then 1 else 0 end as is_valid;
反之亦然:
select case when @recivedNo not between @minSourceNo and @maxSourceNo
or exists (select * from source where source_no = @recivedNo)
then 0 else 1 end as is_valid;
答案 1 :(得分:0)
看起来你的if语句是个问题。试试"如果存在"如下
$_SERVER["HTTP_REFERER"]
如果存在,您也可以使用select * inside或从源
中选择top 1IF EXISTS (Select count(1) from Source
WHere Source_no = @recivedNo AND (@recivedNo Between @minSourceNo AND @maxSourceNo)
)
BEGIN
END
ELSE
BEGIN
END
答案 2 :(得分:0)
您可以将@minSourceNo
和@maxSourceNo
放在一个查询中。在case语句中使用between
:
DECLARE @recivedNo As INT = 10,
@minSourceNo as INT,
@maxSourceNo as INT
SELECT @minSourceNo = min(Source_no),
@maxSourceNo = max(Source_no)
FROM @source
IF (
SELECT COUNT(*)+CASE WHEN @recivedNo Between @minSourceNo AND @maxSourceNo THEN 1 ELSE 0 END
FROM @source
WHERE Source_no = @recivedNo
) != 1
SELECT 0 as is_valid
ELSE
SELECT 1 as is_valid
答案 3 :(得分:0)
假设@minSourceNo,@ maxSourceNo可以有任意值
-- sample data - params
declare @Source table (sourceNo int);
insert @Source (sourceNo)
values (2),(25),(120);
Declare @minSourceNo as INT = 20;
Declare @maxSourceNo as INT = 50;
-- sample data
Declare @recivedSamples table (recivedNo int);
insert @recivedSamples(recivedNo)
values (2),(25),(30),(120);
-- query
select recivedNo,
flag=coalesce((select 1 n
from @Source s
where s.sourceNo between @minSourceNo and @maxSourceNo
and s.sourceNo = r.recivedNo), 0)
from @recivedSamples r
答案 4 :(得分:0)
由于您从@minSourceNo
表本身获取变量@maxSourceNo
和source
的值,因此查询将从该表返回SourceNo
的最小值和最大值。所以条件@recivedNo Between @minSourceNo AND @maxSourceNo
将始终满足,除非并且直到@recivedNo
表中没有输入Source
。
无论如何,下面的脚本会做同样的事情,但使用EXIST
。
DECLARE @recivedNo INT = 82
DECLARE @minSourceNo INT
DECLARE @maxSourceNo INT
SELECT @minSourceNo = min(Source_no), @maxSourceNo = max(Source_no) FROM Source
IF EXISTS (SELECT 1 FROM Source
WHERE Source_no = @recivedNo AND @recivedNo Between @minSourceNo AND @maxSourceNo)
BEGIN
SELECT 1 AS is_valid
END
ELSE
BEGIN
SELECT 0 AS is_valid
END
答案 5 :(得分:0)
我写了这个查询,它运行正常。
Declare @number as INT = 120
Declare @minsourceNo as INT
Declare @maxsourceNo as INT
Select @minsourceNo = min(source_no) from source
Select @maxsourceNo = max(source_no) from source
IF EXists(Select * from source where source_no = @number)
Begin
select Cast(0 as bit)
end
else
If(@number between @minsourceNo and @maxsourceNo)
Begin
select Cast(1 as bit)
end
else
select Cast(0 as bit)