You are on page 1of 34

1.1.1 What are the difference between DDL, DML and DCL commands?

? DDL is Data Definition Language statements. Some examples:


CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed COMMENT - add comments to the data dictionary GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT command

DML is Data Manipulation Language statements. Some examples:


SELECT - retrieve data from the a database INSERT - insert data into a table UPDATE - updates existing data within a table DELETE - deletes all records from a table, the space for the records remain CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to data LOCK TABLE - control concurrency

DCL is Data Control Language statements. Some examples:


COMMIT - save work done SAVEPOINT - identify a point in a transaction to which you can later roll back ROLLBACK - restore database to original since the last COMMIT SET TRANSACTION - Change transaction options like what rollback segment to use

1.1.2 How does one escape special characters when building SQL queries? The LIKE keyword allows for string searches. The '_' wild card character is used to match exactly one character, '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL. Example: SELECT name FROM emp WHERE id LIKE '%\_%' ESCAPE '\'; Use two quotes for every one displayed. Example: SELECT 'Franks''s Oracle site' FROM DUAL; SELECT 'A ''quoted'' word.' FROM DUAL; SELECT 'A ''''double quoted'''' word.' FROM DUAL;

1.1.3 How does one eliminate duplicates rows from a table? Choose one of the following queries to identify or remove duplicate rows from a table leaving unique records in the table: Method 1: SQL> DELETE FROM table_name A WHERE ROWID > ( 2 SELECT min(rowid) FROM table_name B 3 WHERE A.key_values = B.key_values); Method 2: SQL> create table table_name2 as select distinct * from table_name1; SQL> drop table_name1; SQL> rename table_name2 to table_name1; Method 3: (thanks to Kenneth R Vanluvanee) SQL> Delete from my_table where rowid not in( SQL> select max(rowid) from my_table SQL> group by my_column_name ); Method 4: (thanks to Dennis Gurnick) SQL> delete from my_table t1 SQL> where exists (select 'x' from my_table t2 SQL> where t2.key_value1 = t1.key_value1 SQL> and t2.key_value2 = t1.key_value2 SQL> and t2.rowid > t1.rowid); Note: If you create an index on the joined fields in the inner loop, you, for all intents purposes, eliminate N^2 operations (no need to loop through the entire table on each pass by a record). This will speed-up th Note 2: If you are comparing NOT-NULL columns, use the NVL function. Remember that NULL is not equal to NULL. This should not be a problem as all key columns should be NOT NULL by definition. 1.1.4 How does one generate primary key values for a table? Create your table with a NOT NULL column (say SEQNO). This column can now be populated with unique values: SQL> UPDATE table_name SET seqno = ROWNUM; or use a sequences generator: SQL> CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 1; SQL> UPDATE table_name SET seqno = sequence_name.NEXTVAL; Finally, create a unique index on this column.

1.1.5 How does one get the time difference between two date columns Look at this example query: select floor(((date1-date2)*24*60*60)/3600) || ' HOURS ' || floor((((date1-date2)*24*60*60) floor(((date1date2)*24*60*60)/3600)*3600)/60) || ' MINUTES ' || round((((date1-date2)*24*60*60) floor(((date1-date2)*24*60*60)/3600)*3600 (floor((((date1-date2)*24*60*60) floor(((date1date2)*24*60*60)/3600)*3600)/60)*60))) || ' SECS ' time_difference from ... If you don't want to go through the floor and ceiling math, try this method (contributed by Erik Wile): select to_char(to_date('00:00:00','HH24:MI:SS') + (date1 - date2), 'HH24:MI:SS') time_difference from ... Note that the second query only uses the time-of-day portion of the date fields and thus will never return a value bigger than 23:59:59. 1.1.6 How does one add a day/hour/minute/second to a date value? The SYSDATE pseudo-column shows the current system date and time. Adding 1 to SYSDATE will advance the date by 1 day. Use fractions to add hours, minutes or seconds to the date. Look at these examples: SQL> select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual; SYSDATE SYSDATE+1/24 SYSDATE+1/1440 SYSDATE+1/86400 -------------------- --------------------------------------- -------------------03-Jul-2002 08:32:12 03-Jul-2002 09:32:12 03-Jul2002 08:33:12 03-Jul-2002 08:32:13 The following format is frequently used with Oracle Replication: select sysdate NOW, sysdate+30/(24*60*60) NOW_PLUS_30_SECS from dual; NOW NOW_PLUS_30_SECS

-------------------- -------------------03-JUL-2002 16:47:23 03-JUL-2002 16:47:53 1.1.7 How does one count different data values in a column? select dept, sum( decode(sex,'M',1,0)) MALE, sum( decode(sex,'F',1,0)) FEMALE, count(decode(sex,'M',1,'F',1)) TOTAL from my_emp_table group by dept;

1.1.8 How does one count/sum RANGES of data values in a column? A value x will be between values y and z if GREATEST(x, y) = LEAST(x, z). Look at this example: select f2, sum(decode(greatest(f1,59), least(f1,100), 1, 0)) "Range 60-100", sum(decode(greatest(f1,30), least(f1, 59), 1, 0)) "Range 30-59", sum(decode(greatest(f1, 0), least(f1, 29), 1, 0)) "Range 00-29" from my_table group by f2; For equal size ranges it might be easier to calculate it with DECODE(TRUNC(value/range), 0, rate_0, 1, rate_1, ...). Eg. select ename "Name", sal "Salary", decode( trunc(f2/1000, 0), 0, 0.0, 1, 0.1, 2, 0.2, 3, 0.31) "Tax rate" from my_table; 1.1.9 Can one retrieve only the Nth row from a table? Ravi Pachalla provided this solution to select the Nth row from a table: SELECT f1 FROM t1 WHERE rowid = ( SELECT rowid FROM t1 WHERE rownum <= 10 MINUS SELECT rowid FROM t1 WHERE rownum < 10); Alternatively... SELECT * FROM emp WHERE rownum=1 AND rowid NOT IN (SELECT rowid FROM emp WHERE rownum < 10); Please note, there is no explicit row order in a relational database. However, this query is quite fun and may even help in the odd situation.

