今天我在做一個項目的時候,需要向資料庫(資料庫是Oracle 8.0.5)一次同時插入多條資料,但是程式運行卻出錯:ORA-00911: 無效字元。SQL語句如下:
DELETE FROM SYSROLEOPENR WHERE ROLEID = 2;
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090010';
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090020';
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090050';
我去掉所有的;號,也報錯。困惑了許久。如果資料庫是SQL SERVER 2000,程式去掉分號後是可以正常啟動並執行。後來發現在Oracle中需要加上begin
end才正確。改成以下的SQL就正確了:
BEGIN
DELETE FROM SYSROLEOPENR WHERE ROLEID = 2;
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090010';
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090020';
INSERT INTO SYSROLEOPENR
(PKID, ROLEID, OPERID)
SELECT SYSROLEOPENR_PKID_SEQ.NEXTVAL, '2' ROLEID, OPERID
FROM SYSMODULEOPER
WHERE OPERCODE = '300090050';
END;