-1

I am new to PL/SQL and I need your help..

I was created student table named(students) and inserted 10 rows successfully. I need to create a block when the user enter the student ID should be show the all details which match with the entered student ID.

SET SERVEROUTPUT ON;
ACCEPT inputID PROMPT 'Please enter the student ID to show the details'
DECLARE
B_students students%ROWTYPE;
BEGIN 
  SELECT *
  INTO B_students
  FROM students
  WHERE B_students.studentID= inputID;

dbms_output.put_line('Student details : ID:'
                     ||B_students.studentID
                     ||'Student Name'
                     ||B_students.studentName
                     ||'Student GPA'
                     ||B_students.studentGPA
                     ||'Student AGE'
                     ||B_students.studentAGE
                     ||'Student Gender'
                     ||B_students.studentGender);

END;

The error report : "inputID": invalid identifire PL/SQL: SQL Statment ignored 06550.00000 -"line %s, column %s:\n%s" *cause: usually a PL/SQL compilation error

2 Answers2

1

The problem is that the identifier inputID is declared in SQL*Plus and your PL/SQL block cannot directly see it. To use it you need to precede it with an & character:

SET SERVEROUTPUT ON;
ACCEPT inputID PROMPT 'Please enter the student ID to show the details'

DECLARE
  B_students students%ROWTYPE;
BEGIN 
  SELECT *
    INTO B_students
    FROM students
    WHERE B_students.studentID = &inputID;

  dbms_output.put_line('Student details : ID:'
                       ||B_students.studentID
                       ||'Student Name'
                       ||B_students.studentName
                       ||'Student GPA'
                       ||B_students.studentGPA
                       ||'Student AGE'
                       ||B_students.studentAGE
                       ||'Student Gender'
                       ||B_students.studentGender);

END;
/

Note the use of &inputID instead of inputID in the WHERE clause of the SELECT statement.

0

A function containing SYS_REFCURSOR might be created :

CREATE OR REPLACE FUNCTION get_std_rs( i_ID students.studentID%TYPE ) 
                    RETURN SYS_REFCURSOR IS
  v_recordset SYS_REFCURSOR;
  v_sql       VARCHAR2(32767);
BEGIN
  v_sql := 'SELECT *
              FROM students
             WHERE studentID = :inputID';

  OPEN v_recordset FOR v_sql USING i_ID;
  RETURN v_recordset;
END;
/

and then invoked from SQL Developer's Command Line :

VAR rc REFCURSOR
EXEC :rc := get_std_rs(123);
PRINT rc;
  • INTO Clause cannot be used for multiple returned rows, but one row only
  • Through the above method, no need to specify all returning columns individually
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55