简单 PL/SQL 程序的问题
这是我的简单 PL/SQL 程序:
DECLARE
CURSOR c1 is
SELECT typ, specifikacia_typu, spz FROM Auta
WHERE (substr(spz, 1, 2) = 'KE' OR substr(spz, 1, 2) = 'KS') AND ROWNUM <= 2;
CURSOR c2 is
SELECT typ, specifikacia_typu, spz FROM Auta
WHERE substr(spz, 1, 2) <> 'KE' AND substr(spz, 1, 2) <> 'KS';
my_typ CHAR(10);
my_specifikacia_typu CHAR(15);
my_spz CHAR(8);
BEGIN
-- vytovirt potrebne tabulky pre kosicke a nekosicke auta
CREATE TABLE Kosicke (
typ CHAR(10),
specifikacia_typu CHAR(15),
spz CHAR(8)
);
CREATE TABLE Ostatne (
typ CHAR(10),
specifikacia_typu CHAR(15),
spz CHAR(8)
);
-- prve dve auta z Kosic vlozit do tabulky Kosicke
OPEN c1;
FOR i IN 1..2 LOOP
FETCH c1 INTO my_typ, my_specifikacia_typu, my_spz;
EXIT WHEN c1%NOTFOUND;
INSERT INTO Kosice VALUES(my_typ, my_specifikacia_typu, my_spz);
COMMIT;
END LOOP;
CLOSE c1;
-- auta, ktore nie su z Kosic vlozit do tabulky Ostatne
OPEN c2;
LOOP
FETCH c2 INTO my_typ, my_specifikacia_typu, my_spz;
EXIT WHEN c2%NOTFOUND;
INSERT INTO Ostatne VALUES(my_typ, my_specifikacia_typu, my_spz);
COMMIT;
END LOOP;
CLOSE c1;
END;
/
当我在 Oracle 10g Express Edition 上运行它时,出现此错误:
ORA-06550: line 16, column 5:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin case declare exit for goto if loop mod null pragma
raise return select update while with
<<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe
1. DECLARE
2.
3. CURSOR c1 is
我不确定问题出在哪里,这是我用 PL/SQL 编写的第一个程序,所以我有点丢失的。我使用 Oracle 网站上的示例程序来编写此程序: http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/a_samps.htm#563
编辑:
此外,当我首先在程序外部创建表,然后运行程序,我收到此错误:
ORA-06550: line 17, column 21:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 17, column 9:
PL/SQL: SQL Statement ignored
1. DECLARE
2. CURSOR c1 is
3. SELECT typ, specifikacia_typu, spz FROM Auta
这没有意义,因为表“Auta”存在,程序中使用的所有表都存在。
Here is my simple PL/SQL program:
DECLARE
CURSOR c1 is
SELECT typ, specifikacia_typu, spz FROM Auta
WHERE (substr(spz, 1, 2) = 'KE' OR substr(spz, 1, 2) = 'KS') AND ROWNUM <= 2;
CURSOR c2 is
SELECT typ, specifikacia_typu, spz FROM Auta
WHERE substr(spz, 1, 2) <> 'KE' AND substr(spz, 1, 2) <> 'KS';
my_typ CHAR(10);
my_specifikacia_typu CHAR(15);
my_spz CHAR(8);
BEGIN
-- vytovirt potrebne tabulky pre kosicke a nekosicke auta
CREATE TABLE Kosicke (
typ CHAR(10),
specifikacia_typu CHAR(15),
spz CHAR(8)
);
CREATE TABLE Ostatne (
typ CHAR(10),
specifikacia_typu CHAR(15),
spz CHAR(8)
);
-- prve dve auta z Kosic vlozit do tabulky Kosicke
OPEN c1;
FOR i IN 1..2 LOOP
FETCH c1 INTO my_typ, my_specifikacia_typu, my_spz;
EXIT WHEN c1%NOTFOUND;
INSERT INTO Kosice VALUES(my_typ, my_specifikacia_typu, my_spz);
COMMIT;
END LOOP;
CLOSE c1;
-- auta, ktore nie su z Kosic vlozit do tabulky Ostatne
OPEN c2;
LOOP
FETCH c2 INTO my_typ, my_specifikacia_typu, my_spz;
EXIT WHEN c2%NOTFOUND;
INSERT INTO Ostatne VALUES(my_typ, my_specifikacia_typu, my_spz);
COMMIT;
END LOOP;
CLOSE c1;
END;
/
When I run it Oracle 10g Express Edition, I get this error:
ORA-06550: line 16, column 5:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin case declare exit for goto if loop mod null pragma
raise return select update while with
<<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe
1. DECLARE
2.
3. CURSOR c1 is
I'm not sure where the problem is and this is my first program I wrote in PL/SQL so I'm a little lost. I have used this example programs from Oracle website to write this program: http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/a_samps.htm#563
EDIT:
Also, when I create the tables outside the program first and then run the program, I get this error:
ORA-06550: line 17, column 21:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 17, column 9:
PL/SQL: SQL Statement ignored
1. DECLARE
2. CURSOR c1 is
3. SELECT typ, specifikacia_typu, spz FROM Auta
Which doesn't make sense because table "Auta" exists, all tables used in the program do exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能直接在 PL/SQL 中执行像 CREATE TABLE 这样的 DDL。但是,您可以使用动态 PL/SQL 来完成此操作,如下所示:
您的插入也需要是动态的,因为这些表在编译时不存在,因此代码无效:
了解为什么会很有趣不过,需要这样做:在 Oracle 中几乎不需要“动态”创建表,而且这样做通常不是一个好主意。
You cannot perform DDL like CREATE TABLE directly in PL/SQL. However, you can do it using dynamic PL/SQL like this:
Your inserts will also need to be dynamic, since the tables don't exist at compile time and so the code isn't valid:
It would be interesting to know why you need to do this, though: there is almost never any need to create tables "on the fly" in Oracle and it isn't generally a good idea to do so.
除了托尼已经解释过的语法问题之外,这段代码应该只是直接的 SQL 插入,没有任何游标。如果您确实需要游标,请在使用显式游标之前尝试使用隐式游标。
Aside from your syntax issue, which Tony has explained, this code ought to just be straight SQL inserts without any cursors. If you really did need a cursor then try to use an implicit one before using an explicit one.
好吧,只是让你知道我是如何解决这个问题的(在有人问之前我已经解决了)。
首先,我使用普通的 SQL 查询在程序外部创建了表:
然后我像这样编辑了程序(它已经可以工作了):
Ok, so just to let you know how I solved the problem (I have already solved it before anyone aswered).
First, I created the tables outside of the program with a normal SQL queries:
And I edited the program like this (it works already):