PL/SQL User’s Guide and Reference phần 8 pptx

48 357 0
PL/SQL User’s Guide and Reference phần 8 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Cursors 11-48 PL/SQL User’s Guide and Reference Cursors To execute a multi-row query, Oracle opens an unnamed work area that stores processing information. An explicit cursor lets you name the work area, access the information, and process the rows individually. For more information, see "Managing Cursors" on page 5-6. Syntax CURSOR cursor_name ( cursor_parameter_declaration , ) cursor_body RETURN rowtype IS select_statement ; CURSOR cursor_name ( cursor_parameter_declaration , ) cursor_spec RETURN rowtype ; CURSOR cursor_name ( cursor_parameter_declaration , ) cursor_declaration RETURN rowtype IS select_statement ; parameter_name IN datatype := DEFAULT expression cursor_parameter_declaration Cursors Language Elements 11-49 Keyword and Parameter Description cursor_name This identifies an explicit cursor previously declared within the current scope. datatype This is a type specifier. For the syntax of datatype, see "Constants and Variables" on page 11-33. db_table_name This identifies a database table (or view) that must be accessible when the declaration is elaborated. expression This is an arbitrarily complex combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. When the declaration is elaborated, the value of expression is assigned to the parameter. The value and the parameter must have compatible datatypes. parameter_name This identifies a cursor parameter; that is, a variable declared as the formal parameter of a cursor. A cursor parameter can appear in a query wherever a constant can appear. The formal parameters of a cursor must be IN parameters. The query can also reference other PL/SQL variables within its scope. db_table_name cursor_name cursor_variable_name % ROWTYPE record_name % TYPE record_type_name rowtype Cursors 11-50 PL/SQL User’s Guide and Reference record_name This identifies a user-defined record previously declared within the current scope. record_type_name This identifies a user-defined record type that was defined using the datatype specifier RECORD. RETURN This keyword introduces the RETURN clause, which specifies the datatype of a cursor return value. You can use the %ROWTYPE attribute in the RETURN clause to provide a record type that represents a row in a database table or a row returned by a previously declared cursor. Also, you can use the %TYPE attribute to provide the datatype of a previously declared record. A cursor body must have a SELECT statement and the same RETURN clause as its corresponding cursor spec. Also, the number, order, and datatypes of select items in the SELECT clause must match the RETURN clause. %ROWTYPE This attribute provides a record type that represents a row in a database table or a row fetched from a previously declared cursor or cursor variable. Fields in the record and corresponding columns in the row have the same names and datatypes. select_statement This is a query that returns a result set of rows. Its syntax is like that of select_ into_statement without the INTO clause. See "SELECT INTO Statement" on page 11-154. If the cursor declaration declares parameters, each parameter must be used in the query. %TYPE This attribute provides the datatype of a previously declared user-defined record. Usage Notes You must declare a cursor before referencing it in an OPEN, FETCH, or CLOSE statement. And, you must declare a variable before referencing it in a cursor declaration. The word SQL is reserved by PL/SQL for use as the default name for implicit cursors and cannot be used in a cursor declaration. Cursors Language Elements 11-51 You cannot assign values to a cursor name or use it in an expression. However, cursors and variables follow the same scoping rules. For more information, see "Scope and Visibility" on page 2-38. You retrieve data from a cursor by opening it, then fetching from it. Because the FETCH statement specifies the target variables, using an INTO clause in the SELECT statement of a cursor_declaration is redundant and invalid. The scope of cursor parameters is local to the cursor, meaning that they can be referenced only within the query used in the cursor declaration. The values of cursor parameters are used by the associated query when the cursor is opened. The query can also reference other PL/SQL variables within its scope. The datatype of a cursor parameter must be specified without constraints. For example, the following parameter declarations are illegal: CURSOR c1 (emp_id NUMBER NOT NULL, dept_no NUMBER(2)) illegal Examples Some examples of cursor declarations follow: CURSOR c1 IS SELECT empno, ename, job, sal FROM emp WHERE sal > 2000; CURSOR c2 RETURN dept%ROWTYPE IS SELECT * FROM dept WHERE deptno = 10; CURSOR c3 (start_date DATE) IS SELECT empno, sal FROM emp WHERE hiredate > start_date; Related Topics CLOSE Statement, FETCH Statement, OPEN Statement, SELECT INTO Statement DELETE Statement 11-52 PL/SQL User’s Guide and Reference DELETE Statement The DELETE statement removes entire rows of data from a specified table or view. For a full description of the DELETE statement, see Oracle8i SQL Reference. Syntax WHERE search_condition CURRENT OF cursor_name returning_clause ; schema_name . db_table_name view_name @ dblink_name table_reference RETURNING single_row_expression , INTO variable_name : host_variable_name , multiple_row_expression , BULK COLLECT INTO collection_name : host_array_name , returning_clause delete_statement DELETE FROM ( subquery TABLE ( subquery2 ) alias table_reference ) DELETE Statement Language Elements 11-53 Keyword and Parameter Description alias This is another (usually short) name for the referenced table or view and is typically used in the WHERE clause. BULK COLLECT This clause instructs the SQL engine to bulk-bind output collections before returning them to the PL/SQL engine. The SQL engine bulk-binds all collections referenced in the RETURNING INTO list. The corresponding columns must store scalar (not composite) values. For more information, see "Taking Advantage of Bulk Binds" on page 4-29. returning_clause This clause lets you return values from the deleted rows, thereby eliminating the need to SELECT the rows beforehand. You can retrieve the column values into variables and/or host variables, or into collections and/or host arrays. However, you cannot use the RETURNING clause for remote or parallel deletes. subquery This is a SELECT statement that provides a set of rows for processing. Its syntax is like that of select_into_statement without the INTO clause. See "SELECT INTO Statement" on page 11-154. table_reference This specifies a table or view, which must be accessible when you execute the DELETE statement, and for which you must have DELETE privileges. TABLE (subquery2) The operand of TABLE is a SELECT statement that returns a single column value, which must be a nested table. Operator TABLE informs Oracle that the value is a collection, not a scalar value. DELETE Statement 11-54 PL/SQL User’s Guide and Reference WHERE CURRENT OF cursor_name This clause refers to the latest row processed by the FETCH statement associated with the cursor identified by cursor_name. The cursor must be FOR UPDATE and must be open and positioned on a row. If the cursor is not open, the CURRENT OF clause causes an error. If the cursor is open, but no rows have been fetched or the last fetch returned no rows, PL/SQL raises the predefined exception NO_DATA_FOUND. WHERE search_condition This clause conditionally chooses rows to be deleted from the referenced table or view. Only rows that meet the search condition are deleted. If you omit the WHERE clause, all rows in the table or view are deleted. Usage Notes You can use the DELETE WHERE CURRENT OF statement after a fetch from an open cursor (this includes implicit fetches executed in a cursor FOR loop), provided the associated query is FOR UPDATE. This statement deletes the current row; that is, the one just fetched. The implicit cursor SQL and the cursor attributes %NOTFOUND, %FOUND, and %ROWCOUNT let you access useful information about the execution of a DELETE statement. Examples The following statement deletes from the bonus table all employees whose sales were below quota: DELETE FROM bonus WHERE sales_amt < quota; The following statement returns two column values from a deleted row into local variables: DECLARE my_empno emp.empno%TYPE; my_ename emp.ename%TYPE; my_job emp.job%TYPE; BEGIN DELETE FROM emp WHERE empno = my_empno RETURNING ename, job INTO my_ename, my_job; END; DELETE Statement Language Elements 11-55 You can combine the BULK COLLECT clause with a FORALL statement, in which case, the SQL engine bulk-binds column values incrementally. In the following example, if collection depts has 3 elements, each of which causes 5 rows to be deleted, then collection enums has 15 elements when the statement completes: FORALL j IN depts.FIRST depts.LAST DELETE FROM emp WHERE deptno = depts(j) RETURNING empno BULK COLLECT INTO enums; The column values returned by each execution are added to the values returned previously. Related Topics FETCH Statement, SELECT Statement EXCEPTION_INIT Pragma 11-56 PL/SQL User’s Guide and Reference EXCEPTION_INIT Pragma The pragma EXCEPTION_INIT associates an exception name with an Oracle error number. That allows you to refer to any internal exception by name and to write a specific handler for it instead of using the OTHERS handler. For more information, see "Using EXCEPTION_INIT" on page 6-8. Syntax Keyword and Parameter Description error_number This is any valid Oracle error number. These are the same error numbers returned by the function SQLCODE. exception_name This identifies a user-defined exception previously declared within the current scope. PRAGMA This keyword signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They do not affect the meaning of a program; they simply convey information to the compiler. Usage Notes You can use EXCEPTION_INIT in the declarative part of any PL/SQL block, subprogram, or package. The pragma must appear in the same declarative part as its associated exception, somewhere after the exception declaration. Be sure to assign only one exception name to an error number. PRAGMA EXCEPTION_INIT ( exception_name , error_number ) ; exception_init_pragma EXCEPTION_INIT Pragma Language Elements 11-57 Example The following pragma associates the exception deadlock_detected with Oracle error 60: DECLARE deadlock_detected EXCEPTION; PRAGMA EXCEPTION_INIT(deadlock_detected, -60); BEGIN EXCEPTION WHEN deadlock_detected THEN handle the error END; Related Topics AUTONOMOUS_TRANSACTION Pragma, Exceptions, RESTRICT_REFERENCES Pragma, SERIALLY_RESUABLE Pragma, SQLCODE [...]... This keyword stands for all the exceptions not explicitly named in the exception-handling part of the block The use of OTHERS is optional and is allowed only as the last exception handler You cannot include OTHERS in a list of exceptions following the keyword WHEN statement This is an executable statement For the syntax of statement, see "Blocks" on page 11-10 11- 58 PL/SQL User’s Guide and Reference Exceptions... invalid_number OR STANDARD.INVALID_NUMBER THEN The exception-handling part of a PL/SQL block is optional Exception handlers must come at the end of the block They are introduced by the keyword EXCEPTION The exception-handling part of the block is terminated by the same keyword END that terminates the entire block An exception handler can reference only those variables that the current block can reference An... USING clause and the RETURNING INTO clause, the USING clause can contain only IN arguments 11-62 PL/SQL User’s Guide and Reference EXECUTE IMMEDIATE Statement At run time, bind arguments replace corresponding placeholders in the dynamic string So, every placeholder must be associated with a bind argument in the USING clause and/ or RETURNING INTO clause You can use numeric, character, and string literals... Expressions, LOOP Statements 11-66 PL/SQL User’s Guide and Reference exits both loops Expressions Expressions An expression is an arbitrarily complex combination of variables, constants, literals, operators, and function calls The simplest expression is a single variable The PL/SQL compiler determines the datatype of an expression from the types of the variables, constants, literals, and operators that comprise... necessary, PL/SQL rounds to the nearest integer The integers must specify a valid range of consecutive index numbers The SQL engine executes the SQL statement once for each index number in the range The expressions are evaluated only when the FORALL statement is first entered sql_statement This must be an INSERT, UPDATE, or DELETE statement that references collection elements 11 -82 PL/SQL User’s Guide and Reference. .. expression LIKE NOT pattern BETWEEN expression AND , IN ( expression character_expression character_constant_name character_function_call character_literal character_variable_name : : indicator_name host_variable_name character_constant_name character_function_call || character_literal character_variable_name : : host_variable_name 11- 68 PL/SQL User’s Guide and Reference indicator_name ) expression Expressions... identifies a cursor variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable Host cursor variables must be prefixed with a colon host_variable_name This identifies a variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable The datatype of the host variable must be implicitly convertible to the appropriate PL/SQL datatype Also, host variables must... (part_number, quantity); COMMIT; END; Related Topics Blocks, EXCEPTION_INIT Pragma, RAISE Statement 11-60 PL/SQL User’s Guide and Reference EXECUTE IMMEDIATE Statement EXECUTE IMMEDIATE Statement The EXECUTE IMMEDIATE statement prepares (parses) and immediately executes a dynamic SQL statement or anonymous PL/SQL block For more information, see Chapter 10, "Native Dynamic SQL" Syntax execute_immediate_statement... multiplication and division addition, subtraction, and concatenation PL/SQL evaluates operators of equal precedence in no particular order When parentheses enclose an expression that is part of a larger expression, PL/SQL evaluates the parenthesized expression first, then uses the result in the larger expression When parenthesized expressions are nested, PL/SQL evaluates the innermost expression first and the... character expression character expression date expression date expression numeric expression numeric expression Related Topics Assignment Statement, Constants and Variables, EXIT Statement, IF Statement, LOOP Statements 11-76 PL/SQL User’s Guide and Reference FETCH Statement FETCH Statement The FETCH statement retrieves rows of data one at a time from the result set of a multi-row query The data is stored . THEN handle the error END; Related Topics AUTONOMOUS_TRANSACTION Pragma, Exceptions, RESTRICT_REFERENCES Pragma, SERIALLY_RESUABLE Pragma, SQLCODE Exceptions 11- 58 PL/SQL User’s Guide and Reference Exceptions An. reference other PL/SQL variables within its scope. db_table_name cursor_name cursor_variable_name % ROWTYPE record_name % TYPE record_type_name rowtype Cursors 11-50 PL/SQL User’s Guide and Reference record_name This. the exception propagates normally. Exceptions 11-60 PL/SQL User’s Guide and Reference Example The following PL/SQL block has two exception handlers: DECLARE bad_emp_id EXCEPTION; bad_acct_no

Ngày đăng: 07/08/2014, 11:22

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan