我有表adv_days(adv_date date not null primary key, edit_state integer)
,我想执行一系列命令:检查表中是否存在'27 .11.2019',如果存在,则将edit_state更新为1,如果不存在,则使用以下命令插入日期'27 .11.2019' edit_state = 1。
我想出了Firebrid合并声明:
merge into adv_days d
using (select adv_date, edit_state from adv_days where adv_date='27.11.2019') d2
on (d.adv_date=d2.adv_date)
when matched then update set d.edit_state=1
when not matched then
insert(adv_date, edit_state)
values(:adv_date, 1);
它可以按预期进行编译和工作,但是我为合并而阅读的所有示例都是关于将应用到2个表的。因此-也许对于一个表的选择检查更新操作比合并有更好的解决方案?
这是设计问题,但由于如此具体,因此SO比软件工程更合适。
有一个社区回应,对于一张表的插入/更新的merge
是完全有效的解决方案。但是,当然update or insert
是更优雅的解决方案,例如在这种情况下,代码为: