我想删除记录的写程序,我将传递给程序表名和ID。
CREATE OR REPLACE PROCEDURE TRYNIMAS(
ID NUMBER,
LENTELE VARCHAR2,
KLAIDA OUT VARCHAR2
)
IS
INUSE EXCEPTION;
PRAGMA EXCEPTION_INIT (INUSE,-02292 );
BEGIN
IF LENTELE = 'table1' THEN
DELETE FROM KATEGORIJOS WHERE KAT_ID = ID;
ELSE IF LENTELE = 'table2' THEN
DELETE FROM KATEGORIJOS WHERE PRT_ID = ID;
ELSE IF LENTELE = 'table3' THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
ELSE IF LENTELE = 'table4' THEN
DELETE FROM KATEGORIJOS WHERE PR_ID = ID;
ELSE IF LENTELE = 'table5' THEN
DELETE FROM KATEGORIJOS WHERE PIRK_ID = ID;
ELSE IF LENTELE = 'table6' THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
ELSE
KLAIDA:= 'TABLE OR RECORD NOT EXIST';
END IF;
EXCEPTION
WHEN INUSE THEN KLAIDA:= 'Record is in usera and can not be deleted';
WHEN OTHERS THEN KLAIDA:= 'Error!';
END;
我收到编译错误:
错误(33,1):PLS-00103:遇到以下其中一项时遇到符号“EXCEPTION”:(如果使用&lt,循环mod为空pragma raise返回选择更新,则为goto开始情况声明结束退出;<继续关闭当前删除提取锁插入打开回滚保存点设置sql执行提交forall合并管道清除
错误(36,4):PLS-00103:遇到以下其中一项时遇到符号“文件结束”:end not pragma final instantiable order overriding static member constructor map < / p>
答案 0 :(得分:2)
我建议用CASE声明替换IF ... ELSIF ... ELSIF:
CREATE OR REPLACE PROCEDURE TRYNIMAS
(ID IN NUMBER,
LENTELE IN VARCHAR2,
KLAIDA OUT VARCHAR2)
IS
INUSE EXCEPTION;
PRAGMA EXCEPTION_INIT (INUSE,-02292 );
BEGIN
CASE LENTELE
WHEN table1 THEN
DELETE FROM KATEGORIJOS WHERE KAT_ID = ID;
WHEN table2 THEN
DELETE FROM KATEGORIJOS WHERE PRT_ID = ID;
WHEN table3 THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
WHEN table4 THEN
DELETE FROM KATEGORIJOS WHERE PR_ID = ID;
WHEN table5 THEN
DELETE FROM KATEGORIJOS WHERE PIRK_ID = ID;
WHEN LENTELE = table6 THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
ELSE
KLAIDA:= 'TABLE OR RECORD NOT EXIST';
END CASE;
EXCEPTION
WHEN INUSE THEN
KLAIDA:= 'Record is in usera and can not be deleted';
WHEN OTHERS THEN
KLAIDA:= 'Error!';
END TRYNIMAS;
我仍然不知道这是否会编译,因为我不知道table1,table2等被声明为 - 是字符串还是数据库中的表? (如果它们是字符串,则可能会编译。如果它们是数据库中的表,则必须执行不同的操作。)
分享并享受。
答案 1 :(得分:1)
将所有ELSE IF
替换为ELSIF
。
我已将您的程序格式化为显示Oracle正在解释它:
CREATE OR REPLACE PROCEDURE TRYNIMAS(
ID NUMBER,
LENTELE VARCHAR2,
KLAIDA OUT VARCHAR2
)
IS
INUSE EXCEPTION;
PRAGMA EXCEPTION_INIT (INUSE,-02292 );
BEGIN
IF LENTELE = table1 THEN
DELETE FROM KATEGORIJOS WHERE KAT_ID = ID;
ELSE
IF LENTELE = table2 THEN
DELETE FROM KATEGORIJOS WHERE PRT_ID = ID;
ELSE
IF LENTELE = table3 THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
ELSE
IF LENTELE = table4 THEN
DELETE FROM KATEGORIJOS WHERE PR_ID = ID;
ELSE
IF LENTELE = table5 THEN
DELETE FROM KATEGORIJOS WHERE PIRK_ID = ID;
ELSE
IF LENTELE = table6 THEN
DELETE FROM KATEGORIJOS WHERE TK_ID = ID;
ELSE
KLAIDA:= 'TABLE OR RECORD NOT EXIST';
END IF;
EXCEPTION
WHEN INUSE THEN KLAIDA:= 'Record is in usera and can not be deleted';
WHEN OTHERS THEN KLAIDA:= 'Error!';
END;
在这种情况下,您可以看到它抱怨,因为它在EXCEPTION
个语句中有IF
。
您当然可以通过添加所有丢失的END IF
来修复您的程序,但只是使用ELSIF
而不是ELSE IF
更简单。
答案 2 :(得分:0)
一旦你选择了合理的语法 - 你仍然需要
添加显式提交 - 如果未通过调用进程处理提交。
解决了对其他人不当使用的问题
其他时候KLAIDA:='错误!';
这不应该用于处理预期的数据错误。这是因为系统错误和意外的数据问题,应该导致程序失败。
WHEN OTHERS
THEN
ROLLBACK;
KLAIDA:= 'Error!';
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_STACK);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END;