PL / SQL存储过程

时间:2015-10-31 05:42:31

标签: sql oracle stored-procedures plsql

请帮我解释一下这个存储过程的语法?这是在oracle的SQL开发人员中完成的。

存储过程的目的是允许CALL函数将新记录插入CLASS表(使用下面的语法创建):

CREATE TABLE class
(
class_number NUMBER(8), 
teacher_name VARCHAR2(50),
class_name VARCHAR2(50) NOT NULL,
start_date DATE,
end_date DATE,
class_category VARCHAR2(20),
topic VARCHAR2(20),
teacher_credential VARCHAR2(40),
CONSTRAINT pk_class PRIMARY KEY (class_number,teacher_name,class_name,start_date)
);

使用下面编码的存储过程和序列,应该创建一个新类,如果结束日期在开始日期之前,那么结束日期应该是默认的开始日期+两周。

CREATE SEQUENCE class_number_seq;

 CREATE OR REPLACE PROCEDURE insert_class
(
  Teacher_name_param class.teacher_name%TYPE,
  class_name_param class.class_name%type,
  start_date_param class.start_date%TYPE ,
  end_date_param class.end_date%TYPE,
  class_category_param class.class_category%TYPE,
  topic_param class.topic%TYPE,
  end_date_param class.end_date%TYPE
  )
  AS 
      class_number_var class.class_number%TYPE;
      end_date_var class.end_number%TYPE;


BEGIN
   SELECT class_number_seq.NEXTVAL INTO class_number_var FROM dual;

   IF end_date_param < start_date_param OR end_date_param IS NULL THEN
     end_date_param := start_date_param + 14; 
    ELSE
      end_date_var := end_date_param;
    END IF;
   INSERT INTO class
   VALUES(class_number_var, teacher_name_param, class_name_param, start_date_param, end_date_param, class_category_param, topic_param);
    END;

错误我得到的是:

  

PL / SQL complilation unit analysis终止,并且:重复字段   不允许列出RECORD,TABLE或参数。

我不确定我的逻辑是否有任何问题 - 或重复的字段在哪里。

请帮忙!

1 个答案:

答案 0 :(得分:5)

end_date_param class.end_date%TYPE在过程参数列表中列出两次。删除其中一个应修复此错误。

  CREATE OR REPLACE PROCEDURE insert_class ( Teacher_name_param class.teacher_name%TYPE, class_name_param class.class_name%type, start_date_param class.start_date%TYPE , end_date_param class.end_date%TYPE, class_category_param class.class_category%TYPE, topic_param class.topic%TYPE, end_date_param class.end_date%TYPE )

我认为以下行有拼写错误

   end_date_var class.end_numberdate%TYPE;

以下语句可能会导致错误,因为end_date_param未声明为OUT参数。所以end_date_param在这里应该可以改为end_date_var

   IF end_date_param < start_date_param OR end_date_param IS NULL THEN end_date_paramvar := start_date_param + 14; ELSE

insert语句提供7个值,而表有8列。

INSERT INTO class
VALUES(class_number_var, teacher_name_param, class_name_param,
start_date_param, end_date_param, class_category_param, topic_param);

这可能导致值错误太少。

通常最好将其写为

INSERT INTO class
(class_number, teacher_name, class_name, 
 start_date, end_date, class_category, topic)
VALUES(class_number_var, teacher_name_param, class_name_param,
 start_date_param, end_date_param, class_category_param, topic_param);

因此我们可以轻松验证并避免错误。