gpt4 book ai didi

sql - 在存在条件sql server中设置变量值

转载 作者:行者123 更新时间:2023-12-02 03:27:04 25 4
gpt4 key购买 nike

Declare @CategoryID as int
BEGIN
SELECT
(CASE
WHEN EXISTS(
SELECT t0.Categoryid AS [EMPTY]
FROM Categories AS [t0]
WHERE [t0].Categoryname = @CategoryName
) THEN 1
ELSE 0
END) AS [value]

我想使用 t0.Categoryid 在存在 block 内设置我的变量。怎么才能做到这一点?

我想将 then 1 替换为类别 ID 值...

最佳答案

Declare @CategoryID as int
SET @CategoryID = CASE WHEN EXISTS(SELECT 1
FROM Categories
WHERE Categoryname = @CategoryName)
THEN 1 ELSE 0
END

另一种方式是......

IF EXISTS (SELECT 1
FROM Categories
WHERE Categoryname = @CategoryName)
BEGIN
SET @CategoryID = 1;
END
ELSE
BEGIN
SET @CategoryID = 0;
END

关于sql - 在存在条件sql server中设置变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22321749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com