Oracle cursor use full solution

Source: Internet
Author: User
Tags dname oracle cursor rowcount

-- Declare the cursor; cursor cursor_name is select_statement

-- For Loop cursor
-- (1) define a cursor
-- (2) define the cursor variable
-- (3) use the cursor for a For Loop
Declare
-- Type Definition
Cursor c_job
Is
Select empno, ename, job, Sal
From EMP
Where job = 'manager ';
-- Defines a cursor variable v_cinfo c_emp % rowtype, which is a row of data type in the cursor c_emp.
C_row c_job % rowtype;
Begin
For c_row in c_job Loop
Dbms_output.put_line (c_row.empno | '-' | c_row.ename | '-' | c_row.job | '-' | c_row.sal );
End loop;
End;


-- Fetch cursor
-- You must enable and disable it explicitly when using it.

Declare
-- Type Definition
Cursor c_job
Is
Select empno, ename, job, Sal
From EMP
Where job = 'manager ';
-- Define a cursor variable
C_row c_job % rowtype;
Begin
Open c_job;
Loop
-- Extract a row of data to c_row
Fetch c_job into c_row;
-- Determine whether to extract the value. Exit if the value is not obtained.
-- The value c_job % notfound is false.
-- The value c_job % notfound is not obtained. True.
Exit when c_job % notfound;
Dbms_output.put_line (c_row.empno | '-' | c_row.ename | '-' | c_row.job | '-' | c_row.sal );
End loop;
-- Close the cursor
Close c_job;
End;

-- 1: execute any update operation. Use the attributes % found, % notfound, % rowcount, and % isopen of the implicit cursor SQL statement to observe the execution of the update statement.
Begin
Update EMP set ename = 'eark' where empno = 7469;
If SQL % isopen then
Dbms_output.put_line ('openging ');
Else
Dbms_output.put_line ('closing ');
End if;
If SQL % found then
Dbms_output.put_line ('cursor points to valid row'); -- determines whether the cursor points to a valid row.
Else
Dbms_output.put_line ('sorry ');
End if;
If SQL % notfound then
Dbms_output.put_line ('also sorry ');
Else
Dbms_output.put_line ('hahaha ');
End if;
Dbms_output.put_line (SQL % rowcount );
Exception
When no_data_found then
Dbms_output.put_line ('Sorry no data ');
When too_many_rows then
Dbms_output.put_line ('too writable rows ');
End;
Declare
Empnumber EMP. empno % type;
Empname EMP. ename % type;
Begin
If SQL % isopen then
Dbms_output.put_line ('cursor is opinging ');
Else
Dbms_output.put_line ('cursor is close ');
End if;
If SQL % notfound then
Dbms_output.put_line ('no value ');
Else
Dbms_output.put_line (empnumber );
End if;
Dbms_output.put_line (SQL % rowcount );
Dbms_output.put_line ('-------------');

Select empno, ename into empnumber, empname from EMP where empno = 7499;
Dbms_output.put_line (SQL % rowcount );

If SQL % isopen then
Dbms_output.put_line ('cursor is opinging ');
Else
Dbms_output.put_line ('cursor is closing ');
End if;
If SQL % notfound then
Dbms_output.put_line ('no value ');
Else
Dbms_output.put_line (empnumber );
End if;
Exception
When no_data_found then
Dbms_output.put_line ('no value ');
When too_many_rows then
Dbms_output.put_line ('too writable rows ');
End;



-- 2. The cursor and loop are used to display the names of all departments.
-- Cursor Declaration
Declare
Cursor csr_dept
Is
-- SELECT statement
Select dname
From depth;
-- Specifies the row pointer. This statement should be a variable of the same type as the csr_dept row.
Row_dept csr_dept % rowtype;
Begin
-- For Loop
For row_dept in csr_dept Loop
Dbms_output.put_line ('department name: '| row_dept.dname );
End loop;
End;

-- 3. Use a cursor and a while loop to display the geographical location of all departments (use the % found attribute)
Declare
-- Cursor Declaration
Cursor csr_testwhile
Is
-- SELECT statement
Select Loc
From depth;
-- Specify the row pointer
Row_loc csr_testwhile % rowtype;
Begin
-- Open the cursor
Open csr_testwhile;
-- Feed data to the first line
Fetch csr_testwhile into row_loc;
-- Test whether data exists and execute a loop
While csr_testwhile % found Loop
Dbms_output.put_line ('department location: '| row_loc.loc );
-- Feed data to the next row
Fetch csr_testwhile into row_loc;
End loop;
Close csr_testwhile;
End;
Select * from EMP

 


