我希望在我的Oracle数据库中添加一条新记录,如果它已经存在则更新它。
我正在使用If-else子句来实现此目的。
以下是我的代码:
DECLARE
any_rows_found number;
BEGIN
select count(*)
into any_rows_found
from reports
where report_id=27;
IF(any_rows_found > 1)
THEN
UPDATE reports set
TITLE = 'Check user entered keywords have valid resource link',
CATEGORY = 'zData checks',
DISPLAY_ORDER= 9120,
QUERY ='...',
CONTENT_SEQ=1,
DELD=0,
ADMIN_ID=1,
DATE_UPD = to_timestamp('05-FEB-15 01.44.53.753311000 PM','DD-MON-RR HH.MI.SS.FF AM')
WHERE REPORT_ID = 27;
ELSE
INSERT INTO reports (REPORT_ID,TITLE,CATEGORY,DISPLAY_ORDER,QUERY,DESCRIPTION,CONTENT_SEQ,DELD,ADMIN_ID,DATE_UPD)
VALUES (27,
'Check user entered keywords have valid resource link',
'zData checks',
9120,
'...',
1,
0,
1,
to_timestamp('05-FEB-15 01.44.53.753311000 PM','DD-MON-RR HH.MI.SS.FF AM'));
END IF;
这会引发以下错误:
PLS-00103:遇到以下任何一种情况时遇到符号“文件结束”:
(如果循环mod,则为goto开始大小写声明结束异常退出 null pragma raise返回选择更新时使用 <<继续关闭当前删除获取锁定 insert open rollback savepoint set sql execute commit forall 合并管道清除 06550. 00000 - “行%s,列%s:\ n%s” *原因:通常是PL / SQL编译错误。
我哪里错了?请帮忙。
谢谢。
答案 0 :(得分:3)
你需要添加END;作为文件的最后一行,关闭顶部附近的BEGIN。
答案 1 :(得分:1)
答案 2 :(得分:1)
你错过了一个"结束;"在文件末尾。
但我必须说:你还应该看看Merge SQL DML Statment。 它确实是您想要的,并且直接从DBMS开发人员实现,因此我们可以建议它将优于自写PL / SQL脚本。
对于您的脚本,它将是:
MERGE INTO REPORTS old USING (select report_id = 27,
TITLE = 'Check user entered keywords have valid resource link',
CATEGORY = 'zData checks',
DISPLAY_ORDER= 9120,
QUERY ='...',
CONTENT_SEQ=1,
DELD=0,
ADMIN_ID=1,
DATE_UPD = to_timestamp('05-FEB-15 01.44.53.753311000 PM','DD-MON-RR HH.MI.SS.FF AM') FROM DUAL) new
ON (old.report_id = new.report_id)
when matched then update set
old.TITEL = new.TITLE,
old.CATEGORY = new.CATEGORY,
old.DISPLAY_ORDER = new.DISPLAY_ORDER,
old.QUERY = new.QUERY,
old.CONTENT_SEQ = new.CONTENT_SEQ,
old.DELD = new.DELD,
old.ADMIN_ID = new.ADMIN_ID,
old.DATE_UPD = new.DATE_UPD
when not matched then
insert (report_id, TITLE, CATEGORY, DISPLAY_ORDER, QUERY, CONTENT_SEQ, DELD, ADMIN_ID, DATE_UPD) values (new.report_id, new.TITLE, new.CATEGORY, new.DISPLAY_ORDER, new.QUERY, new.CONTENT_SEQ, new.DELD, new.ADMIN_ID, new.DATE_UPD)