1.1.10 Can one retrieve only rows X to Y from a table? To display rows 5 to 7, construct a query like this: SELECT * FROM tableX WHERE rowid in ( SELECT rowid FROM tableX WHERE rownum <= 7 MINUS SELECT rowid FROM tableX WHERE rownum < 5); Please note, there is no explicit row order in a relational database. However, this query is quite fun and may even help in the odd situation. 1.1.11 How does one select EVERY Nth row from a table? One can easily select all even, odd, or Nth rows from a table using SQL queries like this: Method 1: Using a subquery SELECT * FROM emp WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4) FROM emp); Method 2: Use dynamic views (available from Oracle7.2): SELECT * FROM ( SELECT rownum rn, empno, ename FROM emp ) temp WHERE MOD(temp.ROWNUM,4) = 0; Please note, there is no explicit row order in a relational database. However, these queries are quite fun and may even help in the odd situation. 1.1.12 How does one select the TOP N rows from a table? Form Oracle8i one can have an inner-query with an ORDER BY clause. Look at this example: SELECT * FROM (SELECT * FROM my_table ORDER BY col_name_1 DESC) WHERE ROWNUM < 10; Use this workaround with prior releases: SELECT * FROM my_table a WHERE 10 >= (SELECT COUNT(DISTINCT maxcol) FROM my_table b WHERE b.maxcol >= a.maxcol) ORDER BY maxcol DESC;

1.1.13 How does one code a tree-structured query? Tree-structured queries are definitely non-relational (enough to kill Codd and make him roll in his grave). Also, this feature is not often found in other database offerings. The SCOTT/TIGER database schema contains a table EMP with a self-referencing relation (EMPNO and MGR columns). This table is perfect for tesing and demonstrating tree-structured queries as the MGR column contains the employee number of the "current" employee's boss. The LEVEL pseudo-column is an indication of how deep in the tree one is. Oracle can handle queries with a depth of up to 255 levels. Look at this example: select LEVEL, EMPNO, ENAME, MGR from EMP connect by prior EMPNO = MGR start with MGR is NULL; One can produce an indented report by using the level number to substring or lpad() a series of spaces, and concatenate that to the string. Look at this example: select lpad(' ', LEVEL * 2) || ENAME ........ One uses the "start with" clause to specify the start of the tree. More than one record can match the starting condition. One disadvantage of having a "connect by prior" clause is that you cannot perform a join to other tables. The "connect by prior" clause is rarely implemented in the other database offerings. Trying to do this programmatically is difficult as one has to do the top level query first, then, for each of the records open a cursor to look for child nodes. One way of working around this is to use PL/SQL, open the driving cursor with the "connect by prior" statement, and the select matching records from other tables on a rowby-row basis, inserting the results into a temporary table for later retrieval. 1.1.14 How does one code a matrix report in SQL? Look at this example query with sample output: SELECT * FROM (SELECT job, sum(decode(deptno,10,sal)) sum(decode(deptno,20,sal)) sum(decode(deptno,30,sal)) sum(decode(deptno,40,sal)) FROM scott.emp GROUP BY job) ORDER BY 1; JOB DEPT40 DEPT10 DEPT20

DEPT10, DEPT20, DEPT30, DEPT40

DEPT30

--------- ---------- ---------- ------------------ANALYST 6000 CLERK 1300 1900 950 MANAGER 2450 2975 2850 PRESIDENT 5000 SALESMAN 5600 1.1.15 How does one implement IF-THEN-ELSE in a select statement? The Oracle decode function acts like a procedural statement inside an SQL statement to return different values or columns based on the values of other columns in the select statement. Some examples: select decode(sex, 'M', 'Male', 'F', 'Female', 'Unknown') from employees; select a, b, decode( abs(a-b), a-b, 'a > b', 0, 'a = b', 'a < b') from tableX; select decode( GREATEST(A,B), A, 'A is greater OR EQUAL than B', 'B is greater than A')... select decode( GREATEST(A,B), A, decode(A, B, 'A NOT GREATER THAN B', 'A GREATER THAN B'), 'A NOT GREATER THAN B')... Note: The decode function is not ANSI SQL and is rarely implemented in other RDBMS offerings. It is one of the good things about Oracle, but use it sparingly if portability is required. From Oracle 8i one can also use CASE statements in SQL. Look at this example: SELECT ename, CASE WHEN sal>1000 THEN 'Over paid' ELSE 'Under paid' END FROM emp; 1.1.16 How can one dump/ examine the exact content of a database column? SELECT DUMP(col1) FROM tab1 WHERE cond1 = val1;

DUMP(COL1) ------------------------------------Typ=96 Len=4: 65,66,67,32 For this example the type is 96, indicating CHAR, and the last byte in the column is 32, which is the ASCII code for a space. This tells us that this column is blank-padded. 1.1.17 Can one drop a column from a table? From Oracle8i one can DROP a column from a table. Look at this sample script, demonstrating the ALTER TABLE table_name DROP COLUMN column_name; command. Other workarounds: 1. SQL> update t1 set column_to_drop = NULL; SQL> rename t1 to t1_base; SQL> create view t1 as select <specific columns> from t1_base; 2. SQL> create table t2 as select <specific columns> from t1; SQL> drop table t1; SQL> rename t2 to t1; 1.1.18 Can one rename a column in a table? No, this is listed as Enhancement Request 163519. Some workarounds: 1. -- Use a view with correct column names... rename t1 to t1_base; create view t1 <column list with new name> as select * from t1_base; 2. -- Recreate the table with correct column names... create table t2 <column list with new name> as select * from t1; drop table t1; rename t2 to t1; 3. -- Add a column with a new name and drop an old column... alter table t1 add ( newcolame datatype ); update t1 set newcolname=oldcolname; alter table t1 drop column oldcolname; 1.1.19 How can I change my Oracle password? Issue the following SQL command: ALTER USER <username> IDENTIFIED BY <new_password> /

From Oracle8 you can just type "password" from SQL*Plus, or if you need to change another user's password, type "password user_name". 1.1.20 How does one find the next value of a sequence? Perform an "ALTER SEQUENCE ... NOCACHE" to unload the unused cached sequence numbers from the Oracle library cache. This way, no cached numbers will be lost. If you then select from the USER_SEQUENCES dictionary view, you will see the correct high water mark value that would be returned for the next NEXTVALL call. Afterwards, perform an "ALTER SEQUENCE ... CACHE" to restore caching. You can use the above technique to prevent sequence number loss before a SHUTDOWN ABORT, or any other operation that would cause gaps in sequence values. 1.1.21 Workaround for snapshots on tables with LONG columns You can use the SQL*Plus COPY command instead of snapshots if you need to copy LONG and LONG RAW variables from one location to another. Eg: COPY TO SCOTT/TIGER@REMOTE CREATE IMAGE_TABLE USING SELECT IMAGE_NO, IMAGE FROM IMAGES; Note: If you run Oracle8, convert your LONGs to LOBs, as it can be replicated.