-- 4. Receive the Department number entered by the user. Print all information of all employees of the Department using the for loop and cursor (use the cyclic cursor)
-- Cursor cursor_name [(parameter [, parameter],...)] is select_statement;
-- Syntax for defining parameters: parameter_name [in] data_type [{:=| default} value]

Declare
Cursor
C_dept (p_deptno number)
Is
Select * from EMP where EMP. depno = p_deptno;
R_emp EMP % rowtype;
Begin
For r_emp in c_dept (20) loop
Dbms_output.put_line ('employee ID: '| r_emp.empno | 'employee name:' | r_emp.ename | 'salary: '| r_emp.sal );
End loop;
End;
Select * from EMP
-- 5: pass a type of work to the cursor to display all information of all employees of this type of work (using the parameter cursor)
Declare
Cursor
C_job (p_job nvarchar2)
Is
Select * from EMP where job = p_job;
R_job EMP % rowtype;
Begin
For r_job in c_job ('cler') loop
Dbms_output.put_line ('employee ID '| r_job.empno | ''| 'employee name' | r_job.ename );
End loop;
End;
Select * from EMP

-- 6: use an update cursor to add commission to employees: (use if to create an emp1 table that is the same as the EMP table and modify the emp1 table ), and output the data before and after the update.
Http://zheng12tian.iteye.com/blog/815770
Create Table emp1 as select * from EMP;

Declare
Cursor
Csr_update
Is
Select * From emp1 for update of Sal;
Empinfo csr_update % rowtype;
Saleinfo emp1.sal % type;
Begin
For empinfo in csr_update Loop
If empinfo. Sal <1500 then
Saleinfo: = empinfo. Sal * 1.2;
Elsif empinfo. Sal <2000 then
Saleinfo: = empinfo. Sal * 1.5;
Elsif empinfo. Sal <3000 then
Saleinfo: = empinfo. Sal * 2;
End if;
Update emp1 set sal = saleinfo where current of csr_update;
End loop;
End;

-- 7: compile a PL/SQL program block. All employees whose names start with 'A' or 'S' are paid according to their basic salary (SAL) 10% to give them a raise (modify the emp1 table)
Declare
Cursor
Csr_addsal
Is
Select * From emp1 where ename like 'a % 'or ename like's %' for update of Sal;
R_addsal csr_addsal % rowtype;
Saleinfo emp1.sal % type;
Begin
For r_addsal in csr_addsal Loop
Dbms_output.put_line (r_addsal.ename | 'original salary: '| r_addsal.sal );
Saleinfo: = r_addsal.sal * 1.1;
Update emp1 set sal = saleinfo where current of csr_addsal;
End loop;
End;
-- 8: compile a PL/SQL block to increase the Commission (Comm) by 500 for all salesman.
Declare
Cursor
Csr_addcomm (p_job nvarchar2)
Is
Select * From emp1 where job = p_job for update of comm;
R_addcomm emp1 % rowtype;
Comminfo emp1.comm % type;
Begin
For r_addcomm in csr_addcomm ('salesman') loop
Comminfo: = r_addcomm.comm + 500;
Update emp1 set comm = comminfo where current of csr_addcomm;
End loop;
End;

-- 9: compile a PL/SQL program block to improve the qualification of the two oldest employees as managers (the longer the working hours, the older the qualifications)
-- (Tip: You can define a variable as a counter to control the cursor to extract only two pieces of data. You can also find the two most qualified employees in the cursor when declaring the cursor .)
Declare
Cursor crs_testcomput
Is
Select * From emp1 order by hiredate ASC;
-- Counter
Top_two number: = 2;
R_testcomput crs_testcomput % rowtype;
Begin
Open crs_testcomput;
Fetch crs_testcomput into r_testcomput;
While top_two> 0 Loop
Dbms_output.put_line ('employee name: '| r_testcomput.ename | 'Working Time:' | r_testcomput.hiredate );
-- Speedometer minus 1
Top_two: = top_two-1;
Fetch crs_testcomput into r_testcomput;
End loop;
Close crs_testcomput;
End;

