Oracle stored procedure instance

Oracle cursors and stored procedures
1. PL/SQL uses cursors to manage SQL SELECT statements. A cursor is a large block of memory allocated for processing these statements. It provides the ability to process a result set row by row, which can It is regarded as a special pointer. It is associated with a query result set and can point to any position in the result set in order to process the data in the specified position. Using it, the data can be processed while querying the data.
Two . Classification of cursors: static cursors and dynamic cursors
   1. Before explaining the classification of cursors, first explain the two operations of PL/SQL, cursor attributes, variable attributes
SET SERVEROUTPUT ON ;-- This statement is used to enable the print output function
DMBS_OUTPUT. PUT_LINE();-- Print the output and wrap.
%FOUND: Determine whether the current cursor points to a valid line record. If it is, it returns TRUE, otherwise it returns FALSE
%NOTFOUND: Just the opposite of %FOUND.
%ISOPEN: Determine whether the cursor is open, Returns TRUE if open, otherwise returns FALSE.
%ROWCOUNT: Determines the number of rows fetched by the current cursor in the result set pointed to. Not all total records
%TYPE: The type of the declared variable is exactly the same as the data type of a column in the table. The %TYPE declaration has two advantages: one is that you don't have to know the exact data type of a column, and the other is that you don't need to modify the data type of the variable when the data type of a column changes.
%ROWTYPE: declares the data type of the variable and the row records in the table The data types are the same. For custom records, you must declare your own domain.
  2. Static cursors:
Static cursors can be divided into two categories:
1)
Implicit cursor Features of implicit cursor:
- Implicit cursor is managed by PL/SQL, that is, it does not need to declare cursor statement, nor OPEN, FETCH, CLOSE operation
- implicit cursor must have select cur_name into [variable name or other data type]. This sentence completes OPEN, FETCH, CLOSE operations.
- Implicit cursor can only return one row of records, if there are no records that meet the conditions, a NO_DATA_FOUND exception will occur. If there are multiple records, TOO_MANY_ROWS will occur Exception.
- Implicit cursors can only be judged with SQL% [img][/img] its cursor properties
- using SQL%ISOPEN for any position results in FALSE, implicit cursors are managed by PL/SQL
- for positions in implicit cursors Before using SQL%FOUND or SQL%NOTFOUND, SQL%ROWCOUNT result value is NULL (indeterminate value)
Example table:
table name: test



EG1: verify the feature of implicit cursor
    set serveroutput on;-- open output
declare
  no test.id %type;-- declare that the data type of variable no is consistent with the id column of the test table
  ename test.name%type;
  begin
  if sql%isopen then -- determine whether the cursor is open
  dbms_output.put_line(' cursor is openning');
  else
  dbms_output.put_line('cursor is closed');
  end if;
if sql%notfound then  -- 判断游标是否指向有效的行
  dbms_output.put_line('no value');
  else
  dbms_output.put_line(no||' '||ename);
  end if;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line('---------------');
/** 去掉where 条件时, 将会出现too_many_rows 异常**/
  select id ,name into no ,ename from test where cj=90;-- 隐式游标必-- 须使用INTO
  dbms_output.put_line(sql%rowcount);
  if sql%isopen then
  dbms_output.put_line(' cursor is openning');
  else
  dbms_output.put_line('cursor is closed');
  end if;
  if sql%notfound then
  dbms_output.put_line('no value');
  else
  dbms_output.put_line(no||' '||ename);
  end if;
  exception
  when no_data_found then
     dbms_output.put_line('no value');
when too_many_rows then
      dbms_output.put_line('too many rows');

end;

执行结果为:
cursor is closed
--------------------
1
cursor is closed
1001 qbnajj
去掉where 条件时的结果:
cursor is closed
-------------------
too many rows

EG2: 使用%ROWTYPE

declare

  rowtype test%rowtype;
  begin
  select * into rowtype from test where cj=90;
  dbms_output.put_line(rowtype.name||' '||rowtype.cj);
  exception
  when no_data_found then
     dbms_output.put_line('no value');
  when too_many_rows then
      dbms_output.put_line('too many rows');
  end;

  执行结果:

qbnajj 90
EG3: 自定义RECORD 数据类型

declare

/** 自定义RECORD 数据类型
  语法为:
  TYPE type_name IS RECORD(cloumn1 datatpe,.....);
**/
  type r_type is record(name test.name%type ,cj test.cj%type);
  re_type r_type;
  begin
  select name,cj into re_type from test where cj=90;
  dbms_output.put_line(re_type.name||' '||re_type.cj);
  exception
  when no_data_found then
     dbms_output.put_line('no value');
  when too_many_rows then
      dbms_output.put_line('too many rows');
  end;

execution result:

qbnajj 90
2) Display cursor
declaration syntax:
DECLARE
CURSOR cur_name(parameter name data type) is select_satatements ;-- after the cursor name-- can take parameters
BEGIN
OPEN cur_name;
FETCH cur_name into [variable or other data type];
-- can use a loop statement to iterate and output the result set pointed to by the cursor.
CLOSE cur_name;
END;
Display cursor characteristics :
- The display cursor is defined by the user, and the user manages the cursor, which can return multiple rows of records.
- Usually, the display cursor needs to comply with the following rules to
   declare the cursor -> open the cursor -> read data -> close the cursor
    but due to FOR IN LOOP The statement contains OPEN, FETCH, CLOSE operations so this is not the case.
  - When viewing cursor properties, use cur_name%.
