Oracle/PLSQL: Creating FunctionsIn Oracle, you can create your own functions.
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsThe syntax for a function is:
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsCREATE [OR REPLACE] FUNCTION function_name
Oracle/PLSQL: Creating Functions    [ (parameter [,parameter]) ]
Oracle/PLSQL: Creating Functions    RETURN return_datatype
Oracle/PLSQL: Creating FunctionsIS | AS
Oracle/PLSQL: Creating Functions    [declaration_section]
Oracle/PLSQL: Creating FunctionsBEGIN
Oracle/PLSQL: Creating Functions    executable_section
Oracle/PLSQL: Creating Functions[EXCEPTION
Oracle/PLSQL: Creating Functions    exception_section]
Oracle/PLSQL: Creating FunctionsEND [function_name];
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsWhen you create a procedure or function, you may define parameters. There are three types of parameters that can be declared:
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsIN - The parameter can be referenced by the procedure or function. The value of the parameter can not be overwritten by the procedure or function. 
Oracle/PLSQL: Creating FunctionsOUT - The parameter can not be referenced by the procedure or function, but the value of the parameter can be overwritten by the procedure or function. 
Oracle/PLSQL: Creating FunctionsIN OUT - The parameter can be referenced by the procedure or function and the value of the parameter can be overwritten by the procedure or function. 
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsThe following is a simple example of a function: 
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsCREATE OR REPLACE Function FindCourse
Oracle/PLSQL: Creating Functions   ( name_in IN varchar2 )
Oracle/PLSQL: Creating Functions   RETURN number
Oracle/PLSQL: Creating FunctionsIS
Oracle/PLSQL: Creating Functions    cnumber number;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions    cursor c1 is
Oracle/PLSQL: Creating Functions    select course_number
Oracle/PLSQL: Creating Functions      from courses_tbl
Oracle/PLSQL: Creating Functions      where course_name = name_in;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsBEGIN
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functionsopen c1;
Oracle/PLSQL: Creating Functionsfetch c1 into cnumber;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functionsif c1%notfound then
Oracle/PLSQL: Creating Functions     cnumber := 9999;
Oracle/PLSQL: Creating Functionsend if;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functionsclose c1;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsRETURN cnumber;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsEXCEPTION
Oracle/PLSQL: Creating FunctionsWHEN OTHERS THEN
Oracle/PLSQL: Creating Functions      raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
Oracle/PLSQL: Creating FunctionsEND;
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsThis function is called FindCourse. It has one parameter called name_in and it returns a number. The function will return the course number if it finds a match based on course name. Otherwise, it returns a 99999.
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating FunctionsYou could then reference your new function in an SQL statement as follows: 
Oracle/PLSQL: Creating Functions
Oracle/PLSQL: Creating Functionsselect course_name, FindCourse(course_name) as course_id
Oracle/PLSQL: Creating Functionsfrom courses
Oracle/PLSQL: Creating Functionswhere subject = 'Mathematics'; 
Oracle/PLSQL: Creating Functions

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
  • 2021-04-14
  • 2021-10-10
猜你喜欢
  • 2022-12-23
  • 2021-10-23
  • 2021-04-12
  • 2021-11-14
  • 2021-07-11
  • 2021-07-04
相关资源
相似解决方案