德比中的条件语句

发布于 2024-08-22 18:54:37 字数 378 浏览 6 评论 0原文

看起来 derby 不支持条件语句 [IF] 。我们如何

if exists (select 1 from customers where name='somename') 
    update customers ...
else 
    insert into customers ...

在德比战中写作? Derby 也没有 mysql 的“替换为”。

注意:我使用 derby 作为 mysql 的单元测试替代品 [即生产将使用 mysql,单元测试将使用 derby]。

It looks like derby doesn't support conditional statements [IF]. How do we write

if exists (select 1 from customers where name='somename') 
    update customers ...
else 
    insert into customers ...

in derby? Derby doesn't have 'replace into' of mysql either.

Note: I am using derby as unit-testing replacement for mysql [that is production will use mysql and unit-test will use derby].

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

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

发布评论

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

评论(2

预谋 2024-08-29 18:54:37

http://db.apache.org/derby/ 怎么样? docs/10.2/ref/rrefcasenullif.html#rrefcasenullif

   CASE
      WHEN 1 = 2 THEN 3
      WHEN 4 = 5 THEN 6
      ELSE 7
   END

所以也许你可以尝试这样的事情:

CASE
 WHEN  select 1 from customers where name='somename' = 1 THEN update...
ELSE  insert...
END

我不知道这是否有效,但这似乎是一个开始。祝你好运!

编辑:尝试了其中一些方法后...我不知道这是否真的对您有帮助。看起来你不能在 SELECT 和 INSERT 之间切换;必须是其中之一,然后 CASE 就会进入。你想做的事可能实现,也可能不可能......

What about http://db.apache.org/derby/docs/10.2/ref/rrefcasenullif.html#rrefcasenullif?

   CASE
      WHEN 1 = 2 THEN 3
      WHEN 4 = 5 THEN 6
      ELSE 7
   END

So maybe you can try something like:

CASE
 WHEN  select 1 from customers where name='somename' = 1 THEN update...
ELSE  insert...
END

I have no idea if that would work but it seems like a start. Good luck!

edit: After trying a few of these things out...I don't know if this will really help you. It doesn't seem like you can fire switch between SELECT and INSERT; it has to be one or the other and the CASE goes inside. What you want to do may or may not be possible...

贱人配狗天长地久 2024-08-29 18:54:37

我知道这可能为时已晚,但以防万一有人仍在寻找它:

MERGE

https: //issues.apache.org/jira/browse/DERBY-3155

例如:

MERGE INTO 客户 USING SYSIBM.SYSDUMMY1 ONcustomers.name='somename'
匹配后更新 SET name = 'someothername', ...
当不匹配时插入(id, name, ...) VALUES (DEFAULT, 'someothername', ...)

I know this might be too late but in case anyone is still looking for it:

MERGE

https://issues.apache.org/jira/browse/DERBY-3155

For Example:

MERGE INTO customers USING SYSIBM.SYSDUMMY1 ON customers.name='somename'
WHEN MATCHED THEN UPDATE SET name = 'someothername', ...
WHEN NOT MATCHED THEN INSERT(id, name, ...) VALUES (DEFAULT, 'someothername', ...)

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