Oracle SQL存储过程游标打印

时间:2013-05-06 22:15:40

标签: sql database oracle

好的,所以我正在使用存储过程完成家庭作业。

基本上我只是尝试使用存储过程运行查询然后打印结果。

这是我到目前为止所拥有的。

create or replace procedure movie_actors (mtitle varchars)as
DECLARE
CURSOR c1 is SELECT "NAME",GENDER,ADDRESS FROM MOVIESTAR WHERE "NAME" in(
SELECT STARNAME FROM STARSIN WHERE MOVIETITLE=mtitle);
actor_name MOVIESTAR.NAME%TYPE;
actor_gender MOVIESTAR.NAME%TYPE;
actor_address MOVIESTAR.ADDRESS%TYPE;
BEGIN
LOOP
   FETCH c1 INTO actor_name;
   FETCH c1 INTO actor_gender;
   FETCH c1 INTO actor_address;
   EXIT WHEN c1%NOTFOUND;
   DBMS_OUTPUT.PUT_LINE(mtitle ||', '||actor_name||': '||actor_gender||', '||actor_address); 
END LOOP;
END;

我是数据库和存储过程的新手。我不确定我是否真的会采用这种方法。

应该很简单,我不确定我做错了什么。

这是我得到的编译器错误。

Error(2,1): PLS-00103: Encountered the symbol "DECLARE" when expecting one of the   
following:     begin function pragma procedure subtype type <an identifier>    <a   
double-quoted delimited-identifier> current cursor delete    exists prior external 
language The symbol "begin" was substituted for "DECLARE" to continue. 

Error(16,4): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the
following:     ( begin case declare end exception exit for goto if loop mod    
null pragma raise return select update while with    <an identifier> <a double-quoted 
delimited-identifier>    <a bind variable> << continue close current delete fetch lock 
insert open rollback savepoint set sql execute commit forall    merge pipe purge 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

首先,您的类型无效:

create or replace procedure movie_actors (mtitle varchars)as
                                                        ^

这应该是varchar2,而不是varchars

其次,您不需要DECLARE。 “as”有点替代它。像这样开始你的过程:

create or replace procedure movie_actors (mtitle varchar2)as
  CURSOR c1

最后,我强烈建议更改此内容:

CURSOR c1 is SELECT "NAME",GENDER,ADDRESS FROM MOVIESTAR WHERE "NAME"

......对此(没有双引号):

CURSOR c1 is SELECT NAME,GENDER,ADDRESS FROM MOVIESTAR WHERE NAME

双引号会使列名称区分大小写。在这种情况下你很幸运,因为Oracle中的默认值是大写的,但是迟早会使用这样的双引号会给你带来麻烦 - 有很多StackOverflow帖子来自沮丧的用户,他们在使用双引号时丢失了数小时或时间他们没有必要。