1.1.21.1 What built-in functions/operators are available for manipulating strings? The most useful ones are LENGTH, SUBSTR, INSTR, and ||:
LENGTH(str) returns the length of str in characters. SUBSTR(str,m,n) returns a portion of str, beginning at character m, n characters long. If n is omitted, all characters to the end of str will be returned. INSTR(str1,str2,n,m) searches str1 beginning with its n-th character for the m-th occurrence of str2 and returns the position of the character in str1 that is

the first character of this occurrence. str1 || str2 returns the concatenation of str1 and str2.

The example below shows how to convert a string name of the format 'last, first' into the format 'first last':
|| || SUBSTR(name, INSTR(name,',',1,1)+2) ' ' SUBSTR(name, 1, INSTR(name,',',1,1)-1)

For case-insensitive comparisons, first convert both strings to all upper case using Oracle's built-in function upper() (or all lower case using lower()).

1.1.21.2 1.1.21.3 Can I print inside a PL/SQL program? Strictly speaking PL/SQL doesn't currently support I/O. But, there is a standard package DBMS_OUTPUT that lets you do the trick. Here is an example:
-- create the procedure CREATE PROCEDURE nothing AS BEGIN DBMS_OUTPUT.PUT_LINE('I did nothing'); -- use TO_CHAR to convert variables/columns -- to printable strings END; . RUN; -- set output on; otherwise you won't see anything SET SERVEROUTPUT ON; -- invoke the procedure BEGIN nothing; END; . RUN;

Then you should see "I did nothing" printed on your screen. is very useful for debugging PL/SQL programs. However, if you print too much, the output buffer will overflow (the default buffer size is 2KB). In that case, you can set the buffer size to a larger value, e.g.:
DBMS_OUTPUT BEGIN DBMS_OUTPUT.ENABLE(10000); nothing; END; . RUN;

1.1.21.4 1.1.21.5 Is it possible to write a PL/SQL procedure that takes a table name as input and does something with that table? For pure PL/SQL, the answer is no, because Oracle has to know the schema of the table in order to compile the PL/SQL procedure. However, Oracle provides a package called DBMS_SQL, which allows PL/SQL to execute SQL DML as well as DDL dynamically at run time. For example, when called, the following stored procedure drops a specified database table:
CREATE PROCEDURE drop_table (table_name IN VARCHAR2) AS cid INTEGER; BEGIN -- open new cursor and return cursor ID cid := DBMS_SQL.OPEN_CURSOR; -- parse and immediately execute dynamic SQL statement -- built by concatenating table name to DROP TABLE command DBMS_SQL.PARSE(cid, 'DROP TABLE ' || table_name, dbms_sql.v7);

-- close cursor DBMS_SQL.CLOSE_CURSOR(cid); EXCEPTION -- if an exception is raised, close cursor before exiting WHEN OTHERS THEN DBMS_SQL.CLOSE_CURSOR(cid); -- reraise the exception RAISE; END drop_table; . RUN;

1.1.21.6 1.1.21.7 What is the correct syntax for ordering query results by row-type objects? As a concrete example, suppose we have defined an object type PersonType with an ORDER MEMBER FUNCTION, and we have created a table Person of PersonType objects. Suppose you want to list all PersonType objects in Person in order. You'd probably expect the following to work:
SELECT * FROM Person p ORDER BY p;

But it doesn't. Somehow, Oracle cannot figure out that you are ordering PersonType objects. Here is a hack that works:
SELECT * FROM Person p ORDER BY DEREF(REF(p));

1.1.21.8 1.1.21.9 How do I kill long-running queries in sqlplus, Pro*C, and JDBC? Sometimes it is necessary to stop long-running queries, either because they take longer to run than you'd like, or because you realize you've made a mistake. It is important kill off such queries properly so that they don't take up extra computational resources and prevent others from using the system, especially near project deadlines when resources are most strained. As a general precautionary measure, please be sure to test your queries under sqlplus prompt before running them through CGI or JDBC. It is much easier to kill a query in sqlplus than in CGI or JDBC. If your test query takes a long time to run under sqlplus, you can simply hit Ctrl-C to terminate it. Never close an ssh or telnet or xterm window without properly logging out. Always quit your programs (including sqlplus), stop Java servlets, and type "exit" or "logout" to quit. If you force-close your ssh/telnet/xterm window, there may still be processes running in the background, and you may be taking up system resources without knowing it. If, for some reason, you cannot logout normally (for example, the system is not responding), you should open another window, login to the same machine where you have the problem, and kill the processes that is causing trouble:

Type "ps -aef | grep [username]" to find the Process IDs of your processes (replace [username] with your leland user name), and kill the processes you want to terminate using "kill [processID]". Always use the "kill" command without the -9 flag first. Use -9 flag only if you cannot kill it otherwise. If you closed the window by mistake and do not remember which sweet hall machine you were logged into, open another window immediately and log into any sweet hall machine, then type "sweetfinger [username]" (replace [username] with your actual leland user name). It will give you the machine names you were on a few minutes ago. Then, log in to the appropriate machine and kill your processes there. If you issued a query through JDBC that is taking a long time to execute and you want to kill it, you should stop your Java servlet. In most cases this will kill the query. You can also use the setQueryTimeout([time in seconds]) method on a statement object to stop queries that run too long. If you issued a query through CGI that is taking a long time to execute, normally the CGI service will kill it for you within 10 seconds. However, the above occasionally fails to work, and we do not know of any better way of killing runaway queries issued by JDBC or CGI (other than asking the administrator to kill them for you). That's why we ask you to always test your queries under sqlplus first. It is much easier to kill queries there. 1.1.21.10 1.1.21.11 In Pro*C, why do I get a strange "break outside loop or switch" error message? If you get an error message
"break" outside loop or switch

when compiling your Pro*C program, chances that you have the following statement somewhere before a loop:
EXEC SQL WHENEVER NOT FOUND DO break;

After the loop, you should insert the following statement:


EXEC SQL WHENEVER NOT FOUND CONTINUE;

