为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:v8
【操作系统】:centos7
【CPU】:
【问题描述】*:我有一句sql语句
INSERT INTO "auth_location" (location, code)
VALUES
('page.admin.system.license.opt.replace','F00-001-002'),
('page.agent.opt.monitor','R00-004-002');
现在这个sql语句会在程序启动时被执行,但是如果已经被执行过,就会报错。如何适当的修改这个sql,达到程序每次重启执行都不会出错?
表中location是主键的话。
方法1,insert 时候对数据进行判断:INSERT INTO auth_location (location, code)
select * from
(select 'page.admin.system.license.opt.replace' location ,'F00-001-002' code from dual union all
select 'page.agent.opt.monitor' location,'R00-004-002' code from dual ) b where b.location not in
(select location from auth_location);
方法2,使用mere into 语法进行匹配判断:
merge into auth_location a
using (select 'page.admin.system.license.opt.replace' location ,'F00-001-002' code from dual union all
select 'page.agent.opt.monitor' location,'R00-004-002' code from dual ) b
on(a.location=b.location )
when matched then update set a.code=b.code
when not matched then insert (a.location, a.code)values(b.code,b.code);
建议方法使用方法1