EG:PL/SQL demo
declare
  no test.id%type;
  ename test.name%type;
  cjs test.cj%type;
  cursor test_cur is
  select id,name from test where cj>=cjs;-- PL/SQL available Bind variable input -- input value (&cjs)
  begin
  cjs:=50;
  for t_cur in test_cur
  loop
  no:=t_cur.id;
  ename:=t_cur.name;
  dbms_output.put_line(no||' '||ename);
  end loop;
  end;
Execution result:
chenjunhua 80
qbnajj 90
cjh 52
EG2: Demonstrate
create or replace procedure test_proce(cjs in test.cj%type)
  as
  no test.id%type;
  ename test.name% type;
  cursor test_cur is
  select id,name from test where cj>=cjs;
  begin
  open test_cur;
  fetch test_cur into no,ename;
  while test_cur%found
  loop
  dbms_output.put_line(no||' '||ename);
  fetch test_cur into no, ename;-- Point the cursor to the next record, otherwise it is an infinite loop.
  end loop;
  close test_cur;
  end test_proce;
  exec test_proce(50);
Execution result:
chenjunhua 80
qbnajj 90
cjh 52
EG3: The cursor with parameters is similar to the above
2. Dynamic cursor (REF CURSOR)
First of all, the cursor variable and the cursor are two different concepts. Similar to the cursor, the cursor variable refers to the current row of the result set of the multi-row query cursor query. The cursor is static, and the cursor variable is dynamic. At the same time, the cursor variable is not Participates in binding to a specific query, so cursor variables can be opened for any compatible query, increasing flexibility. Also, new values ​​can be assigned to cursor variables, which can be passed as parameters to local and stored procedures. Every PL/SQL user is available and can fully use cursor variables on the client side. The ORACLE server also includes a PL/SQL engine that can pass cursor variables between applications and servers.
1. Cursor variables: declare the actual cursor The above is to create a pointer, the pointer has the data type REF X.REF is REFERENCE, X is the representation class object. Therefore, the cursor variable has the data type REF CURSOR.
Note: The cursor always points to the same query work area, the cursor variable can point to different
3. Define the REF CURSOR type, there are two steps to create a cursor variable: 1
  ) Define the REF CURSOR type
  Syntax format:
  TYPE ref_type_name
  IS
  REF CURSOR [RETURN return_type]
  Description:
  ref_type_name is The type used in the cursor variable; return_type must be a record (record) or a row in the database table. (rowtype)
  The following defines a REF CURSOR type cursor
  DELARE
   TYPE xs_cur
  IS
  REF CURSOR RETURN xs%ROWTYPE;
  Note:
  REF CURSOR type can be either strong type or weak type, the difference is that strong type has return type, weak type does not. As shown below
   DECLARE
            TYPE xs_cur IS REF CURSOR RETURN xs %ROWTYPE;-- Strong type
            TYPE mycur IS REF CURSOR;-- Weak type
2) Declare a cursor variable of this type: Once the REF CURSOR type is defined, this cursor variable can be declared in a PL/SQL block or subroutine. For example :
  DECARE
   TYPE xs_cur REF CURSOR RETURN xs%ROWTYPE;
   xscur xs_cur;
   Of course, a custom RECORD type can also be defined in the RETURN clause, such as:
   DECLARE
           TYPE kc_cj IS RECORD
           (
            kch number (4),
            kcm number(10),
            cj number(4,2)
);
TYPE kc_cjcur IS REF CURSOR RETURN kc_cj;
In addition, cursor variables can also be declared as parameters of functions and procedures. For example:
DECLARE
       TYPE xs_cur IS REF CURSOR RETURN xs%ROWTYPE;
       PROCEDURE open_xs (xscur IN OUT xs_cur)IS
       … ................
3. Controlling Cursor Variables
  When using cursor variables, follow these steps: The OPEN-FETCH-CLOSE
  OPEN statement is associated with a cursor variable for a multi-row query, which executes Query, flag result set
  syntax format:
  OPEN {cursor_variable|:host_cursor_variable }FOR
  {
select_statement|dynamic_string[USING bind_argument[,...]]
}
  Such as:
  IF NOT xscur%ISOPEN THEN
            OPEN xscur FOR SELECT * FROM xs;
  END IF ;
Cursor variables can also use cursor attributes: %FOUND,%ISOPEN,%ROWTYPE
During use, other OPEN statements can open the same cursor variable for different queries. Therefore, do not close the cursor variable before reopening. The cursor can be opened and passed as a parameter to the stored procedure. For example:
CREATE PACKAGE xs_data AS
...
TYPE xs_cur IS REF CURSOR RETURN xs%ROWTYPE;
RROCEDURE open_xs(xscur IN OUT xs_cur);
END xs_data;

CREATE PACKAGE BODY xs_data AS
...
PROCEDURE open_xs(xscur IN OUT xs_cur)
AS
BEGIN
      OPEN xscur FOR SELECT * FROM xs ;
END open_xs;
END xs_data;
When declaring a cursor variable as an argument to an open cursor variable subroutine, IN OUT mode must be defined. That is, the subroutine can pass an open cursor variable to the caller

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326310968&siteId=291194637
Recommended