I. Introduction
1. Definitions
- It is essentially a set of data, similar to arrays, the query data sets stored in the memory of them.
- Cursor directed by a recording wherein use can achieve the purpose of the data set through the circulation loop cursor.
2. The types of cursors
Explicit cursor: have to use before the first declaration defines generally the result of the query defined cursor.
Can be acquired by recording the result set cursor loop, the end of the cycle may be acquired cursor out according to business needs.
After the cycle is completed by closing the cursor result set can no longer be acquired. All operations completed entirely written by the developers themselves, their control.
Implicit cursor: refers to the PL / SQL to manage cursors, developers can not control their own operations, it can only get attribute information.
Second, explicit cursor
Explicit cursors are often used in the actual development to be rich to write PL / SQL development process, to achieve some complex business cycle class. Cursors in the following steps:
- Declare cursor: - declare a cursor refers to the name and the cursor to the cursor result set associated with a query DECLARE the Cursor cursor name is query - Open the cursor: - Initialization cursor pointer - PS : Once the cursor after opening, the corresponding cursor result set is static will not be changed, regardless of the underlying data table query changed. open cursor name; - 3. Read the cursor data: - by fetch into the statement is complete, the pointer to the current cursor row is read to the data (record variable declarations) corresponding to the variable. - the PS : LOOP cursor read cycle for use with ships and for circulating acquiring records in a dataset. fetch cursor name into declared record variable - 4, close the cursor: - After closing the cursor associated with the result set is released, and can no longer be operated - PS : cursor finished using, be sure to close the cursor free up resources. close The cursor name;
Explicit cursor attributes
We use explicit cursor attribute value to obtain a state in which the cursor, and then the corresponding process accordingly, there are four common attributes:
1, % NOTFOUND . Acquiring data indicating whether the cursor when the extracted data, no data returns TRUE, the data returns false, the cursor is often used to determine whether all cycle is completed.
2, % FOUND . % NOTFOUND and exactly the contrary, when the cursor fetch data value has a value, returns TRUE, otherwise returns FALSE.
3, % ISOPEN . Used to determine whether the cursor is open.
4, % ROWCOUNT . It represents the current cursor FETCH INTO obtain a record of how many rows values, used to do the counting.
Example: Create a cursor loop that prints high school students basic information about the student information form, as follows:
- define a cursor DECLARE Cursor cur_xsjbxx IS SELECT * from stuinfo Order by stuid; - defined record variable ls_curinfo cur_xsjbxx % ROWTYPE; the begin - open cursors Open cur_xsjbxx; - cyclic Loop - acquiring and recording the value of the variables included in the write fetch cur_xsjbxx INTO ls_curinfo; - not when there is no data acquired (% NOTFOUND cursor is used to determine whether all cycle is completed.) Exit when cur_xsjbxx % NOTFOUND; -PL / sql output information (equivalent to the System.out.println java ()) DBMS_OUTPUT.PUT_LINE ( ' Student ID: ' || ls_curinfo.stuid || ' , Name: ' || ls_curinfo.stuname); End Loop ; - Close cursor Close cur_xsjbxx; End ;
Third, implicit cursor
- Implicit cursor although not as explicit cursor has the same maneuverability, but it is often used to attribute value in the actual development process.
- When implicit cursor is mainly used in the select statement or some operations dml statements, PL / SQL program automatically opens an implicit cursor, the implicit cursor is not controlled by the developer.
- oracle implicit cursor does not like to explicitly declare a cursor as a cursor name, but directly using " SQL " or " SQL " as the name of an implicit cursor.
- Explicit cursor attribute values are represented by some of the results of determination of the number of set lines is implicitly influence the number of lines corresponding to a cursor DML statements.
- implicit cursor DECLARE - define only record variable, did not declare the cursor (if a table has more columns, use% rowtype to define a variable table indicates a row) ls_xsjbxx stuinfo % ROWTYPE; the begin - Query students information (select * into the whole data set into the table records defined variables, the variables used to implement cursor functionality) SELECT * iNTO ls_xsjbxx from stuinfo T WHERE t.stuid = ' SC201801001 ' ; IF SQL % found the then DBMS_OUTPUT.PUT_LINE ( ' Student number: ' || ls_xsjbxx.stuid || ' , name: ' || ls_xsjbxx.stuname); End IF ; - Query Student Information (absent students) the SELECT * INTO ls_xsjbxx from stuinfo t the WHERE t.stuid = ' SC201901001 ' ; IF SQL % found the then dbms_output.put_line ( ' student number: ' || ls_xsjbxx.stuid | | ' , name: ' || ls_xsjbxx.stuname); End IF ; Exception the when NO_DATA_FOUND the then dbms_output.put_line ( ' the students does not exist SC201901001'); end;