oracle

create table e( a numeric, b varchar2(50));declaretemp varchar2(20):='aaaa';i number;begin  

for i in 1..100 loop insert into e(a,b) values(i,temp ); end loop;end;

oracle stored procedure, declare variable, for loop | turn |



oracle stored procedure, declare variable, for loop

1, create stored procedure

create or replace procedure test(var_name_1 in type,var_name_2 out type) as

-- Declare a variable (variable name variable type)

begin

-- the execution body of the stored procedure

end test;

print out the input time information

Eg:

create or replace procedure test(workDate in Date) is

begin

dbms_output.putline('The input date is: '||to_date(workDate,'yyyy-mm-dd'));

end test;

2. Variable assignment

Variable name:= value;

Eg:

create or replace procedure test(workDate in Date) is

x number(4,2);

begin

x := 1;

end test;

3、判断语句:

if 比较式 then begin end; end if;

E.g

create or replace procedure test(x in number) is

begin

        if x >0 then

         begin

        x := 0 - x;

        end;

    end if;

    if x = 0 then

       begin

        x: = 1;

    end;

    end if;

end test;

4、For 循环

For ... in ... LOOP

--执行语句

end LOOP;

(1)循环遍历游标

create or replace procedure test() as

Cursor cursor is select name from student; name varchar(20);

begin

for name in cursor LOOP

begin

dbms_output.putline(name);

end;

end LOOP;

end test;

(2) Loop through the array

create or replace procedure test(varArray in myPackage.TestArray) as

--(The input parameter varArray is a custom array type, see Title 6 for the definition method)

i number;

begin

i := 1; -- the starting position of the stored procedure array starts from 1, which is the same as java , C, C++ and other languages ​​are different. Because there is no concept of an array in Oracle, an array is actually a

table (Table), and each array element is a record in the table, so traversing the array is equivalent to the first record in the table. Start traversal

for i in 1..varArray.count LOOP    

dbms_output.putline('The No.'|| i || 'record in varArray is:'||varArray(i)); 

end LOOP;

end test ;

5. While loop

while conditional statement LOOP

begin

end;

end LOOP;

Eg

create or replace procedure test(i in number) as

begin while i < 10

LOOP

begin  

i:= i + 1;

end;

end LOOP;

end test; Concept: There is no concept of an array in Oracle. An array is actually a table, and each array element is a record in the table. When using arrays, users can use the array types already defined by Oracle, or define array types according to their own needs. (1) Use Oracle's own array type x array; -- need to be initialized when using eg: create or replace procedure test(y out array) is x array; begin x := new array(); y := x ; end test; (2) Custom array type (when custom data type, it is recommended to create a Package for easy management)



























Eg (see heading 4.2 for custom usage) create or replace package myPackage is

  -- Public type declarations type info is record( name varchar(20), y number);

  type TestArray is table of info index by binary_integer; -- declare here The type data of a TestArray is actually a Table that stores the Info data type, and TestArray is a table with two fields, one is

name and the other is y. It should be noted that Index by binary_integer is used here to compile the index item of the Table, or it can be written directly as: type TestArray is

table of info. If it is not written, it needs to be initialized when using the array: varArray myPackage.TestArray; varArray := new myPackage.TestArray();

end TestArray;

7. Cursor use

    Cursor in Oracle is very useful for traversing the query results in the temporary table. There are also many related methods and properties. Now I will only introduce the commonly used usage:

(1) Cursor cursor (cannot be used for parameter passing)

create or replace procedure test() is 

cusor_1 Cursor is select std_name from student where ...; --Cursor usage 1 cursor_2 Cursor;

begin

select class_name into cursor_2 from class where ...; --Cursor usage 2

can use For x in cursor LOOP .. .. end LOOP; to realize the traversal of Cursor

end test;

(2) SYS_REFCURSOR cursor, which is a cursor predefined by Oracle, which can be passed as parameters

create or replace procedure test(rsCursor out SYS_REFCURSOR) is

cursor SYS_REFCURSOR; name varhcar(20);

begin OPEN cursor FOR select name from student where ... --SYS_REFCURSOR can only be opened and assigned LOOP

through the OPEN method



fetch cursor into name --SYS_REFCURSOR can only be opened and traversed through fetch into exit when cursor%NOTFOUND; --SYS_REFCURSOR can use three status attributes: ---%NOTFOUND (record information not found) %FOUND (record information found ) ---%ROWCOUNT (then the row position pointed to by the current cursor)

dbms_output.putline(name);

end LOOP;

rsCursor := cursor;

end test;

write a simple example below to use the above-mentioned stored procedure Make an application:

Now suppose there are two tables, one is the student grade sheet (studnet), and the fields are: stdId, math, article, language, music, sport, total, average, step and one is the student extracurricular grade sheet (out_school ), the fields are: stdId, parctice, comment

The total grade and average grade of each student are automatically calculated through the stored procedure. At the same time, if the student gets an A in the extracurricular course, 20 points will be added to the total grade.

create or replace procedure autocomputer(step in number) is

rsCursor SYS_REFCURSOR;

commentArray myPackage.myArray;

math number;

article number;

language number;

music number;

sport number;

total number;

average number;

stdId varchar(30);

record myPackage.stdInfo;

i number;

begin

i := 1;

get_comment(commentArray); --调用名为get_comment()的存储过程获取学生课外评分信息

OPEN rsCursor for select stdId,math,article,language,music,sport from student t where t.step = step;

LOOP

fetch rsCursor into stdId,math,article,language,music,sport; exit when rsCursor%NOTFOUND;

total := math + article + language + music + sport;

for i in 1..commentArray.count LOOP

record := commentArray(i);  

if stdId = record.stdId then

begin   

if record.comment = 'A' then   

  begin       

total := total + 20; 

   go to next; --使用go to跳出for循环     

  end;   

end if;

end;

end if;

end LOOP;

<<continue>>  average := total / 5;

update student t set t.total=total and t.average = average where t.stdId = stdId;

end LOOP;

end;

end autocomputer;

--取得学生评论信息的存储过程

create or replace procedure get_comment(commentArray out myPackage.myArray) is

rs SYS_REFCURSOR;

record myPackage.stdInfo;

stdId varchar(30);

comment varchar(1);

i number;

begin

open rs for select stdId,comment from out_school

i := 1;

LOOP

fetch rs into stdId,comment; exit when rs%NOTFOUND;

record.stdId := stdId;

record.comment := comment;

recommentArray(i) := record;

i:=i + 1;

end LOOP;

end get_comment; --define the

array type myArray

create or replace package myPackage is begin

type stdInfo is record(stdId varchar(30),comment varchar(1));

type myArray is table of stdInfo index by binary_integer;

end myPackage;



This article is from CSDN blog, please indicate the source for reprint: http://blog.csdn.net/squirrelrao/archive/2008/07/11/2639571.aspx

Application:


oracle cursor, stored procedure, for loop , loop unification


create or replace procedure libsys.add_money(money out number) ascursor cur is

select amount from libsys.money_record;

begin

money:=0;

for amounts in cur

loop

money:=money+amounts;

end loop;

end add_money;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326799296&siteId=291194637