CASE语句,在THEN部分分配多个值

发布于 2024-12-21 22:38:55 字数 432 浏览 1 评论 0原文

之前已经有人问过这个问题,但我不确定是否得到了明确的回答。如何在 CASE 语句的 THEN 部分中分配多个值。根据MySQL手册“每个statement_list由一个或多个语句组成;”,所以我想知道这是如何完成的。

我的 CASE 语句中有一个 THEN,并且想为两个变量赋值。所以类似 THEN @var1 := 'something', @var2 := 'something else' ESLE 'something more' END。

这可能吗?

This has been asked before but I'm not sure it was clearly answered. How do you assign multiple values in the THEN portion of a CASE statement. According to the MySQL manual "Each statement_list consists of one or more statements;", so I'm wondering how this is done.

I have a THEN in my CASE statement and would like to assign a value to two variables. So something like THEN @var1 := 'something', @var2 := 'something else' ESLE 'something more' END.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀子花开つ 2024-12-28 22:38:55

我不认为你能够在案例本身内部分配变量,除非 MySql 与 TSQL 不同,但这里是你如何完成你想要做的事情:

SELECT @va1 = CASE WHEN Some_condition THEN 'something' ELSE 'something else' END,
       @va1 = CASE WHEN Some_condition THEN 'something' ELSE 'something else' END

希望有帮助

I don't think you'll be able to assign variables inside of the case itself, unless MySql is that different from TSQL, but here is how you can accomplish what you are trying to do:

SELECT @va1 = CASE WHEN Some_condition THEN 'something' ELSE 'something else' END,
       @va1 = CASE WHEN Some_condition THEN 'something' ELSE 'something else' END

hope that helps

川水往事 2024-12-28 22:38:55

从您从文档中发布的链接中提取一些内容:

CREATE PROCEDURE procase()
BEGIN
    DECLARE var1 INT;
    DECLARE var2 INT;

    SET var1 = 1;
    SET var2 = 20;

    CASE var1
        WHEN 1000 THEN SELECT var1;
        WHEN 3000 THEN SELECT var1;
        ELSE
        BEGIN
        -- multiple statements; statement list right below
        -- performing multiple variable value setting
            SET var1 = var1 + 200;
            SET var2 = var2 + 200;
            SELECT var1, var2;
        END;
    END CASE;
END

将多个语句封装在 BEGIN...END.. 看看这是否有效

Pulling a bit from the link you posted from the docs:

CREATE PROCEDURE procase()
BEGIN
    DECLARE var1 INT;
    DECLARE var2 INT;

    SET var1 = 1;
    SET var2 = 20;

    CASE var1
        WHEN 1000 THEN SELECT var1;
        WHEN 3000 THEN SELECT var1;
        ELSE
        BEGIN
        -- multiple statements; statement list right below
        -- performing multiple variable value setting
            SET var1 = var1 + 200;
            SET var2 = var2 + 200;
            SELECT var1, var2;
        END;
    END CASE;
END

Encapsulate multiple statements within a BEGIN...END.. See if this works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文