0

I'm trying to write a procedure. When we query this procedure with id no,
the tables will be joined and the results will be as follows;
(PROCEDURE_NAME: "student_information")
id_no, name, surname, school_number, department_information, city, lesson


Here are my tables and procedure code;

CREATE TABLE student_info (
    school_number NUMBER,
    id_no NUMBER NOT NULL UNIQUE,
    name VARCHAR2(50) NOT NULL,
    surname VARCHAR2(50) NOT NULL,
    city VARCHAR2(50) NOT NULL,
    birth_date DATE NOT NULL,
    CONSTRAINT student_info_pk PRIMARY KEY (okul_numarasi)
);
CREATE TABLE school_info (
    school_number NUMBER,
    entry_date DATE NOT NULL,
    faculty_info VARCHAR2(50) NOT NULL,
    department_information VARCHAR2(50) NOT NULL,
    CONSTRAINT school_info_pk PRIMARY KEY (school_number),
    CONSTRAINT student_school_fk FOREIGN KEY (school_number)      
    REFERENCES student_info(school_number)
);
CREATE TABLE lessons(
     school_number NUMBER,
     lesson_name VARCHAR2(100) NOT NULL,
     lesson_number NUMBER NOT NULL,
     midterm_1 NUMBER,
     midterm_2 NUMBER,
     final_note NUMBER,
     integration_note NUMBER, 
    CONSTRAINT lessons_pk PRIMARY KEY (school_number),
    CONSTRAINT lessons_student_fk FOREIGN KEY (school_number)     
    REFERENCES ogrenci_bilgileri (okul_numarasi)
);

CREATE OR REPLACE PROCEDURE 
      student_information(
                     p_no                     IN  student_info.id_no%type,
                     p_name                   OUT student_info.name%type,
                     p_surname                OUT student_info.surname%type,
                     p_school_number          OUT student_info.school_number%type,
                     p_department_information OUT school_info.department_information%type,
                     p_city                   OUT student_info.city%type,
                     p_lesson                 OUT lessons.lesson_name%type
                   ) AS
BEGIN
  SELECT o.name,
         o.surname,
         o.school_number,
         ok.department_information,
         o.city,
         d.lesson_name
    INTO p_name,
         p_surname,
         p_school_number,
         p_city,
         p_department_information,
         p_lesson
    FROM student_info o
    JOIN school_info ok
      ON o.school_info = ok.school_number
    JOIN lessons d
      ON d.school_number = ok.school_number;
   WHERE o.id_no = p_no;
END student_information;

And here is my declare to run the procedure.

DECLARE 
    v_id student_info.id_no%type:= 12345;      
    v_name student_info.name%type;      
    v_surname student_info.surname%type;      
    v_school_num student_info.school_number%type;      
    v_department school_info.department_information%type;      
    v_city student_info.city%type;      
    v_lesson lessons.lesson_name%type;
BEGIN
    student_information(v_id,v_name,v_surname,v_school_num,v_department,v_city, v_lesson );
    DBMS_OUTPUT.put_line ('Student Information');
    DBMS_OUTPUT.put_line ('ID: ' || v_id);
    DBMS_OUTPUT.put_line ('Name: ' || v_name || ' ' || v_surname);
    DBMS_OUTPUT.put_line ('School Number: ' || v_school_num);
    DBMS_OUTPUT.put_line ('Department Information: ' || v_department);
    DBMS_OUTPUT.put_line ('City: ' || v_city);
    DBMS_OUTPUT.put_line ('Lesson Name:' || v_lesson);
END;

The error is:

ORA-06502: PL / SQL: numerical or value error: character-to-number error
ORA-06512: location "SYSTEM.student_information", line 12
ORA-06512: location line 10
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    An arithmetic, numeric, string, conversion, or constraint error
           occurred. For example, this error occurs if an attempt is made to
           assign the value NULL to a variable declared NOT NULL, or if an
           attempt is made to assign an integer larger than 99 to a variable
           declared NUMBER(2).
*Action:   Change the data, how it is manipulated, or how it is declared so
           that values do not violate constraints.
4

1 回答 1

2

Simple mismatch on your columns

  SELECT o.name,
         o.surname,
         o.school_number,
         ok.department_information,   <===
         o.city,                      <===
         d.lesson_name
    INTO p_name,
         p_surname,
         p_school_number,
         p_city,                      <===
         p_department_information,    <===
         p_lesson

but before you slap your head and think you've wasted your time, the changes you have made (to using %TYPE etc) have made your code so much more robust and maintainable.

于 2020-07-29T07:26:08.653 回答