This action might not be possible to undo. Are you sure you want to continue?
1. Which is the subset of SQL commands used to manipulate Oracle Database structures, including tables? Data Definition Language (DDL) 2. What operator performs pattern matching? LIKE operator 3. What operator tests column for the absence of data? IS NULL operator 4. Which command executes the contents of a specified file? START <filename> or @<filename> 5. What is the parameter substitution symbol used with INSERT INTO command? & 6. Which command displays the SQL command in the SQL buffer, and then executes it? RUN 7. What are the wildcards used for pattern matching? _ for single character substitution and % for multi-character substitution 8. State true or false. EXISTS, SOME, ANY are operators in SQL. True 9. State true or false. !=, <>, ^= all denote the same operation. True 10. What are the privileges that can be granted on a table by a user to others? Insert, update, delete, select, references, index, execute, alter, all 11. What command is used to get back the privileges offered by the GRANT command? REVOKE 12. Which system tables contain information on privileges granted and privileges obtained? USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
.'1234567890'. 19. 20.'YES'. '**'). then all the rows or rows satisfying the condition will be copied to the new table. AS SELECT command Explanation : To copy only the structure. '1'. 17. TRUNCATE TABLE EMP.0) FROM EMP. What will be the output of the following query? SELECT REPLACE(TRANSLATE(LTRIM(RTRIM('!! ATHEN !!'. '!'). Hence DELETE operation can be rolled back.13. the WHERE clause of the SELECT command should contain a FALSE statement as in the following. If the WHERE condition is true. Answer : NO Explanation : The query checks whether a given string is a numerical digit. 'NO' ).'!').'*'. but TRUNCATE operation cannot be rolled back. WHERE clause can be used with DELETE and not with TRUNCATE. The null values in the commission column will be replaced by 0 and added to salary.'TROUBLE') FROM DUAL. TROUBLETHETROUBLE 18. What is the difference between TRUNCATE and DELETE commands? TRUNCATE is a DDL command whereas DELETE is a DML command. What command is used to create a table by copying the structure of another table? Answer : CREATE TABLE . What will be the output of the following query? SELECT DECODE(TRANSLATE('A'. CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2. What does the following query do? SELECT SAL + NVL(COMM. 'AN'. Which system table contains information on constraints on all the tables created? USER_CONSTRAINTS 14. This displays the total salary of all employees. 15. 16. DELETE FROM EMP. Will the outputs of the above two commands differ? Both will result in deleting all the rows in the table EMP.'1111111111'). Which date function is used to find the difference between two dates? MONTHS_BETWEEN .
comm = 1000 25. What is the advantage of specifying WITH GRANT OPTION in the GRANT command? The privilege receiver can further grant the privileges he/she has obtained from the owner to any other user. Why does the following command give a compilation error? DROP TABLE &TABLE_NAME. Variable names should start with an alphabet. What is the use of DESC in SQL? Answer : DESC has two purposes.21. What is the use of the DROP option in the ALTER TABLE command? It is used to drop constraints specified on the table. COMM = SAL*0. 1200 SQL – QUERIES I. 26. a parent table can be dropped even when a child table exists.-2) FROM DUAL. 24. It is used to describe a schema as well as to retrieve rows from table in descending order. 23. Here the table name starts with an '&' symbol.1. 27. Explanation : The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted on ENAME in descending order. What is the value of ‘comm’ and ‘sal’ after executing the following query if the initial value of ‘sal’ is 10000? UPDATE EMP SET SAL = SAL + 1000. Which function is used to find the largest integer less than or equal to a specific value? FLOOR 28. sal = 11000. SCHEMAS .5678. What is the output of the following query? SELECT TRUNC(1234. 22. What is the use of CASCADE CONSTRAINTS? When this clause is used with the DROP command.
SPLACE – Study Place. SCOST . Display the names and date of birth of all programmers born in April. PROF2 (VARCHAR). How many programmers studied at Pentafour. Find out the selling cost average for packages developed in Oracle. 11. How many programmers have done the DCA course. PROF1 – Proficiency 1 QUERIES : 1. 2. How many programmers paid 10000 to 15000 for the course? 18. Display the details of programmers knowing C. 20. Find out the number of copies which should be sold in order to recover the development cost of each package. How much revenue has been earned through the sale of packages developed in C. DEVIN – Developed in. 7. What is the price of costliest software developed in VB? 15. DCOST – Development Cost. How many programmers know either C or Pascal? (VARCHAR). DEVIN (NUMBER). 13. SOLD (NUMBER) Table 3 : PROGRAMMER PNAME (VARCHAR). COURSE (VARCHAR). SEX (CHAR). SPLACE (VARCHAR). Display the names. 10.Table 1 : STUDIES PNAME (VARCHAR). SAL (NUMBER) LEGEND : PNAME – Programmer Name. DCOST (NUMBER). SCOST – Software Cost. 6. 8. TITLE (VARCHAR). 12. Display the names of those who have done the PGDCA course. Display the details of packages whose sales crossed the 5000 mark. Display the details of packages for which the development cost has been recovered. CCOST – Course Cost. 4. ages and experience of all programmers. Display the details of software developed by Rakesh. Display the lowest course fee. 3. DOJ (DATE). 9. What is the highest number of copies sold by a package? 5. 14. How many programmers studied at PRAGATHI? 17. PROF1 (VARCHAR). CCOST (NUMBER) Table 2 : SOFTWARE PNAME (VARCHAR). How many packages were developed in Oracle ? 16. What is the average course fee? 19. DOB (DATE).
18. Calculate the experience in years for each programmer and display along with their names in descending order. How many programmers don’t know C and C++? 22.'PASCAL') OR PROF2 IN ('C'. SELECT COUNT(*) FROM STUDIES WHERE COURSE = 'DCA'. How many people draw 5000 to 7500? 30. 14. 16. . Who are the programmers who celebrate their birthdays during the current month? 26. SELECT PNAME. What is the average salary? 29. 19. SELECT * FROM PROGRAMMER WHERE PROF1 IN ('C'. 10. SELECT PNAME. TRUNC(MONTHS_BETWEEN(SYSDATE. Display the costliest package developed by each programmer. SELECT AVG(SCOST) FROM SOFTWARE WHERE DEVIN = 'ORACLE'. 2. 8. 9. SELECT COUNT(*) FROM STUDIES WHERE CCOST BETWEEN 10000 AND 15000. 17. What is the average age of female programmers? 24. SELECT CEIL(DCOST/SCOST) FROM SOFTWARE. SELECT MAX(SOLD) FROM SOFTWARE. SELECT SUM(SCOST*SOLD-DCOST) FROM SOFTWARE GROUP BY DEVIN HAVING DEVIN = 'C'. SELECT MIN(CCOST) FROM STUDIES.21. 20.DOJ)/12) "EXPERIENCE" FROM PROGRAMMER. How old is the oldest male programmer? 23.'PASCAL'). C++ or Pascal. 25. Produce the following output for all the male programmers Programmer Mr. SELECT COUNT(*) FROM STUDIES WHERE SPLACE = 'PRAGATHI'. Arvind – has 15 years of experience KEYS: 1. 6. SELECT * FROM SOFTWARE WHERE PNAME = 'RAKESH'. 7. SELECT * FROM STUDIES WHERE SPLACE = 'PENTAFOUR'.TRUNC(MONTHS_BETWEEN(SYSDATE. 11. 15. SELECT * FROM SOFTWARE WHERE SCOST*SOLD >= DCOST. Display the details of those who don’t know C. 3. SELECT MAX(SCOST) FROM SOFTWARE GROUP BY DEVIN HAVING DEVIN = 'VB'. 32. 31. 13. SELECT PNAME FROM STUDIES WHERE COURSE = 'PGDCA'. What are the languages known by the male programmers? 28. SELECT AVG(CCOST) FROM STUDIES. DOB FROM PROGRAMMER WHERE DOB LIKE '%APR %'. How many female programmers are there? 27. 5. 12. SELECT * FROM SOFTWARE WHERE SCOST*SOLD-DCOST > 5000. 4.DOB)/12) "AGE". SELECT * FROM PROGRAMMER WHERE PROF1 = 'C' OR PROF2 = 'C'. SELECT COUNT(*) FROM SOFTWARE WHERE DEVIN = 'ORACLE'.
'PASCAL') AND PROF2 NOT IN ('C'.DOJ)/12) || ' years of experience' “Programmer” FROM PROGRAMMER WHERE SEX = 'M' UNION SELECT 'Ms.'C+ +'. MGR (NUMBER(4)).' || PNAME || ' .DOJ)/12) FROM PROGRAMMER ORDER BY PNAME DESC.DOB)/12)) FROM PROGRAMMER WHERE SEX = 'M'. 27. SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C'. ENAME (VARCHAR2(10)). SCHEMA : Table 1 : DEPT DEPTNO (NOT NULL . 30.' || PNAME || ' has ' || TRUNC(MONTHS_BETWEEN(SYSDATE. SAL (NUMBER(7. NUMBER(2)).21. JOB (VARCHAR2(9)). DEPTNO (NUMBER(2)) MGR is the empno of the employee whom the employee reports to. List all the employees who have at least one person reporting to them.'MON'). 23.TITLE. 29.has ' || TRUNC (MONTHS_BETWEEN (SYSDATE. SELECT TRUNC(MAX(MONTHS_BETWEEN(SYSDATE. SELECT DISTINCT(PROF1) FROM PROGRAMMER WHERE SEX = 'M'. COMM (NUMBER(7. 32. SELECT TRUNC(AVG(MONTHS_BETWEEN(SYSDATE. TRUNC(MONTHS_BETWEEN(SYSDATE. SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C'. 28.'C++') AND PROF2 NOT IN ('C'. II .'PASCAL'). .'C++'. NUMBER(4)).2)). 22. HIREDATE (DATE).DOJ)/12) || ' years of experience' “Programmer” FROM PROGRAMMER WHERE SEX = 'F'. DEPTNO is a foreign key. SELECT PNAME. SELECT AVG(SAL) FROM PROGRAMMER. 25. SELECT PNAME FROM PROGRAMMER WHERE TO_CHAR(DOB. 26.'C++'). SELECT PNAME. DNAME (VARCHAR2(14)). SELECT COUNT(*) FROM PROGRAMMER WHERE SAL BETWEEN 5000 AND 7500. SELECT COUNT(*) FROM PROGRAMMER WHERE SEX = 'F'.SCOST FROM SOFTWARE WHERE SCOST IN (SELECT MAX(SCOST) FROM SOFTWARE GROUP BY PNAME).2)).'MON') = TO_CHAR(SYSDATE. QUERIES 1. LOC (VARCHAR2(13) Table 2 : EMP EMPNO (NOT NULL . 31. 24.SELECT 'Mr.DOB)/12)) FROM PROGRAMMER WHERE SEX = 'F'.
6. 2. 4. MAX(HIREDATE) FROM EMP GROUP BY DEPTNO). 7. 10. 12.MGR=B.EMPNO. List the name of the employees with their immediate higher authority. 14.2. OF EMPLOYEES" FROM EMP GROUP BY TO_CHAR(HIREDATE. SELECT DISTINCT(A.ENAME) FROM EMP A. Find the most recently hired employee in each department. 10. SELECT DEPTNO. which are inserted. EMP B WHERE A. List the employee details whose salary is greater than the lowest salary of an employee belonging to deptno 20. 9. 13. Which department has the highest annual remuneration bill? 11. KEYS: 1. 6. 3.ENAME "EMPLOYEE". SELECT * FROM EMP WHERE (DEPTNO. Find the nth maximum salary. 3. Write a query to display a ‘*’ against the row of the most recently hired employee.0))). SELECT * FROM EMP WHERE EMPNO IN ( SELECT EMPNO FROM EMP MINUS SELECT MGR FROM EMP).15) "COMPENSATION" FROM EMP GROUP BY DEPTNO HAVING . 8. SELECT TO_CHAR(HIREDATE.ENAME "REPORTS TO" FROM EMP A. COUNT(EMPNO) "NO.'YYYY')). SELECT A. SELECT * FROM EMP WHERE SAL > ( SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO HAVING DEPTNO=20). List the details of the employee earning more than the highest paid manager.'YYYY') "YEAR".'YYYY') HAVING COUNT(EMPNO) = (SELECT MAX(COUNT(EMPNO)) FROM EMP GROUP BY TO_CHAR(HIREDATE. List the highest salary paid for each job. 15. MAX(SAL) FROM EMP GROUP BY JOB. Write a query to list the length of service of the employees (of the form n years and m months). SELECT * FROM EMP WHERE SAL > ( SELECT MAX(SAL) FROM EMP GROUP BY JOB HAVING JOB = 'MANAGER' ). 7. B. SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT(EMPNO)>10 AND DEPTNO=10). 8. 9. Write a correlated sub-query to list out the employees who earn more than the average salary of their department. that already exist) in the EMP table. List all the employees who do not manage any one. In which year did most people join the company? Display the year and the number of employees. EMP B WHERE A. Select the duplicate records (Records. 5.MGR. 5. HIREDATE) IN (SELECT DEPTNO.EMPNO = B. SELECT JOB. List the employee details if and only if more than 10 employees are present in department no 10. 4. or SELECT ENAME FROM EMP WHERE EMPNO IN (SELECT MGR FROM EMP). LPAD(SUM(12*(SAL+NVL(COMM.
15.05 $ $Author: Frank Naudé $ Topics What is PL/SQL and what is it used for? Should one use PL/SQL or Java to code procedures and triggers? How can one see if somebody modified any code? How can one search PL/SQL code for a string/key value? How can one keep a history of PL/SQL code changes? How can I protect my PL/SQL source code? Can one print to the screen from PL/SQL? Can one read/write files from PL/SQL? Can one call DDL statements from PL/SQL? .DEPTNO). SELECT ENAME.HIREDATE)/1 2))||' YEARS '|| TO_CHAR(TRUNC(MOD(MONTHS_BETWEEN (SYSDATE. 11.ROWID!=MIN (ROWID)). HIREDATE. LPAD('*'. HIREDATE. 14. SELECT ENAME "EMPLOYEE".0))) = (SELECT MAX(SUM(12*(SAL+NVL(COMM. SELECT ENAME. SELECT * FROM EMP A WHERE A.SAL FROM EMP E WHERE SAL > (SELECT AVG(SAL) FROM EMP F WHERE E.0)))) FROM EMP GROUP BY DEPTNO).12)))||' MONTHS ' "LENGTH OF SERVICE" FROM EMP.SUM( 12*(SAL+NVL(COMM. 12. 13. LPAD(' '.TO_CHAR(TRUNC(MONTHS_BETWEEN(SYSDATE. HIREDATE). SELECT ENAME. Index Search Add FAQ Ask Question Oracle PL/SQL FAQ $Date: 17-Dec-2002 $ $Revision: 2. SAL FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT(SAL)) FROM EMP B WHERE A.DEPTNO = F.SAL<=B.8) "RECENTLY HIRED" FROM EMP WHERE HIREDATE = (SELECT MAX(HIREDATE) FROM EMP) UNION SELECT ENAME NAME.EMPNO IN (SELECT EMPNO FROM EMP GROUP BY EMPNO HAVING COUNT(EMPNO)>1) AND A.15) "RECENTLY HIRED" FROM EMP WHERE HIREDATE != (SELECT MAX(HIREDATE) FROM EMP).SAL).
Many enhancements are still being made to PL/SQL. Back to top of file Should one use PL/SQL or Java to code procedures and triggers? Internally the Oracle database supports two procedural languages. all indications are that PL/SQL still has a bright future ahead of it. PL/SQL and Java appeal to different people in different job roles. PL/SQL is commonly used to write data-centric programs to manipulate data in an Oracle database. What is wrong? What is a mutating and constraining table? Can one pass an object/table as an argument to a remote procedure? Is it better to put code in triggers or procedures? What is the difference? Is there a PL/SQL Engine in SQL*Plus? Is there a limit on the size of a PL/SQL block? Where can one find more info about PL/SQL? Back to Oracle FAQ Index What is PL/SQL and what is it used for? PL/SQL is Oracle's Procedural Language extension to SQL. Can one use dynamic SQL statements from PL/SQL? What is the difference between %TYPE and %ROWTYPE? What is the result of comparing NULL with NULL? How does one get the value of a sequence into a PL/SQL variable? Can one execute an operating system command from PL/SQL? How does one loop through tables in PL/SQL? How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy? I can SELECT from SQL*Plus but not from PL/SQL. PL/SQL's language syntax. information hiding (all but inheritance). Oracle 9iDB supports native compilation of Pl/SQL code to binaries. This leads to questions like "Which of the two is the best?" and "Will Oracle ever desupport PL/SQL in favour of Java?". In fact. function overloading. 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. namely PL/SQL and Java. structure and data types are similar to that of ADA. Many Oracle applications are based on PL/SQL and it would be difficult of Oracle to ever desupport PL/SQL. 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 . The PL/SQL language includes object oriented programming techniques such as encapsulation. For example.
'FUNCTION'. . 'PACKAGE'.SCHEMA -. One can detect code changes by looking at the LAST_DDL_TIME column in the USER_OBJECTS dictionary view.* FROM USER_SOURCE WHERE TYPE = DICTIONARY_OBJ_TYPE AND NAME = DICTIONARY_OBJ_NAME. SELECT TYPE.2. This way one can easily revert to previous code should someone make any catastrophic changes. / show errors Back to top of file How can I protect my PL/SQL source code? PL/SQL V2. end if.Store old code in SOURCE_HIST table INSERT INTO SOURCE_HIST SELECT sysdate.Store code in hist table AFTER CREATE ON SCOTT. SQLERRM). STATUS FROM USER_OBJECTS WHERE LAST_DDL_TIME > '&CHECK_FROM_DATE'.Create history table AS SELECT SYSDATE CHANGE_DATE. CREATE OR REPLACE TRIGGER change_hist -. 'DD-Mon-RR HH24:MI') CREATE_TIME. END. TO_CHAR(CREATED. field or expression is referenced in your PL/SQL source code. TO_CHAR(LAST_DDL_TIME. implements a binary wrapper for PL/SQL programs to protect the source code. Back to top of file 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. NAME. functions and packages is stored in the Oracle Data Dictionary. 'DD-Mon-RR HH24:MI') MOD_TIME. user_source.1. EXCEPTION WHEN OTHERS THEN raise_application_error(-20000.How can one see if somebody modified any code? Code for stored procedures. Example: SELECT OBJECT_NAME.* FROM USER_SOURCE WHERE 1=2. available with Oracle7. 'TYPE') then -. LINE FROM USER_SOURCE WHERE UPPER(TEXT) LIKE '%&KEYWORD%'. Look at this example: CREATE TABLE SOURCE_HIST -. USER_SOURCE.Change SCOTT to your schema name DECLARE BEGIN if DICTIONARY_OBJ_TYPE in ('PROCEDURE'. 'PACKAGE BODY'.7). Back to top of file 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.2.
command. / Back to top of file . end.ORA. UTL_FILE. In that case.3 is an UTL_FILE package that can read and write operating system files. However. I can print from PL/SQL!!!'). set the buffer size to a larger value. END. The syntax is: wrap iname=myscript. and then EXEC NULL. 'Look ma.This is done via a standalone utility that transforms the PL/SQL source code into portable binary object code (somewhat larger than the original).FCLOSE(fileHandler). Copy this example to get started: DECLARE fileHandler UTL_FILE. This way you can distribute software without having to worry about exposing your proprietary algorithms and methods.. / DBMS_OUTPUT is useful for debugging PL/SQL programs. I''m writing to a file!!!\n'). UTL_FILE. SQL*Plus and SQL*DBA will still understand and know how to execute such scripts. For example: set serveroutput on begin dbms_output. the output buffer will overflow. BEGIN fileHandler := UTL_FILE. The directory you intend writing to has to be in your INIT. 'ERROR: Invalid path for file or path not in INIT. parameter).: set serveroutput on size 200000 If you forget to set serveroutput on type SET SERVEROUTPUT ON once you remember.').ORA file (see UTL_FILE_DIR=.. there is no "decode" command available. This buffer can be displayed on the screen from SQL*Plus if you issue the SET SERVEROUTPUT ON.put_line('Look Ma.FILE_TYPE. Just be careful. Back to top of file Can one read/write files from PL/SQL? Included in Oracle 7.invalid_path THEN raise_application_error(-20000.PUTF(fileHandler.FOPEN('/tmp'. eg. Before Oracle 7.plb Back to top of file Can one print to the screen from PL/SQL? One can use the DBMS_OUTPUT package to write information to an output buffer. SQL*Plus will display the entire contents of the buffer when it executes this dummy PL/SQL block. 'w').. 'myfile'. if you print too much. EXCEPTION WHEN utl_file.3 the only means of writing a file was to use DBMS_OUTPUT with the SQL*Plus SPOOL command.sql oname=xxxx. If you haven't cleared the DBMS_OUTPUT buffer with the disable or enable procedure.
NOTE: The DDL statement in quotes should not be terminated with a semicolon.NATIVE). location.1 and above) to execute dynamic statements.OPEN_CURSOR. / More complex DBMS_SQL example using bind variables: CREATE OR REPLACE PROCEDURE DEPARTMENTS(NO IN DEPT. 20). -. v_dname. -. DROP. 1. EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id. end loop. end. rc integer.Returning a cursor.PARSE(v_cursor.PARSE(cur. dept_name.PUT_LINE('Deptartment name: '||v_dname). DBMS_OUTPUT. DBMS_SQL. BEGIN cur := DBMS_SQL.CLOSE_CURSOR(cur). END. ':x'. v_dname char(20). Look at these examples: CREATE OR REPLACE PROCEDURE DYNSQL AS cur integer. 1. Users running Oracle versions below 8i can look at the DBMS_SQL package (see FAQ about Dynamic SQL). loop if DBMS_SQL. TRUNCATE.Using bind variables.. DBMS_SQL. etc. sql_stmt := 'INSERT INTO dept VALUES (:1. sql_stmt := 'SELECT * FROM emp WHERE empno = :id'. Look at these examples. .COLUMN_VALUE_CHAR(v_cursor. v_rows integer. :2.FETCH_ROWS(v_cursor) = 0 then exit.V7). 'select dname from dept where deptno > :x'. begin EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)'.BIND_VARIABLE(v_cursor. DBMS_SQL. Back to top of file 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). from PL/SQL by using the "EXECUTE IMMEDATE" statement.OPEN_CURSOR. end if. v_dname). BEGIN v_cursor := DBMS_SQL. v_rows := DBMS_SQL.. DBMS_SQL. DBMS_SQL. 'CREATE TABLE X (Y DATE)'.. Note that statements are NOT terminated by semicolons: EXECUTE IMMEDIATE 'CREATE TABLE x (a NUMBER)'.DEPTNO%TYPE) AS v_cursor integer.DEFINE_COLUMN_CHAR(v_cursor.. DBMS_SQL. DBMS_SQL. :3)'.Can one call DDL statements from PL/SQL? One can call DDL statements like CREATE. One can also use the older DBMS_SQL package (V2. EXECUTE IMMEDIATE sql_stmt USING dept_id.EXECUTE(v_cursor). DBMS_SQL.EXECUTE(cur). rc := DBMS_SQL. no).
Any comparison to NULL is evaluated to NULL. one can use embedded SQL statements to obtain sequence values: select sq_sequence. Oracle (for some silly reason) prohibits this: i := sq_sequence. declare a number := NULL.CLOSE_CURSOR(v_cursor).NEXTVAL into :i from dual.CLOSE_CURSOR(v_cursor). However. NULL = NULL').empno%TYPE. and allows programs to adapt as the database changes to meet new business needs.put_line('False. b number := NULL. end. view or cursor. END. Example: DECLARE v_EmpRecord emp%ROWTYPE.NEXTVAL. one cannot use sequences directly from PL/SQL. EXCEPTION when others then DBMS_SQL. elsif a<>b then dbms_output. %TYPE is used to declare a field with the same type as that of a specified table's column. end if. 'Unknown Exception Raised: '||sqlcode||' '||sqlerrm). NULL <> NULL'). raise_application_error(-20000.put_line('Undefined NULL is neither = nor <> to NULL'). else dbms_output. What is the result of comparing NULL with NULL? NULL is neither equal to NULL. Back to top of file How does one get the value of a sequence into a PL/SQL variable? As you might know. %ROWTYPE is used to declare a record with the same types as found in the specified database table.DBMS_SQL. Look at this code example to convince yourself.put_line('True. begin if a=b then dbms_output. Example: DECLARE v_EmpNo Back to top of file emp. / Back to top of file What is the difference between %TYPE and %ROWTYPE? The %TYPE and %ROWTYPE constructs provide data independence. Thanks to Ronald van Woensel Back to top of file . reduces maintenance costs. nor it is not equal to NULL.
see chapter 8 of the Oracle Application Developers Guide. To fix this problem one can easily rewrite code like this: FOR records IN my_cursor LOOP . one can write an external program (using one of the precompiler languages.DEPTNO%TYPE) IS SELECT ename FROM emp WHERE deptno = v_dept_no..deptno) LOOP dbms_output.. One just write a library in C/ C++ to do whatever is required... i := i+1.Employee is '||emp_rec. Look at this External Procedure example.ename). 10000) THEN -. BEGIN FOR dept_rec IN dept_cur LOOP dbms_output.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. END LOOP. END LOOP. one should COMMIT less frequently within a PL/SQL loop to prevent ORA1555 (Snapshot too old) errors. / Back to top of file How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy? Contrary to popular believe.Employee cursor all employees for a dept number CURSOR emp_cur (v_dept_no DEPT. FOR records IN my_cursor LOOP .do some stuff..Commit every 10000 records . Results are passed back on a different database pipe. OCI or Perl with Oracle access modules) to act as a listener on a database pipe (SYS.. the sooner the extents in the rollback segments will be cleared for new transactions. For an Pro*C example. Your PL/SQL program then put requests to run commands in the pipe. COMMIT.deptno)).. END LOOP..do some stuff.. However. Defining this C/C++ function to PL/SQL makes it executable. DECLARE CURSOR dept_cur IS SELECT deptno FROM dept ORDER BY deptno.put_line('Employees in Department '|| TO_CHAR(dept_rec. IF mod(i. END. FOR emp_rec in emp_cur(dept_rec. Back to top of file How does one loop through tables in PL/SQL? Look at the following nested loop code example. to . causing ORA-1555 errors. the listener picks it up and run the requests. ... In Oracle8 one can call external 3GL code in a dynamically linked library (DLL or shared object).put_line('. The higher the frequency of commit... -..DBMS_PIPE).
contact your DBA to increase the rollback segments. NEW and OLD still can be accessed by the trigger) . Back to top of file 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. Move all the tables to one user/schema. delete. Do not use roles! GRANT select ON scott. A mutating table is a table that is currently being modified by an update. NOTE: Although fetching across COMMITs work with Oracle. is not supported by the ANSI standard. Note that it is not enough to just use the same type definitions.VARCHAR2S) IS BEGIN -. END LOOP. but does not observe privileges given through roles.emp TO my_user. but will give an error in PL/SQL.Database A: receives a PL/SQL table from database B CREATE OR REPLACE PROCEDURE pcalled(TabX DBMS_SQL.COMMIT. (Of course. A statement-level trigger cannot query or modify a mutating table if the trigger is fired as the result of a CASCADE delete. Back to top of file What is a mutating and constraining table? "Mutating" means "changing". or insert statement. END IF. the workaround is to enforce the referential integrity through triggers as well. The consequence is that a SQL statement can work in SQL*Plus.do something with TabX from database B null. 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. If you still get ORA-1555 errors. What is wrong? PL/SQL respect object privileges given directly to the user. There are several restrictions in Oracle regarding triggers: • • • A row-level trigger cannot query or modify a mutating table. If you must have triggers on tables that have referential constraints. Etc. Look at this example: -. Choose one of the following solutions: • • Grant direct access on the tables to your user. Another way this error can occur is if the trigger has statements to change the primary. . • • Define your procedures with invoker rights (Oracle 8i and higher). Back to top of file I can SELECT from SQL*Plus but not from PL/SQL. foreign or unique key columns of the table off which it fires.
Database B: sends a PL/SQL table to database A CREATE OR REPLACE PROCEDURE pcalling IS TabX DBMS_SQL. Back to top of file Is there a limit on the size of a PL/SQL block? Yes.END. END. Thus.org .A free community for Oracle developers worldwide The PL/SQL Cellar . SQL*Plus does not have an embedded PL/SQL engine. Back to top of file Is there a PL/SQL Engine in SQL*Plus? No.com . but related to the parse tree that is created when you compile the code. At that stage procedures executed faster than triggers as triggers had to be re-compiled every time before executed (unless cached). / -. 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'. BEGIN pcalled@DBLINK2(TabX). all your PL/SQL code is sent directly to the database engine for execution. 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. This makes it much more efficient as SQL statements are not stripped off and sent to the database individually.PL/Solutions provides consulting and training services for the Oracle PL/SQL language and PL/Vision The DMOZ PL/SQL Directory Back to top of file HOME | ASK QUESTION | ADD FAQ | SEARCH | E-MAIL US . the max size is not an explicit byte limit. / Back to top of file 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.VARCHAR2S@DBLINK2. Unlike Oracle Forms.An open source repository for PL/SQL developers RevealNet PL/SQL Pipeline .Free Oracle PL/SQL scripts including a bitwise operations package and message digest algorithms PLSolutions. Back to top of file Where can one find more info about PL/SQL? • • • • • • • Oracle FAQ: PL/SQL code examples Oracle FAQ: PL/SQL Books PLNet.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.