Oracle Stored Procedure demo sharing, oracle Stored Procedure demo

Source: Internet
Author: User

Oracle Stored Procedure demo sharing, oracle Stored Procedure demo

-- 1. Define the Stored Procedure

-- Definition

Create or replace procedure proc01

As n int; -- add points after defining variables

Begin

Select count (*) into n from emp;

Dbms_output.put_line ('employee table has '| n |' records! ');

End;

-- Call

Declare

Begin

Proc01 ();

End;

-- 2. Stored Procedures with Parameters

-- Definition

Create or replace procedure p3 (j1 varchar2)

As

Cursor c1 is select * from emp where job = j1;

Emper emp % rowtype;

Begin

Open c1;

Loop

Fetch c1 into emper;

Exit when c1 % notfound;

Dbms_output.put_line (emper. ename );

End loop;

Close c1;

End;

-- Call

Declare

Begin

P3 ('cler ');

End;

-- Process with Input and Output Parameters

Create or replace procedure p4 (a in int, B in int, c out int)

As

Begin

C: = 0; -- assign an initial value to c

For I in a... B

Loop

C: = c + I; -- accumulate

End loop;

End;

-- Call

Declare

A int: = 1;

B int: = 100;

C int;

Begin

P4 (a, B, c); -- call

Dbms_output.put_line (c );

End;

-- Calculate the factorial of a given number (with input and output parameters)

Create or replace procedure p5 (a in int, B out int)

As

Begin

B: = 1;

For I in 1. a -- factorial of

Loop

B: = B * I;

End loop;

End;

-- Call

Declare

A int: = 5;

B int;

Begin

P5 (a, B );

Dbms_output.put_line (B );

End;

-- Calculate the factorial of a given number (input and output parameters) (using the intermediate parameter "B)

Create or replace procedure p6 (a in out int)

As

B int;

Begin

B: = a; -- assign the value of a to B.

A: = 1; -- change the value of a to 1.

For I in 1 .. B -- loop

Loop

A: = a * I;

End loop;

End;

-- Call

Declare

A int: = 6;

Begin

P6 ();

Dbms_output.put_line ();

End;

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.