[英]Reusing Value of a subquery in Else clause
is there any way i can use the value of X in the if clause in the else clause有什么办法可以在 else 子句的 if 子句中使用 X 的值
if @bid_value>(SELECT MAX(bid_value) AS X FROM dbo.auctionDetails WHERE status = 0 AND vehicle_id=@vid GROUP BY vehicle_id)
BEGIN
//some code
END
ELSE IF X=NULL
BEGIN
//some code
END
You would typically assign the results of the query to a variable.您通常会将查询结果分配给变量。
If you are running SQL Server:如果您正在运行 SQL 服务器:
DECLARE @max_bid_value INT;
SELECT @max_bid_value = MAX(bid_value)
FROM dbo.auctionDetails
WHERE status = 0 AND vehicle_id = @vid;
IF @bid_value > @max_bid_value
BEGIN
//some code
END
...
Note that I removed the GROUP BY
clause from the original query - I think that this makes it clearer that it should always return a scalar value.请注意,我从原始查询中删除了GROUP BY
子句 - 我认为这更清楚地表明它应该始终返回一个标量值。
Note that if you want to check if a variable is null
, you need @bid is null
rather than @bid = null
.请注意,如果要检查变量是否为null
,则需要@bid is null
而不是@bid = null
。
Why don't you create a var for the value?为什么不为值创建一个 var?
DECLARE @maxValue INT
SELECT @maxValue = MAX(bid_value)
FROM dbo.auctionDetails
WHERE status = 0 AND vehicle_id = @vid
IF (@bid_value > @maxValue)
BEGIN
// some code
END
ELSE IF (@maxValue IS NULL)
BEGIN
// some code
END
A comment about how you are getting the MAX value: As you are filtering by vehicle_id = @vid, you don't need GROUP BY clause since you will get results for only one value of vahicle_id关于如何获得 MAX 值的评论:当您通过 vehicle_id = @vid 进行过滤时,您不需要 GROUP BY 子句,因为您只会获得一个 vahicle_id 值的结果
Your query should be like below:您的查询应如下所示:
DECLARE @X INT
SET @X = (SELECT MAX(bid_value) AS X FROM dbo.auctionDetails WHERE status = 0 AND vehicle_id=@vid);
IF @bid_value>@MAX
BEGIN
//some code
END
ELSE IF X=NULL
BEGIN
//some code
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.