Oracle's Custom functions

Source: Internet
Author: User

A function in the database contains four parts: declaration, return value, function body, and exception handling.

  1. --Functions with no parameters
  2. Create or Replace function Get_user return VARCHAR2 is
  3. V_user VARCHAR2 (50);
  4. Begin
  5. Select username to V_user from User_users;
  6. return v_user;
  7. End Get_user;
  8. --Test
  9. Method One
  10. Select Get_user from dual;
  11. Method Two
  12. sql> var v_name varchar2 (50)
  13. Sql> Exec:v_name:=get_user;
  14. The PL/SQL process has completed successfully.
  15. sql> Print V_name
  16. V_name
  17. ------------------------------
  18. TEST
  19. Method Three
  20. sql> exec dbms_output.put_line (' Current database user is: ' | | Get_user);
  21. The current database user is: TEST
  22. The PL/SQL process has completed successfully.

    • --Functions with in parameters
    • Create or Replace function Get_empname (v_id in number) return VARCHAR2 as
    • V_name VARCHAR2 (50);
    • Begin
    • Select name into V_name from employee where id = v_id;
    • return v_name;
    • exception
    • When No_data_found Then
    • Raise_application_error (-20001, ' The ID you entered is invalid! ‘);
    • End Get_empname;

Report:

Function call Restrictions
1, the SQL statement can only call the storage function (server side), and cannot call the client's function
2, SQL can only call with input parameters, not with output, input and output functions
3. SQL cannot use the unique data type (Boolean,table,record, etc.)
4. A function called in an SQL statement cannot contain insert,update and DELETE statements

View function Yard Source code
Oracle stores the function name and its source code information in a data dictionary User_source
Select text from User_source where name= ' get_empname ';


Delete a function
Drop function Get_empname;

Http://www.cnblogs.com/1848/articles/1828927.html

Oracle's Custom functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.