Comparison between cursor's simple writing method and official documents

Source: Internet
Author: User

Comparing the Simple Writing Method of cursor with official documents, it is very common to use cursor in storage, and traditional writing is troublesome. Summary: The format of www.2cto.com is as follows: [SQL] create or replace procedure ff as v_column_name begin for v_info in (select column_name from cols where table_name = 'emp ') loop -- Define and loop cursor dbms_output.put_line (v_info.column_name); -- call the value end loop in the cursor; end;/* advantages: www.2cto.com 1. variables that do not need to be specified to receive the cursor value 2. open or close cursors that do not need to be displayed. Disadvantages: 1. Unable to use the cursors notfound, isopen, found, notfound */The following is an excerpt from several official documents: www.2cto.com [SQL] DECLARE CURSOR c1 (job VARCHAR2, max_wage NUMBER) is select * FROM employees WHERE job_id = job AND salary> max_wage; begin for person IN c1 ('st _ cler', 3000) LOOP -- process data record DBMS_OUTPUT.PUT_LINE ('name = '| person. last_name | ', salary =' | person. salary | ', Job Id =' | person. job_id); end loop; END;/Result: Name = Nayer, salary = 4065.6, Job Id = ST_CLERKName = Mikkilineni, salary = 3430.35, Job Id = ST_CLERKName = Landry, salary = 3049.2, Job Id = ST_CLERK... name = Vargas, salary = 3176.25, Job Id = ST_CLERK Example 2: [SQL] BEGIN FOR item IN (SELECT first_name | ''| last_name AS full_name, salary * 10 AS dream_salary FROM employees where rownum <= 5 order by dream_salary DESC, last_name ASC) LOOP DBMS_OUTPUT.PUT_LINE (item. full_name | 'dreams of making' | item. dream_salary); END LOOP; END;/Result: Michael Hartstein dreams of making 143325Pat Fay dreams of making 66150 Jennifer Whalen dreams of making 48510 Douglas Grant dreams of making 31531.5 Donald OConnell dreams of making 31531.5 Note: when an exception is raised inside a cursorFOR LOOP statement, the cursor closes before the exception handler runs. therefore, the values of explicit cursor attributes are not avail Able in the handler. When an exception occurs inside the for loop statement block, the cursor is closed before the exception is processed. Therefore, the attribute of the cursor displayed in the statement block is invalid. www.2cto.com: (extracted from the official document) You can either declare an explicit cursor first and then define it later in the same block, subprogram, or package, or declare and define it at the same time. an explicit cursor declaration, which only declares a cursor, has this syntax: CURSOR cursor_name [parameter_list] RETURN return_type; An explicit cursor definition has this syntax: CURSOR cursor _ Name [parameter_list] [RETURN return_type] IS select_statement; If you declared the cursor earlier, then the explicit cursor definition defines it; otherwise, it both declares and defines it. example: www.2cto.com [SQL] DECLARE CURSOR c1 RETURN orders ments % ROWTYPE; -- DECLARE c1 CURSOR c1 RETURN orders ments % ROWTYPE IS -- Define c1, SELECT * FROM orders ments WHERE department_id = 110; /* in actual use, the c2 statement and The method for defining the cursor c1 and c3 has never been used. The official document IS the standard... */CURSOR c2 IS -- declare and define c2 SELECT employee_id, job_id, salary FROM employees WHERE salary> 2000;/* the following method IS also commonly used. The CURSOR definition method is open C2 FOR 'SQL statements'. You can flexibly bind the variable */TYPE cursor_type IS REF CURSOR; C2 cursor_type; CURSOR c3 RETURN locations % ROWTYPE; -- declare c3 CURSOR c3 IS -- Define c3, SELECT * FROM locations -- omit return type WHERE country_id = 'jp '; begin null; 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.