Save returning value from a function to a variable : variable « SQL Plus « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » SQL Plus » variable 
Save returning value from a function to a variable
    
SQL>
SQL> CREATE TABLE myStudent (
  2    student_id NUMBER(5NOT NULL,
  3    department CHAR(3)   NOT NULL,
  4    course     NUMBER(3NOT NULL,
  5    grade      CHAR(1)
  6    );

Table created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'CS', 102'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'CS', 102'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10003'CS', 102'C');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'HIS', 101'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10001'HIS', 101'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'HIS', 101'B');

row created.

SQL> CREATE OR REPLACE FUNCTION AverageGrade (
  2    p_Department IN myStudent.department%TYPE,
  3    p_Course IN myStudent.course%TYPERETURN CHAR AS
  4    v_AverageGrade CHAR(1);
  5    v_NumericGrade NUMBER;
  6    v_NumberStudents NUMBER;
  7
  8    CURSOR c_Grades IS
  9      SELECT grade
 10        FROM myStudent
 11        WHERE department = p_Department
 12        AND course = p_Course;
 13  BEGIN
 14    SELECT COUNT(*)
 15      INTO v_NumberStudents
 16      FROM myStudent
 17      WHERE department = p_Department
 18        AND course = p_Course;
 19
 20    IF v_NumberStudents = THEN
 21      RAISE_APPLICATION_ERROR(-20001'No students registered for ' ||
 22        p_Department || ' ' || p_Course);
 23    END IF;
 24
 25    SELECT AVG(DECODE(grade, 'A'5,
 26                             'B'4,
 27                             'C'3,
 28                             'D'2,
 29                             'E'1))
 30      INTO v_NumericGrade
 31      FROM myStudent
 32      WHERE department = p_Department
 33      AND course = p_Course;
 34
 35    SELECT DECODE(ROUND(v_NumericGrade)5'A',
 36                                         4'B',
 37                                         3'C',
 38                                         2'D',
 39                                         1'E')
 40      INTO v_AverageGrade
 41      FROM dual;
 42
 43    RETURN v_AverageGrade;
 44  END AverageGrade;
 45  /

Function created.

SQL>
SQL>
SQL> VARIABLE v_AveGrade VARCHAR2(1)
SQL> EXEC :v_AveGrade := AverageGrade('HIS', 101)

PL/SQL procedure successfully completed.

SQL> PRINT v_AveGrade

V_AVEGRADE
--------------------------------
B

SQL> EXEC :v_AveGrade := AverageGrade('NUT', 307)
BEGIN :v_AveGrade := AverageGrade('NUT', 307)END;

*
ERROR at line 1:
ORA-20001: No students registered for NUT 307
ORA-06512: at "JAVA2S.AVERAGEGRADE", line 21
ORA-06512: at line 1


SQL> PRINT v_AveGrade

V_AVEGRADE
--------------------------------
B

SQL> EXEC :v_AveGrade := AverageGrade('MUS', 410)
BEGIN :v_AveGrade := AverageGrade('MUS', 410)END;

*
ERROR at line 1:
ORA-20001: No students registered for MUS 410
ORA-06512: at "JAVA2S.AVERAGEGRADE", line 21
ORA-06512: at line 1


SQL> PRINT v_AveGrade

V_AVEGRADE
--------------------------------
B

SQL> EXEC :v_AveGrade := AverageGrade('CS', 102)

PL/SQL procedure successfully completed.

SQL> PRINT v_AveGrade

V_AVEGRADE
--------------------------------
B

SQL>
SQL>
SQL>
SQL>
SQL> drop table myStudent;

Table dropped.

SQL>

   
    
    
  
Related examples in the same category
1. refcursor varible
2. Define variable and print its value out
3. Define variable and set value
4. Define variable and use it in sql statement
5. Bind variables can be declared in your SQL*Plus script,Preface a bind variable with a colon to reference it in a PL/SQL block
6. Bind variables can even be referenced by SQL queries
7. Undefine variable
8. User defined-variable in update set statement
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.