-- 10: compile a PL/SQL program block to raise salaries for all employees based on 20% of their basic salary (SAL,
-- If the salary increases by more than 300, cancel the salary increase (modify the emp1 table and output the data before and after the update)
Declare
Cursor
Crs_upadatesal
Is
Select * From emp1 for update of Sal;
R_updatesal crs_upadatesal % rowtype;
Saladd emp1.sal % type;
Salinfo emp1.sal % type;
Begin
For r_updatesal in crs_upadatesal Loop
Saladd: = r_updatesal.sal * 0.2;
If saladd> 300 then
Salinfo: = r_updatesal.sal;
Dbms_output.put_line (r_updatesal.ename | ': salary increase failed. '|' Salary: '| r_updatesal.sal );
Else
Salinfo: = r_updatesal.sal + saladd;
Dbms_output.put_line (r_updatesal.ename | ': salary increase succeeded.' | 'salary changed to: '| salinfo );
End if;
Update emp1 set sal = salinfo where current of crs_upadatesal;
End loop;
End;

-- 11: how many years, months, and days each employee has worked
-- Approximation
-- Ceil (n) function: obtains the smallest integer greater than or equal to the value n.
-- Floor (n) function: returns the largest integer less than or equal to the value n.
-- Truc usage http://publish.it168.com/2005/1028/20051028034101.shtml
Declare
Cursor
Crs_workday
Is
Select ename, hiredate, trunc (months_between (sysdate, hiredate)/12) as spandyears,
Trunc (mod (months_between (sysdate, hiredate), 12) as months,
Trunc (mod (sysdate-hiredate, 365), 12) as days
From emp1;
R_workday crs_workday % rowtype;
Begin
For r_workday in crs_workday Loop
Dbms_output.put_line (r_workday.ename | 'working now' | r_workday.spandyears | 'year, 0' | r_workday.months | 'month, 0' | r_workday.days | 'day ');
End loop;
End;

-- 12: Enter the Department number and execute the statement according to the following salary increase ratio (Use Case to create an emp1 table, modify the data in the emp1 table), and output the data before and after the update.
-- Deptno raise (%)
-- 10 5%
-- 20 10%
-- 30 15%
-- 40 20%
-- The salary increase ratio is based on the existing Sal.
-- Case expr when comparison_expr then return_expr
-- [, When comparison_expr then return_expr]... [else else_expr] End
Declare
Cursor
Crs_casetest
Is
Select * From emp1 for update of Sal;
R_casetest crs_casetest % rowtype;
Salinfo emp1.sal % type;
Begin
For r_casetest in crs_casetest Loop
Case
When r_casetest.depno = 10
Then salinfo: = r_casetest.sal * 1.05;
When r_casetest.depno = 20
Then salinfo: = r_casetest.sal * 1.1;
When r_casetest.depno = 30
Then salinfo: = r_casetest.sal * 1.15;
When r_casetest.depno = 40
Then salinfo: = r_casetest.sal * 1.2;
End case;
Update emp1 set sal = salinfo where current of crs_casetest;
End loop;
End;

-- 13: Judge the salary of each employee. If the employee's salary is higher than the average salary of his/her department, the employee's salary will be reduced by 50 yuan, and the salary before and after the update and the employee's name will be output, department ID.
-- AVG ([distinct | all] expr) over (analytic_clause)
--- Function:
-- Calculate the average value of the group according to the rules in analytic_clause.
-- Analysis Function Syntax:
-- Function_name (<argument>, <argument> ...)
-- Over
-- (<Partition-clause> <order-by-clause> <wing clause>)
-- Partition clause
-- Partition by expression (Group). If the partition clause is omitted, all result sets are considered as a single group.
Select * From emp1
Declare
Cursor
Crs_testavg
Is
Select empno, ename, job, Sal, depno, AVG (SAL) over (partition by depno) as dep_avg
From emp1 for update of Sal;
R_testavg crs_testavg % rowtype;
Salinfo emp1.sal % type;
Begin
For r_testavg in crs_testavg Loop
If r_testavg.sal> r_testavg.dep_avg then
Salinfo: = r_testAvg.SAL-50;
End if;
Update emp1 set sal = salinfo where current of crs_testavg;
End loop;
End;

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.