This would cancel the previous WHENEVER statement. If you do not do this, you may get the error message at subsequent SQL calls.

Q: How to turn off "the number of records affected" message comes after each SQL in PL/SQL? set feedback off Q: How to declare variables and constants? <variable_name> <data_type> [not null][:=<initial_value>]; <constant_name> constant <data_type> := <value>; Q: How to assign values? <variable_name> := <value>; Q: How to write comments? -- for one line comment /* multi line comment */ Q: How to write conditional statement? if <condition> then <statements>; elsif <condition> then <statements>; elsif <condition> then <statements>; else <statements>; END IF; Q: How to write loop? LOOP <statements>; END LOOP; WHILE <condition> LOOP <statements>; END LOOP; FOR <loop_counter> IN [REVERSE] <lower>..<upper> LOOP <statements>; END LOOP; Q: How to terminate a loop? exit; exit when <condition>;

Q: How to handle exceptions? EXCEPTION <statements>; WHEN <exception_ID_or_NAME> THEN <statements>; WHEN <exception_ID_or_NAME> THEN <statements>; WHEN OTHERS THEN <statements>; Q: What is the list of predefined exceptions? ACCESS_INTO_NULL COLLECTION_IS_NULL CURSOR_ALREADY_OPEN DUP_VAL_ON_INDEX INVALID_CURSOR INVALID_NUMBER LOGIN_DENIED NO_DATA_FOUND NOT_LOGGED_ON PROGRAM_ERROR ROWTYPE_MISMATCH SELF_IS_NULL STORAGE_ERROR SUBSCRIPT_BEYOND_COUNT SUBSCRIPT_OUTSIDE_LIMIT SYS_INVALID_ROWID TIMEOUT_ON_RESOURCE TOO_MANY_ROWS VALUE_ERROR ZERO_DIVIDE Q: How to get the exception oracle error or sql-code value? ACCESS_INTO_NULL ORA-06530 -6530 COLLECTION_IS_NULL ORA-06531 -6531 CURSOR_ALREADY_OPEN ORA-06511 -6511 DUP_VAL_ON_INDEX ORA-00001 -1 INVALID_CURSOR ORA-01001 -1001 INVALID_NUMBER ORA-01722 -1722 LOGIN_DENIED ORA-01017 -1017 NO_DATA_FOUND ORA-01403 100 NOT_LOGGED_ON ORA-01012 -1012 PROGRAM_ERROR ORA-06501 -6501 ROWTYPE_MISMATCH ORA-06504 -6504 SELF_IS_NULL ORA-30625 -30625 STORAGE_ERROR ORA-06500 -6500 SUBSCRIPT_BEYOND_COUNT ORA-06533 -6533 SUBSCRIPT_OUTSIDE_LIMIT ORA-06532 -6532 SYS_INVALID_ROWID ORA-01410 -1410 TIMEOUT_ON_RESOURCE ORA-00051 -51 TOO_MANY_ROWS ORA-01422 -1422 VALUE_ERROR ORA-06502 -6502 ZERO_DIVIDE ORA-01476 -1476

Q: How are the predefined exceptions raised? Exception Raised when ... ACCESS_INTO_NULL Your program attempts to assign values to the attributes of an uninitialized (atomically null) object. COLLECTION_IS_NULL Your program attempts to apply collection methods other than EXISTS to an uninitialized (atomically null) nested table or varray, or the program attempts to assign values to the elements of an uninitialized nested table or varray. CURSOR_ALREADY_OPEN Your program attempts to open an already open cursor. A cursor must be closed before it can be reopened. A cursor FOR loop automatically opens the cursor to which it refers. So, your program cannot open that cursor inside the loop. DUP_VAL_ON_INDEX Your program attempts to store duplicate values in a database column that is constrained by a unique index. INVALID_CURSOR Your program attempts an illegal cursor operation such as closing an unopened cursor. INVALID_NUMBER In a SQL statement, the conversion of a character string into a number fails because the string does not represent a valid number. (In procedural statements, VALUE_ERROR is raised.) LOGIN_DENIED Your program attempts to log on to Oracle with an invalid username and/or password. NO_DATA_FOUND A SELECT INTO statement returns no rows, or your program references a deleted element in a nested table or an uninitialized element in an index-by table. SQL aggregate functions such as AVG and SUM always return a value or a null. So, a SELECT INTO statement that calls a aggregate function will never raise NO_DATA_FOUND. The FETCH statement is expected to return no rows eventually, so when that happens, no exception is raised. NOT_LOGGED_ON Your program issues a database call without being connected to Oracle. PROGRAM_ERROR PL/SQL has an internal problem. ROWTYPE_MISMATCH The host cursor variable and PL/SQL cursor variable involved in an assignment have incompatible return types. For example, when an open host cursor variable is passed to a stored subprogram, the return types of the actual and formal parameters must be compatible. SELF_IS_NULL Your program attempts to call a MEMBER method on a null instance. That is, the built-in parameter SELF (which is always the first parameter passed to a MEMBER method) is null. STORAGE_ERROR PL/SQL runs out of memory or memory has been corrupted. SUBSCRIPT_BEYOND_COUNT Your program references a nested table or varray element using an index number larger than the number of elements in the collection. SUBSCRIPT_OUTSIDE_LIMIT Your program references a nested table or varray element using an index number (-1 for example) that is outside the legal range.

SYS_INVALID_ROWID The conversion of a character string into a universal rowid fails because the character string does not represent a valid rowid. TIMEOUT_ON_RESOURCE A time-out occurs while Oracle is waiting for a resource. TOO_MANY_ROWS A SELECT INTO statement returns more than one row. VALUE_ERROR An arithmetic, conversion, truncation, or size-constraint error occurs. For example, when your program selects a column value into a character variable, if the value is longer than the declared length of the variable, PL/SQL aborts the assignment and raises VALUE_ERROR. In procedural statements, VALUE_ERROR is raised if the conversion of a character string into a number fails. (In SQL statements, INVALID_NUMBER is raised.) ZERO_DIVIDE Your program attempts to divide a number by zero. Q: How to write anonymous blocks? declare -- declaration Section -- data, procedure,function declarations BEGIN <statements>; [exception_handling] END; Q: How to write anonymous procedures? PROCEDURE <procedure_name> [(<parameter>,...,<parameter>)] IS [<declerations>] BEGIN <statements>; [exception_handling] END; syntax of paramater is: <paramater>: <parameter_name> [in|out|in out] <data_type> Q: How to write anonymous functions? function <procedure_name> [(<parameter>,...,<parameter>)] RETURN <data_type> IS [<declerations>] BEGIN <statements>; [exception_handling] END; syntax of paramater is: <paramater>: <parameter_name> [in|out|in out] <data_type> Q: How to write stored procedures? CREATE [or replace] PROCEDURE <procedure_name> [(<parameter>,...,<parameter>)] AS [<declerations>] BEGIN <statements>; [exception_handling] END;

