Академический Документы
Профессиональный Документы
Культура Документы
http://www.orafaq.com/wiki/PL/SQL_FAQ
PL/SQL FAQ
From Oracle FAQ
PL/SQL FAQ - Oracle's Procedural Language extension to SQL:
Contents
1 What is PL/SQL and what is it used for? 2 What is the difference between SQL and PL/SQL? 3 Should one use PL/SQL or Java to code procedures and triggers? 4 How can one see if somebody modified any code? 5 How can one search PL/SQL code for a string/ key value? 6 How does one keep a history of PL/SQL code changes? 7 How can I protect my PL/SQL source code? 8 How can I know which stored PL/SQL code is wrapped? 9 Can one print to the screen from PL/SQL? 10 Can one read/write files from PL/SQL? 11 Can one call DDL statements from PL/SQL? 12 Can one use dynamic SQL statements from PL/SQL? 13 What is the difference between %TYPE and %ROWTYPE? 14 How does one get the value of a sequence into a PL/SQL variable? 15 Can one execute an operating system command from PL/SQL? 16 How does one loop through tables in PL/SQL? 17 How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy? 18 I can SELECT from SQL*Plus but not from PL/SQL. What is wrong? 19 What is a mutating and constraining table? 20 Can one pass an object/table as an argument to a remote procedure? 21 What is the difference between stored procedures and functions? 22 Is there a PL/SQL Engine in SQL*Plus? 23 Is there a limit on the size of a PL/SQL block? 24 What are the PL/SQL compiler limits for block, record, subquery and label nesting? 25 Can one COMMIT/ ROLLBACK from within a trigger?
1 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
sequential statements. To overcome this limitation, PL/SQL was created. PL/SQL is Oracle's Procedural Language extension to SQL. PL/SQL's language syntax, structure and data types are similar to that of Ada. Some of the statements provided by PL/SQL: Conditional Control Statements: IF ... THEN ... ELSIF ... ELSE ... END IF; CASE ... WHEN ... THEN ... ELSE ... END CASE; Iterative Statements: LOOP ... END LOOP; WHILE ... LOOP ... END LOOP; FOR ... IN [REVERSE] ... LOOP ... END LOOP; Sequential Control Statements: GOTO ...; NULL; The PL/SQL language includes object oriented programming techniques such as encapsulation, function overloading, information hiding (all but inheritance). PL/SQL is commonly used to write data-centric programs to manipulate data in an Oracle database. Example PL/SQL blocks:
/* Remember to SET SERVEROUTPUT ON to see the output */ BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); END; /
BEGIN -- A PL/SQL cursor FOR cursor1 IN (SELECT * FROM table1) -- This is an embedded SQL statement LOOP DBMS_OUTPUT.PUT_LINE('Column 1 = ' || cursor1.column1 || ', Column 2 = ' || cursor1.column2); END LOOP; END; /
2 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
Some of the differences: SQL is executed one statement at a time. PL/SQL is executed as a block of code. SQL tells the database what to do (declarative), not how to do it. In contrast, PL/SQL tell the database how to do things (procedural). SQL is used to code queries, DML and DDL statements. PL/SQL is used to code program blocks, triggers, functions, procedures and packages. You can embed SQL in a PL/SQL program, but you cannot embed PL/SQL within a SQL statement.
3 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
can detect code changes by looking at the TIMESTAMP and LAST_DDL_TIME column in the USER_OBJECTS dictionary view. Example:
SELECT OBJECT_NAME, TO_CHAR(CREATED, 'DD-Mon-RR HH24:MI') CREATE_TIME, TO_CHAR(LAST_DDL_TIME, 'DD-Mon-RR HH24:MI') MOD_TIME, STATUS FROM USER_OBJECTS WHERE LAST_DDL_TIME > '&CHECK_FROM_DATE';
Note: If you recompile an object, the LAST_DDL_TIME column is updated, but the TIMESTAMP column is not updated. If you modified the code, both the TIMESTAMP and LAST_DDL_TIME columns are updated.
How can one search PL/SQL code for a string/ key value?
The following query is handy if you want to know where certain tables, columns and expressions are referenced in your PL/SQL source code.
SELECT type, name, line FROM user_source WHERE UPPER(text) LIKE UPPER('%&KEYWORD%');
If you run the above query from SQL*Plus, enter the string you are searching for when prompted for KEYWORD. If not, replace &KEYWORD with the string you are searching for.
A better approach is to create an external CVS or SVN repository for the scripts that install the PL/SQL code. The canonical version of what's in the database must match the latest CVS/SVN version or else someone would
4 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
be cheating.
Please note: there is no legal way to unwrap a *.plb binary file. You are supposed to backup and keep your *.pls source files after wrapping them. However it is possible for skilled hackers to unwrap your wrapped Oracle PL/SQL code.
DBMS_OUTPUT is useful for debugging PL/SQL programs. However, if you print too much, the output buffer will overflow. In that case, set the buffer size to a larger value, eg.: set serveroutput on size 200000 If you forget to set serveroutput on type SET SERVEROUTPUT ON once you remember, and then EXEC NULL;. If you haven't cleared the DBMS_OUTPUT buffer with the disable or enable procedure, SQL*Plus will display the entire contents of the buffer when it executes this dummy PL/SQL block. To display an empty line, it is better to use new_line procedure than put_line with an empty string.
5 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
Copy and paste these examples to get you started: Write File
DECLARE fHandler UTL_FILE.FILE_TYPE; BEGIN fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'w'); UTL_FILE.PUTF(fHandler, 'Look ma, Im writing to a file!!!\n'); UTL_FILE.FCLOSE(fHandler); EXCEPTION WHEN utl_file.invalid_path THEN raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.'); END; /
Read File
DECLARE fHandler UTL_FILE.FILE_TYPE; buf varchar2(4000); BEGIN fHandler := UTL_FILE.FOPEN('MYDIR', 'myfile', 'r'); UTL_FILE.GET_LINE(fHandler, buf); dbms_output.put_line('DATA FROM FILE: '||buf); UTL_FILE.FCLOSE(fHandler); EXCEPTION WHEN utl_file.invalid_path THEN raise_application_error(-20000, 'Invalid path. Create directory or set UTL_FILE_DIR.'); END; /
NOTE: UTL_FILE was introduced with Oracle 7.3. Before Oracle 7.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.
6 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
DECLARE var VARCHAR2(100); BEGIN var := 'CREATE TABLE temp1(col1 NUMBER(2))'; EXECUTE IMMEDIATE var; END;
NOTE: The DDL statement in quotes should not be terminated with a semicolon. Users running Oracle versions below Oracle 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL).
One can also use the older DBMS_SQL package (V2.1 and above) to execute dynamic statements. Look at these examples:
CREATE OR REPLACE PROCEDURE DYNSQL AS cur integer; rc integer; BEGIN cur := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(cur, 'CREATE TABLE X (Y DATE)', DBMS_SQL.NATIVE); rc := DBMS_SQL.EXECUTE(cur); DBMS_SQL.CLOSE_CURSOR(cur); END; /
7 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
CREATE OR REPLACE PROCEDURE DEPARTMENTS(NO IN DEPT.DEPTNO%TYPE) AS v_cursor integer; v_dname char(20); v_rows integer; BEGIN v_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(v_cursor, 'select dname from dept where deptno > :x', DBMS_SQL.V7); DBMS_SQL.BIND_VARIABLE(v_cursor, ':x', no); DBMS_SQL.DEFINE_COLUMN_CHAR(v_cursor, 1, v_dname, 20); v_rows := DBMS_SQL.EXECUTE(v_cursor); loop if DBMS_SQL.FETCH_ROWS(v_cursor) = 0 then exit; end if; DBMS_SQL.COLUMN_VALUE_CHAR(v_cursor, 1, v_dname); DBMS_OUTPUT.PUT_LINE('Deptartment name: '||v_dname); end loop; DBMS_SQL.CLOSE_CURSOR(v_cursor); EXCEPTION when others then DBMS_SQL.CLOSE_CURSOR(v_cursor); raise_application_error(-20000, 'Unknown Exception Raised: '||sqlcode||' '||sqlerrm); END; /
%ROWTYPE %ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor. Examples:
DECLARE v_emp emp%ROWTYPE; BEGIN v_emp.empno := 10; v_emp.ename := 'XXXXXXX'; END; /
How does one get the value of a sequence into a PL/SQL variable?
8 of 15 30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
As you might know, one cannot use sequences directly from PL/SQL; Oracle prohibits this:
i := sq_sequence.NEXTVAL;
However, one can use embedded SQL statements to obtain sequence values:
select sq_sequence.NEXTVAL into :i from dual;
Note: This restriction has been removed in oracle 11g and the former syntax (direct assignment) can be used.
External Procedure Listeners: From Oracle 8 one can call external 3GL code in a dynamically linked library (DLL or shared object). One can write a library in C/ C++ to do whatever is required. Defining this C/C++ function to PL/SQL makes it executable. Look at this External Procedure (http://www.orafaq.com/scripts/c_src/extproc.txt) example. Using Java See example at http://www.orafaq.com/scripts/plsql/oscmd.txt DBMS_SCHEDULER In Oracle 10g and above, one can execute OS commands via the DBMS_SCHEDULER package. Look at this example:
9 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
-- Employee cursor all employees for a dept number CURSOR emp_cur (v_dept_no DEPT.DEPTNO%TYPE) IS SELECT ename FROM emp WHERE deptno = v_dept_no; BEGIN FOR dept_rec IN dept_cur LOOP dbms_output.put_line('Employees in Department '||TO_CHAR(dept_rec.deptno)); FOR emp_rec in emp_cur(dept_rec.deptno) LOOP dbms_output.put_line('...Employee is '||emp_rec.ename); END LOOP; END LOOP; END; /
How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy?
Contrary to popular belief, one should COMMIT less frequently within a PL/SQL loop to prevent ORA-1555 (Snapshot too old) errors. The higher the frequency of commit, the sooner the extents in the undo/ rollback segments will be cleared for new transactions, causing ORA-1555 errors. To fix this problem one can easily rewrite code like this:
FOR records IN my_cursor LOOP ...do some stuff... COMMIT; END LOOP; COMMIT;
... to ...
10 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
FOR records IN my_cursor LOOP ...do some stuff... i := i+1; IF mod(i, 10000) = 0 THEN COMMIT; END IF; END LOOP; COMMIT;
If you still get ORA-1555 errors, contact your DBA to increase the undo/ rollback segments. NOTE: Although fetching across COMMITs work with Oracle, is not supported by the ANSI standard. Issuing frequent commits is bad, bad, BAD! Its the WORST thing you can do just dont do it! In the following example I will create around 7 million rows and then attempt to update a portion of them serially. In addition, I will issue a commit every thousandth row. Example 1.1: Creating a somewhat large table
SQL> create table big_employee_table 2 as 3 select rownum as eid 4 , e.* 5 from hr.employees e 6 , dba_objects do; Table created. Elapsed: 00:00:12.23 SQL> select count(*) 2 from big_employee_table; COUNT(*) ---------7838713 Elapsed: 00:00:08.11
Before I go on, notice that Oracles Create Table As (CTAS) method blazed thru table creation. Thats 7.84 Million rows in 12.23 seconds. Sometimes, this is the very best method of updating large data sets. The following block updates 100,000 rows, serially, committing every 1000 rows: Example 1.2: Updating serially
SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 declare cursor c is select * from big_employee_table where rownum <= 100000; begin for r in c loop update big_employee_table set salary = salary * 1.03 where eid = r.eid; if mod ( r.eid, 1000 ) = 0 then commit; end if; end loop; end; /
Observe that the update took more time than I have patience for ;). At 20 minutes I killed the session. It is painfully slow and should never be done. Moreover, it chewed up an entire CPU core for the duration. If youre
11 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
only updating a few rows, why do it in PL/SQL at all? I like Tom Kytes approach (paraphrasing):
1. 2. 3. 4. Do If If If it in SQL. SQL cant do it, do it in PL/SQL. PL/SQL cant do it, do it in Java. Java cant do it ask yourself if it needs to be done.
The following block does the same work in bulk: Example 1.3: Updating in bulk and committing at the end
SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 declare type obj_rowid is table of rowid index by pls_integer; lr_rowid lr_salary obj_rowid; dbms_sql.number_table;
cursor c is select rowid rid , salary from big_employee_table where rownum <= 100000; begin open c; loop fetch c bulk collect into lr_rowid , lr_salary limit 500; for a in 1 .. lr_rowid.count loop lr_salary ( a ) := lr_salary ( a ) * 1.03; end loop; forall b in 1 .. lr_rowid.count update big_employee_table set salary = lr_salary ( b ) where rowid in ( lr_rowid ( b )); exit when c%notfound; end loop; close c; commit; -- there! not in the loop exception when others then rollback; dbms_output.put_line ( sqlerrm ); end; /
Notice that the update completed in 2 seconds! Ive seen faster but my two-gerbil sandbox machine doesnt have the power that our newer servers do. The point is that the update was incredibly fast and chewed up only 10% of one core. So, in answer to the question of how often should I commit? I say dont until you absolutely have to.
I can SELECT from SQL*Plus but not from PL/SQL. What is wrong?
PL/SQL respect object privileges given directly to the user, but does not observe privileges given through roles. The consequence is that a SQL statement can work in SQL*Plus, but will give an error in PL/SQL. Choose one
12 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
of the following solutions: Grant direct access on the tables to your user. Do not use roles!
GRANT select ON scott.emp TO my_user;
13 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
-- Database B: sends a PL/SQL table to database A CREATE OR REPLACE PROCEDURE pcalling IS TabX DBMS_SQL.VARCHAR2S@DBLINK2; BEGIN pcalled@DBLINK2(TabX); END; /
What are the PL/SQL compiler limits for block, record, subquery and label nesting?
The following limits apply: Level of Block Nesting: 255 Level of Record Nesting: 32 Level of Subquery Nesting: 254 Level of Label Nesting: 98
14 of 15
30/05/2011 14:49
http://www.orafaq.com/wiki/PL/SQL_FAQ
by definition an extension of the original DML operation. Changes made within triggers should thus be committed or rolled back as part of the transaction in which they execute. For this reason, triggers are NOT allowed to execute COMMIT or ROLLBACK statements (with the exception of autonomous triggers). Here is an example of what will happen when they do:
SQL> CREATE TABLE tab1 (col1 NUMBER); Table created. SQL> CREATE TABLE log (timestamp DATE, operation VARCHAR2(2000)); Table created. SQL> CREATE TRIGGER tab1_trig 2 AFTER insert ON tab1 3 BEGIN 4 INSERT INTO log VALUES (SYSDATE, 'Insert on TAB1'); 5 COMMIT; 6 END; 7 / Trigger created. SQL> INSERT INTO tab1 VALUES (1); INSERT INTO tab1 VALUES (1) * ERROR at line 1: ORA-04092: cannot COMMIT in a trigger ORA-06512: at "SCOTT.TAB1_TRIG", line 3 ORA-04088: error during execution of trigger 'SCOTT.TAB1_TRIG'
Autonomous transactions: As workaround, one can use autonomous transactions. Autonomous transactions execute separate from the current transaction. Unlike regular triggers, autonomous triggers can contain COMMIT and ROLLBACK statements. Example:
SQL> CREATE OR REPLACE TRIGGER tab1_trig 2 AFTER insert ON tab1 3 DECLARE 4 PRAGMA AUTONOMOUS_TRANSACTION; 5 BEGIN 6 INSERT INTO log VALUES (SYSDATE, 'Insert on TAB1'); 7 COMMIT; -- only allowed in autonomous triggers 8 END; 9 / Trigger created. SQL> INSERT INTO tab1 VALUES (1); 1 row created.
Note that with the above example will insert and commit log entries - even if the main transaction is rolled-back! Remember that an "autonomous_transaction" procedure/function/trigger is a whole transaction in itself and so it must end with a commit or a rollback statement. Retrieved from "http://www.orafaq.com/wiki/PL/SQL_FAQ" Category: Frequently Asked Questions This page was last modified on 26 May 2011, at 15:30. .:: Wiki Home :: Blogger Home :: Forum Home :: Contact :: Privacy ::.
15 of 15
30/05/2011 14:49