Simple Oracle functions and stored procedures

Source: Internet
Author: User

1.Function

1) Create a function

  1. Create or replace function get_tax (x number)
  2. Return number
  3. Begin
  4. Declare y number;
  5. Begin
  6. Y:=X-2000;
  7. If x<= 0 then
  8. Return 0;
  9. End if;
  10. Returns y * 5/100;
  11. End;
  12. End get_tax;

2) Execution function

  1. SQL>Select get_tax (1000) from dual;

Result:

  1. GET_TAX (1, 1000)
  2. -------------
  3. -50

2.Stored Procedure

1) Stored procedure (In)

Create:

  1. Create or replace procedure update_test (uid in varchar2, uname in varchar2)
  2. As
  3. Begin
  4. Update test setUsername=UnameWhereUserid=Uid;
  5. Commit;
  6. End update_test;

Run:
  1. SQL>Execute update_test ('06', 'linuxid ');

2) Stored procedure (Out)

Create:

  1. Create or replace procedure test_up (uid out varchar2, uname out varchar2)
  2. As
  3. Begin
  4. Select * into uid, uname from test whereUserid='04'; // The into keyword cannot be missing
  5. End test_up;
Run:
  1. SQL>Var id varchar2 (10 );
  2. SQL>Var name varchar2 (30 );
  3. SQL>Exec test_up (: id,: name); // a colon must be added to the brackets, which is different from in.
Result:
  1. PL/SQL procedure successfully completed
  2. Id
  3. ---------
  4. 04
  5. Name
  6. ---------
  7. LinuxIDC

For more information about Oracle, see Oracle topics page http://www.bkjia.com/topicnews.aspx? Tid = 12

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.