syntax of paramater is: <paramater>: <parameter_name> [in|out|in out] <data_type> Q: How to write stored functions? CREATE [OR REPLACE] PROCEDURE <procedure_name> [(<parameter>,...,<parameter>)] RETURN <datat_type> AS [<declerations>] BEGIN <statements>; [exception_handling] END; syntax of paramater is: <paramater>: <parameter_name> [in|out|in out] <data_type> Q: How to access to attributes? {<variable_name>|<cursor_name>|<table_name>.<field_name>|}%TYPE; {<variable_name>|<cursor_name>|<table_name>.<field_name>|}%ROWTYPE; Q: How to run stored procedure? EXECUTE <procedure_name>; CALL <procedure_name> Q: How to use DDL commands in PL/SQL? EXECUTE IMMEDIATE '<DDL command>'; Q: How to create more then one trigger in a command file? Seperate each trigger definition with /. Q: How to set a variable to next sequence number? SELECT <sequence_name>.nextval INTO <variable_name> FROM DUAL; Q: How to resume a PL/SQL error? Use inner-block. Example; -- following will not resume if ZERO_DIVIDE occurs DECLARE pe_ratio NUMBER(3,1); BEGIN DELETE FROM stats WHERE symbol = 'XYZ'; SELECT price / NVL(earnings, 0) INTO pe_ratio FROM stocks WHERE symbol = 'XYZ'; INSERT INTO stats (symbol, ratio) VALUES ('XYZ', pe_ratio); EXCEPTION WHEN ZERO_DIVIDE THEN null; -- or do something END; -- this code, on the other hand will resume when ZERO_DIVIDE occurs DECLARE pe_ratio NUMBER(3,1); BEGIN DELETE FROM stats WHERE symbol = 'XYZ'; BEGIN SELECT price / NVL(earnings, 0) INTO pe_ratio FROM stocks WHERE symbol = 'XYZ'; EXCEPTION WHEN ZERO_DIVIDE THEN

null; -- do something END; INSERT INTO stats (symbol, ratio) VALUES ('XYZ', pe_ratio); EXCEPTION WHEN OTHERS THEN null; -- do something END; Q: What is the default directory for sql files called from PL/SQL? When you run a sql file from PL/SQL it searches from {ORACLE_HOME}\bin by default. Of course you can always specify full path to the sql file. I don't know how to change the default by now. Q: How to change PL/SQL prompt and initial environment variables? Create an sql file login.sql and copy it to {ORACLE_HOME}\bin directory of your client software. Q: How to use SQL Code SQLCODE and SQL Message SQLERRM of an error code? Set them to a variable before using. SQLERRM(<errCode>) is to get the message for a secific error. Q: How to handle errors in DECLARE section? Handlers in the current block cannot catch the raised exception because an exception raised in a declaration propagates immediately to the enclosing block Q: How to ignore "Input truncated to # character" message? Leave an empty line at the end of the sql file Q: How to connect from a Unix PL/SQL Client to an Oracle Server running on Windows box? You need to create so called a tns file which keeps a list of service names to the different servers. The default name is tnsnames.ora. You can either create this file by typing yourself or you can use visual tools to create one; for example, Oracle NetManager. Don't forget to tell the location of this file to your Unix client. To accomplish this mapping and set some other system variables you need to have following lines in your shell loging profile, such as .cshrc for Courne Shell. Once you set up your environment variables and tnsnames.ora you can use it in your connection string. example: >plsql msanver@myhomeconn password:****** SQL> Q: How to drop all objects in my database automatically without writing explicit DROP statement for each object? Here is a sample anonymous procedure to do this for you. Copy file. If it is under default sql script folder simply type following at PL/SQL command: SQL>@dropall.sql otherwise type: SQL>@path\dropall.sql

1.1.21.12

Can I Update From Another Table ?

Yes. For example, if we had a table DEPT_SUMMARY, we could update the number of employees field as follows:
update DEPT_SUMMARY s set NUM_EMPS = (

);

select count(1) from EMP E where E.DEPTNO = S.DEPTNO

Back to Top of File 1.1.21.13 Can I remove duplicate rows ?

Yes, using the ROWID field. The ROWID is guaranteed unique. There are many variations on this theme, but the logic is to delete all but one record for each key value.
delete from EMP E where not E.ROWID = ( select min(F.ROWID) from EMP F where F.EMP_ID = E.EMP_ID );

Back to Top of File 1.1.21.14 Can I implement Tree Structured Queries ?

Yes! This is commonly asked by those migrating from non-RDBMS apps. This is definitely non-relational (enough to kill Codd and then make him roll in his grave) and is a feature I have not seen in the competition. The definitive example is in the example SCOTT/TIGER database, when looking at the EMP table (EMPNO and MGR columns). The MGR column contains the employee number of the "current" employee's boss. You have available an extra pseudo-column, LEVEL, that says how deep in the tree you are. Oracle can handle queries with a depth up to 255.
select LEVEL, EMPNO, ENAME, MGR from EMP connect by prior EMPNO = MGR start with MGR is NULL;

You can get an "indented" report by using the level number to substring or lpad a series of spaces and concatenate that to the string.
select lpad(' ', LEVEL * 2) || ENAME ........

You use the start with clause to specify the start of the tree(s). More than one record can match the starting condition.

One disadvantage of a "connect by prior" is that you cannot perform a join to other tables. Still, I have not managed to see anything else like the "connect by prior" in the other vendor offerings and I like trees. Even trying to doing this programmatically in embedded SQL is difficult as you have to do the top level query, for each of them open a cursor to look for lower level rows, for each of these....... soon you blow the cursor limit for your installation. The way around this is to use PL/SQL, open the driving cursor with the "connect by prior" statement, and the select matching records from other tables on a row-by-row basis, inserting the results into a temporary table for later retrieval. Note that you can't trick Oracle by using CONNECT BY PRIOR on a view that does the join. Back to Top of File 1.1.21.15 How can I get information on the row based on group information ?

