我試圖將存儲過程從MySQL移植到Oracle,並且遇到了很多麻煩。我已經閱讀了Oracle文檔,並且在做非常基本的事情時遇到了困難,例如正確地聲明變量。我希望有人能告訴我如何正確地聲明和設置變量。將MySQL存儲過程移植到Oracle
我的存儲過程用於將值添加到兩個不同的表中,並確保它正確映射並且外鍵沒有被違反。
這裏是我的MySQL代碼:
CREATE [email protected]% 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
在Oracle中,我的最後一條語句被忽略(ORA-00922:缺少或無效選項)。它應該是一個局部變量,所以我不確定爲什麼我會得到那個特定的錯誤。
我很努力地宣佈繼續處理程序。我發現了錯誤:
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有人太簡單,但我閱讀文檔而且我看到有關在何處以及如何聲明變量,繼續處理程序以及將值分配給變量的衝突信息。 (是:=或=賦值我用這個詞的開頭語句後聲明來聲明變量,或做我做,我在下面顯示的方式?)
如果有人能告訴我:
a),其中,以聲明一個局部變量
b)中如何將一個值到它(即1分配給一個int)
c)中如何從DB的值分配給變量(組VAR =從table_number中選擇數字tn,其中tn.number = 1)
d)如何聲明重新正確的繼續處理程序
我真的很感激它。