将MySQL存储过程移植到Oracle

将MySQL存储过程移植到Oracle,mysql,oracle,stored-procedures,plsql,Mysql,Oracle,Stored Procedures,Plsql,我试图将一个存储过程从MySQL移植到Oracle,但遇到了很多麻烦。我查阅了Oracle文档,但在正确声明变量等基本操作上遇到了问题。我希望有人能告诉我如何正确地声明和设置变量 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following: := . ( @ % ; not null range default character. 我的存储过程用于向两个不同的表添

我试图将一个存储过程从MySQL移植到Oracle,但遇到了很多麻烦。我查阅了Oracle文档,但在正确声明变量等基本操作上遇到了问题。我希望有人能告诉我如何正确地声明和设置变量

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
我的存储过程用于向两个不同的表添加值,并确保它被正确映射,并且外键没有被违反

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
以下是我的MySQL代码:

 CREATE DEFINER=root@% PROCEDURE proc_add_entry(IN theName vARCHAR(50), IN theKey VARCHAR(50), IN theOtherData VARCHAR(50), IN theOtherData2 INT, IN theStartDate DATE, IN theEndDate DaTE, IN theReferenceDate DaTE)
     LANGUAGE SQL
     NOT DETERMINISTIC
     CONTAINS SQL
     SQL SECURITY DEFINER
 BEGIN
 declare theNameID int ;
 declare theKeyID int ;
 declare theOtherDataID int default null;
 declare error bool default false;
 declare continue handler for SQLEXCEPTION
    set error = true;

    set theKeyID = (select KeyID from map_alias ma where ma.alias = trim(theKey));
    set theOtherDataID = (select theOtherDataID from map_otherdata mc where mc.otherdata = trim(theOtherData));

    set theNameID = (select max(nameID) from inserttable);
    set theNameID = theNameID + 1;
    insert into inserttable values (theNameID , theKeyID , theOtherDataID , theOtherData2, theStartDate , 
    theEndDate , theReferenceDate);

    if error = true then
        insert into errors_inserttable values (theNameID , theKeyID , theOtherDataID , theOtherData2, theStartDate , 
    theEndDate , theReferenceDate);
    end if;

    set error = false;
    insert into map_inserttable (theNameID , datasourceid, theName) values (theNameID , 1, theName);
    if error = true then
        insert into errors_map_inserttable  (theNameID , datasourceid, theName) values (theNameID , 1, theName);
    end if;

 END
 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
在Oracle中,我的最后一条语句被忽略(ORA-00922:缺少或无效选项)。它应该是一个局部变量,所以我不确定为什么会出现这种特殊的错误

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
我也在努力声明continue处理程序。我得到了一个错误:

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
以下是迄今为止我的oracle代码:

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
 CREATE OR REPLACE PROCEDURE PROC_ADD_ENTRY
 (
   THENAME IN VARCHAR2  
 , THEKEY IN VARCHAR2  
 , THEOTHERDATA IN VARCHAR2  
 , THEOTHERDATA2 IN NUMBER  
 , THEFIRSTDATE IN DATE  
 , THELASTDATE IN DATE  
 , THEREFERENCEDATE IN DATE  
 ) AS
  THENAMEID INT;
  THEKEYID INT;
  THEOTHERDATAID int;
  ERROR bool default false;
 BEGIN
 declare continue HANDLER FOR SQLEXCEPTION set error = true;



   set THEKEYID = (select KEYID from map_INSERTTABLE mc where mc.Key = trim(THEKEY));
 END PROC_ADD_ENTRY;
我相信这对于使用oracle的人来说是非常简单的,但是我正在阅读文档,我看到了关于在何处以及如何声明变量、继续处理程序以及为变量赋值的相互冲突的信息。(赋值是:=还是=?我是在begin语句之后使用declare这个词来声明变量,还是按照下面的方式来执行?)

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
如果有人能告诉我:

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
a) 在哪里声明局部变量

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
b) 如何给它赋值(即1给int)

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
c) 如何将DB中的值分配给变量(设置变量=从表中选择数字,其中tn.number=1)

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
d) 如何正确声明continue处理程序

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.

我非常感谢。

您的基本结构很好

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
create or replace procedure <name> (<param list>) as
  <local variables>
begin
  <body>
end <name>;
d) 如何正确声明continue处理程序

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
如果我正确理解了您的代码,那么您希望每次sql语句出现问题时都执行
set error=true
,然后希望存储过程继续运行

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.
这就是你想要的。您可以将您认为可能有错误的任何SQL或PL/SQL语句包装在这样一个异常块中,并根据需要使用尽可能多的异常情况(例如,找不到数据):

 Error(16,27): PLS-00103: Encountered the symbol "FOR" when expecting one of the following:     := . ( @ % ; not null range default character.