Imagine we have the EMP table and want details on the employee who has the highest salary. You need to use a subquery.
select e.ENAME, e.EMPNO, e.SAL from EMP e where e.SAL in ( select max (e2.SAL) from EMP e2 );

You could get similar info on employees with the highest salary in their departments as follows
select e.ENAME, e.DEPTNO, e.SAL from EMP e where e.SAL = ( select max (e2.SAL) from EMP e2 where e2.DEPTNO = e.DEPTNO );

Back to Top of File 1.1.21.16 How can I get a name for a temporary table that will not clash ?

Use a sequence, and use the number to help you build the temporary table name. Note that SQL-92 is developing specific constructs for using temporary tables.

Back to Top of File 1.1.21.17 How can I discover what tables, columns, etc are there ?

Oracle maintains a live set of views that you can query to tell you what you have available. In V6, the first two to look at are DICT and DICT_COLUMNS which act as a directory of the other dictionary views. It is a good idea to be familiar with these. Not all of these views are accessible by all users. If you are a DBA you should also create private DBA synonyms by running $ORACLE_HOME/rdbms/admin/dba_syn.sql in your account. Back to Top of File 1.1.21.18 How can I rename a column ?

There is no way a column can be renamed using normal SQL. It can be done carefully by the DBA playing around with internal SYS dictionary tables and bouncing the database, but this is not supported. (I have successfully done it in V4 thru V7). Do backup the database first unless you feel brave. I've written a quick and dirty script rncol.sql to do this. If you can't figure out how to use it from the source you definitely should not run it. You can use a similar dirty trick for changing ownership of tables if storage space is limited. Back to Top of File 1.1.21.19 Is there a formatter for SQL or PL/SQL ?

There are a number of "beautifiers" for various program languages. The cb and indent programs for the C language spring to mind (although they have slightly different conventions). As far as I know there is no PD formatter for SQL available. Given that there are PD general SQL parsers and that the SQL standards are drafted in something close to BNF, maybe someone could base a reformatter based on the grammar. Note that you CANNOT use cb and indent with Pro*C as both these programs will screw up the embedded SQL code. I have recently heard that Kumaran Systems (see Vendor list) have a Forms PL/SQL and SQL formatter, but I do not now if they have unbundled it. Back to Top of File

1.1.21.20

How come records for the date I want are missing ?

You are trying to retrieve data based on something like:

SELECT fld1, fld2 FROM tbl WHERE date_field = '18-jun-60'

You *know* there are records for that day - but none of them are coming back to you. What has happened is that your records are not set to midnight (which is the default value if time of day not specified). You can either use to_char and to_date functions, which can be a bad move regarding SQL performance, or you can say
WHERE date_field >= '18-jun-60' AND date_field < '19-jun-60'

An alternative could be something like


WHERE date_field between '18-jun-1960' AND to_date('23:59:59 18-jun-60', 'HH24:......YY') ;

Back to Top of File 1.1.21.21 How can I interpret a two digit year after 2000 ?

When converting to dates from characters when you only have two characters for the year, the picture format "RR" will be interpreted as the year based on a guess that that date is between 1950 and 2049. Back to Top of File 1.1.21.22 What are these V$ tables?

There are a number of tables/views beginning with V$ that hold gory details for performance monitoring. These are not guaranteed to be stable from minor release to minor release and are for DBAs only. There are usually no real underlying tables (unlike SYS.OBJ$) and are dummied up by the RDBMS kernel software in much the same way that UNIX System V.4 dummies up the files in the /proc or /dev/proc directories. If you have any code depending on these (and the widely used tools supplied by Oracle but unsupported are in this category) then you need to verify that everything works each time you upgrade your database. And when a major revision changes, all bets are off.

Back to Top of File

1.1.21.23

How do I get a top ten ?

This question often gets the response WHERE ROWNUM <= 10 but this will not work (except accidentally) because the ROWNUM pseudocolumn is generated before the ORDER or WHERE clauses come into effect. One elegant SQL-only approach (although it will be a bitch on a large table) was suggested by Stowe@aol.com
select a.ordered_column, a.other_stuff from table_name a where 10 > ( select count(1) from table_name b where b.ordered_column < a.ordered_column ) order by a.ordered_columnl;

I do not believe that straight SQL is the way to go for such problems when you have PL/SQL available. My approach is to use PL/SQL instead (in SQL*Plus):
variable tenthsal number declare n number; cursor c1 is select SAL from EMP order BY SAL desc; begin open c1; for n in 1..10 loop fetch c1 into :tenthsal; end loop; close c1; end: / select * from EMP where SAL <= :tenthsal order by SAL desc;

Late news: index descending hint to SQL works if you use a dummy restriction to force use of the index. Needs V7, etc. Back to Top of File

1.1.21.24

How do control which rollback segment I use ?

In SQL, you may need to control the rollback segment used as the default rollback segment may be too small for the required transaction, or you may want to ensure that your transaction runs in a special rollback segment, unaffected by others. The statement is as follows: SET TRANSACTION USE ROLLBACK SEGMENT segment_name; On a related note, if all you are doing are SELECTS, it is worth telling the database of this using the following: SET TRANSACTION READ ONLY; Both these statements must be the first statement of the transaction. Back to Top of File

1.1.21.25

How do I order a union ?

(Governments around the world have been trying to figure this one out). Use the column number. Say we are getting a list of names and codes and want it ordered by the name, using both EMP and DEPT tables:
select DEPTNO, DNAME from DEPT union select EMPNO, ENAME from EMP order by 2;

Back to Top of File

1.1.21.26

Who are SCOTT, SYSTEM and SYS ?

These three users are common in many databases. See the glossary entries under SCOTT, SCOTT and SYS. Another common user/password is PLSQL/SUPERSECRET used for PL/SQL demo stuff.

Back to Top of File

1.1.21.27

How can I avoid blowing rollback segments ?

The simple answer is make sure you have them big enough and keep your transactions small, but that is being a smartarse. More recent versions of Oracle have an option for the session that you can set that commits every so many DML statements. This is OK except for where you are doing your work in a single statement rather than using PL/SQL and a loop construct. Imagine you have a HUGE table and need to update it, possibly updating the key. You cannot update it in one go because your rollback segments are too small. You cannot open a cursor and commit every n records, because usually the cursor will close. You cannot have a number of updates of a few records each because the keys may change causing you to visit records more than once. The solution I have used was to have one process select ROWID from the appropriate rows and pump these (via standard I/O) to another process that looped around reading ROWIDs from standard input, updating the appropriate record and committing every 10 records or so. This was very easy to program and also was quite fast in execution. The number of locks and size of rollback segments required was minimal. If you are writing in Pro*C and use MODE=ORACLE, there are ways around it too, but not if you are using MODE=ANSI. Back to Top of File

1.1.21.28

How can I restore passwords ?

OK, so this is really a DBA question, but it is worth putting in here because it involves SQL regardless of interface. First, look at the PASSWORD column in DBA_USERS. It looks like gobbledygook because it is an encrypted password. However you can use this if you have saved it somewhere else. Say you want to impersonate a user in a batch run overnight. First stash the gobbledygook password away somewhere, grant connect to the user identified by some password you know and then run your batches using the new known password.

To restore the password to what it was use the following syntax (which I think is undocumented).
grant connect to SCOTT identified by passwords GOBBLEDYGOOK; Note especially the S on the end of PASSWORDS.

Back to Top of File

1.2 Not Formatted Yet


Z.23. Who do various access methods compare ? How you organize your SQL and indices controls what access methods will be used. The following ranking is valid for V6. I do not know about V7. QUERY PATH RANKING (lowest rank is the best) Rank Path 1 ROWID = constant 2 Unique index column(s) = constant(s) 3 Entire unique contatenated index = constant 4 Entire cluster key = corresponding key in another table in same cluster 5 Entire cluster key = constant 6 Entire non-unique contatenated index = constant 7 Non-unique single column index merge 8 Most leading concatenated index = constant 9 Index column BETWEEN low AND hi or LIKE 'C%' 10 Sort/merge (joins only) 11 MAX/MIN of single indexed column 12 ORDER BY entire index 13 Full table scans 14 Unindexed column = constant or column IS NULL or column LIKE '%C%' A fuller discussion of this is in the document by Tina London, discussed elsewhere.

1.2.1

What is PL/SQL and what is it used for? 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. 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. Back to top of file

1.2.2

Should one use PL/SQL or Java to code procedures and triggers?

Internally the Oracle database supports two procedural languages, namely PL/SQL and Java. This leads to questions like "Which of the two is the best?" and "Will Oracle ever desupport PL/SQL in favour of Java?".

Many Oracle applications are based on PL/SQL and it would be difficult of Oracle to ever desupport PL/SQL. In fact, all indications are that PL/SQL still has a bright future ahead of it. Many enhancements are still being made to PL/SQL. For example, Oracle 9iDB supports native compilation of Pl/SQL code to binaries. PL/SQL and Java appeal to different people in different job roles. The following table briefly describes the difference between these two language environments: PL/SQL: Data centric and tightly integrated into the database Proprietary to Oracle and difficult to port to other database systems Data manipulation is slightly faster in PL/SQL than in Java Easier to use than Java (depending on your background)

Java: Open standard, not proprietary to Oracle Incurs some data conversion overhead between the Database and Java type systems Java is more difficult to use (depending on your background) Back to top of file

1.2.3

How can one see if somebody modified any code?

Code for stored procedures, functions and packages is stored in the Oracle Data Dictionary. One can detect code changes by looking at the 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'; Back to top of file

1.2.4

How can one search PL/SQL code for a string/ key value?

The following query is handy if you want to know where a certain table, field or expression is referenced in your PL/SQL source code. SELECT TYPE, NAME, LINE FROM USER_SOURCE WHERE UPPER(TEXT) LIKE '%&KEYWORD%'; Back to top of file

1.2.5

How can one keep a history of PL/SQL code changes?

One can build a history of PL/SQL code changes by setting up an AFTER CREATE schema (or database) level trigger (available from Oracle 8.1.7). This way one can easily revert to previous code should someone make any catastrophic changes. Look at this example: CREATE TABLE SOURCE_HIST -- Create history table AS SELECT SYSDATE CHANGE_DATE, USER_SOURCE.* FROM USER_SOURCE WHERE 1=2; CREATE OR REPLACE TRIGGER change_hist hist table -- Store code in

AFTER CREATE ON SCOTT.SCHEMA -- Change SCOTT to your schema name DECLARE BEGIN if DICTIONARY_OBJ_TYPE in ('PROCEDURE', 'FUNCTION', 'PACKAGE', 'PACKAGE BODY', 'TYPE') then -- Store old code in SOURCE_HIST table INSERT INTO SOURCE_HIST SELECT sysdate, user_source.* FROM USER_SOURCE WHERE TYPE = DICTIONARY_OBJ_TYPE AND NAME = DICTIONARY_OBJ_NAME; end if; EXCEPTION WHEN OTHERS THEN raise_application_error(-20000, SQLERRM); END; / show errors Back to top of file

1.2.6

How can I protect my PL/SQL source code?

PL/SQL V2.2, available with Oracle7.2, implements a binary wrapper for PL/SQL programs to protect the source code. This is done via a standalone utility that transforms the PL/SQL source code into portable binary object code (somewhat larger than the original). This way you can distribute software without having to worry about exposing your proprietary algorithms and methods. SQL*Plus and SQL*DBA will still understand and know how to execute such scripts. Just be careful, there is no "decode" command available. The syntax is: wrap iname=myscript.sql oname=xxxx.plb Back to top of file

1.2.7

Can one print to the screen from PL/SQL?

One can use the DBMS_OUTPUT package to write information to an output buffer. This buffer can be displayed on the screen from SQL*Plus if you issue the SET SERVEROUTPUT ON; command. For example: set serveroutput on begin dbms_output.put_line('Look Ma, I can print from PL/SQL!!!'); end; / 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. Back to top of file

1.2.8

Can one read/write files from PL/SQL?

Included in Oracle 7.3 is an UTL_FILE package that can read and write operating system files. The directory you intend writing to has to be in your INIT.ORA file (see UTL_FILE_DIR=... parameter). Before Oracle 7.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command. Copy this example to get started: DECLARE fileHandler UTL_FILE.FILE_TYPE; BEGIN fileHandler := UTL_FILE.FOPEN('/tmp', 'myfile', 'w'); UTL_FILE.PUTF(fileHandler, 'Look ma, I''m writing to a file!!!\n'); UTL_FILE.FCLOSE(fileHandler); EXCEPTION WHEN utl_file.invalid_path THEN raise_application_error(-20000, 'ERROR: Invalid path for file or path not in INIT.ORA.'); END; / Back to top of file

1.2.9

Can one call DDL statements from PL/SQL?

One can call DDL statements like CREATE, DROP, TRUNCATE, etc. from PL/SQL by using the "EXECUTE IMMEDATE" statement. Users running Oracle versions below 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL). begin EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)'; end; NOTE: The DDL statement in quotes should not be terminated with a semicolon. Back to top of file

1.2.10 Can one use dynamic SQL statements from PL/SQL?


Starting from Oracle8i one can use the "EXECUTE IMMEDIATE" statement to execute dynamic SQL and PL/SQL statements (statements created at run-time). Look at these examples. Note that statements are NOT terminated by semicolons: EXECUTE IMMEDIATE 'CREATE TABLE x (a NUMBER)'; -- Using bind variables... sql_stmt := 'INSERT INTO dept VALUES (:1, :2, :3)'; EXECUTE IMMEDIATE sql_stmt USING dept_id, dept_name, location; -- Returning a cursor... sql_stmt := 'SELECT * FROM emp WHERE empno = :id'; EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id; 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; / More complex DBMS_SQL example using bind variables: 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; / Back to top of file

1.2.11 What is the difference between %TYPE and %ROWTYPE?


The %TYPE and %ROWTYPE constructs provide data independence, reduces maintenance costs, and allows programs to adapt as the database changes to meet new business needs. %ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor. Example: DECLARE v_EmpRecord

emp%ROWTYPE;

%TYPE is used to declare a field with the same type as that of a specified table's column. Example: DECLARE v_EmpNo Back to top of file

emp.empno%TYPE;

1.2.12 What is the result of comparing NULL with NULL?


NULL is neither equal to NULL, nor it is not equal to NULL. Any comparison to NULL is evaluated to NULL. Look at this code example to convince yourself. declare a number := NULL; b number := NULL; begin if a=b then dbms_output.put_line('True, NULL = NULL'); elsif a<>b then dbms_output.put_line('False, NULL <> NULL'); else dbms_output.put_line('Undefined NULL is neither = nor <> to NULL'); end if; end; Back to top of file

1.2.13 How does one get the value of a sequence into a PL/SQL variable?
As you might know, one cannot use sequences directly from PL/SQL. Oracle (for some silly reason) 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;
Thanks to Ronald van Woensel

Back to top of file

1.2.14 Can one execute an operating system command from PL/SQL?


There is no direct way to execute operating system commands from PL/SQL in Oracle7. However, one can write an external program (using one of the precompiler languages, OCI or Perl with Oracle access modules) to act as a listener on a database pipe (SYS.DBMS_PIPE). Your PL/SQL program then put requests to run commands in the pipe, the listener picks it up and run the requests. Results are passed back on a different database pipe. For an Pro*C example, see chapter 8 of the Oracle Application Developers Guide. In Oracle8 one can call external 3GL code in a dynamically linked library (DLL or shared object). One just 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 example. Back to top of file

1.2.15 How does one loop through tables in PL/SQL?


Look at the following nested loop code example. DECLARE CURSOR dept_cur IS SELECT deptno FROM dept ORDER BY deptno; -- 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; / Back to top of file

1.2.16 How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy?
Contrary to popular believe, one should COMMIT less frequently within a PL/SQL loop to prevent ORA1555 (Snapshot too old) errors. The higher the frequency of commit, the sooner the extents in the 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; ... to ... FOR records IN my_cursor LOOP ...do some stuff... i := i+1; IF mod(i, 10000) THEN -- Commit every 10000 records COMMIT; END IF; END LOOP; If you still get ORA-1555 errors, contact your DBA to increase the rollback segments. NOTE: Although fetching across COMMITs work with Oracle, is not supported by the ANSI standard. Back to top of file

1.2.17 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 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;

Define your procedures with invoker rights (Oracle 8i and higher); Move all the tables to one user/schema.

Back to top of file

1.2.18 What is a mutating and constraining table?


"Mutating" means "changing". A mutating table is a table that is currently being modified by an update, delete, or insert statement. When a trigger tries to reference a table that is in state of flux (being changed), it is considered "mutating" and raises an error since Oracle should not return data that has not yet reached its final state. Another way this error can occur is if the trigger has statements to change the primary, foreign or unique key columns of the table off which it fires. If you must have triggers on tables that have referential constraints, the workaround is to enforce the referential integrity through triggers as well. There are several restrictions in Oracle regarding triggers: A row-level trigger cannot query or modify a mutating table. (Of course, NEW and OLD still can be accessed by the trigger) . A statement-level trigger cannot query or modify a mutating table if the trigger is fired as the result of a CASCADE delete. Etc.

Back to top of file

1.2.19 Can one pass an object/table as an argument to a remote procedure?


The only way the same object type can be referenced between two databases is via a database link. Note that it is not enough to just use the same type definitions. Look at this example: -- Database A: receives a PL/SQL table from database B CREATE OR REPLACE PROCEDURE pcalled(TabX DBMS_SQL.VARCHAR2S) IS BEGIN -- do something with TabX from database B null; END; / -- 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; / Back to top of file

1.2.20 Is it better to put code in triggers or procedures? What is the difference?


In earlier releases of Oracle it was better to put as much code as possible in procedures rather than triggers. At that stage procedures executed faster than triggers as triggers had to be re-compiled every time before executed (unless cached). In more recent releases both triggers and procedures are compiled when created (stored p-code) and one can add as much code as one likes in either procedures or triggers. Back to top of file

1.2.21 Is there a PL/SQL Engine in SQL*Plus?


No. Unlike Oracle Forms, SQL*Plus does not have an embedded PL/SQL engine. Thus, all your PL/SQL code is sent directly to the database engine for execution. This makes it much more efficient as SQL statements are not stripped off and sent to the database individually. Back to top of file

1.2.22 Is there a limit on the size of a PL/SQL block?


Yes, the max size is not an explicit byte limit, but related to the parse tree that is created when you compile the code. You can run the following select statement to query the size of an existing package or procedure: SQL> select * from dba_object_size where name = 'procedure_name'; Back to top of file

You might also like