Beruflich Dokumente
Kultur Dokumente
PL/SQL
By Satish Gonare
PL/SQL
1.
Pl/sql block:
PL/SQL Block is a standard PL/SQL code segment. Block consists of three
parts. The following is the block structure.
Declare
Begin
Exception
End;
/
Executable Section which is mandatory. This section must contain at least one
executable statement.
Packages:
Packages bundle related pl/sql types, items, and subprograms into one
container.
A package will have a specification and body stored separately in the database.
The body fully defines the cursors and so implements the specification.
Once written and complied, the contents can be shared by many applications.
When you call a packaged pl/sql construct for the first time, the whole
package is loaded into memory. Thus later calls to constructs in the same
package require no disk input/output.
Oracle sever stores the specification and body of a package separately in the
database. This enables us to change the definition of a program construct in
the package body without causing the oracle server to invalidate other schema
objects that call or reference the program construct.
By Satish Gonare
PL/SQL
Global or public: Global Or Public package constructs are those that are
declared in the package specification and defined in the package body.
Declared within the package specification and may be defined within the
package body.
Local or private: local or private package constructs are those that are
defined solely within the package body. Declared and defined within the
package body.
Advantages of packages:
a. Modularity: we can combine logically related programming structures in a
named Module.
b. Hiding information:
Only the declarations in the package specification are visible and accessible to
applications. The
package body hides the definitions of private constructs so that
only the package is affected and
not the applications or any calling programs if
the definition changes. This enables us to change the
implementation
without
having to recompile calling programs.
c. Added functionality:
Packaged global variables and cursors persist for the duration of a session
thus; they can be shared
by all subprograms that execute in the environment.
d. Performance improvement:
When we call a subprogram of a package, the first time then entire package is
loaded into memory. This way, later calls to related subprograms in the package
require no further disk i/o. packaged
subprograms
also
stop
cascading
dependencies and so avoid unnecessary compilation.
e. Over loading:
Sometimes the processing in two subprograms is the same, but the Parameter
passed to them varies. In that case it is logically to give them the same name.
3.
DBMS_SHARED_POOL
By Satish Gonare
PL/SQL
4.
5.
6.
By Satish Gonare
PL/SQL
the
autonomous
transaction
Database triggers.
8.
By Satish Gonare
PL/SQL
v1 t1;
begin
open v1 for select * from inv;
open v1 for select * from inv2;
end;
9.
Function
1. Usually used to compute and return a
Value to the calling environment.
By Satish Gonare
PL/SQL
These
types
of
procedures are not available for reference from other Oracle tools. Another
limitation of stand-alone procedures is that they are compiled at run
time, which slows execution.
18. There is a situation where u has to handle the select statement which
does not retrieve any rows. Which of the following can help u in doing
this?
1) Sql%notfound in the execution section
2) Sql%rowcount in the execution section
3) Sql%found in the execution section
By Satish Gonare
PL/SQL
By Satish Gonare
PL/SQL
25. Can you disable database trigger which are written on table? How?
Yes. ALTER TABLE [DISABLE ALL_TRIGGER]
26. Which type of package construct must be declared both within the
package specifications and package body?
Public procedures and functions.
27. When creating stored procedures and functions, which construct allows
you to transfer values to and from the calling environment?
a. Local variables.
b. Formal arguments.
c. Boolean variables.
d. Substitution variables.
Ans:B
28. Which two tables are fused track object dependencies?
USER_DEPENDENSIES.
IDEPTREE.
29. Which type of argument passes a value from a calling environment?
IN, INOUT
30. You need to create a trigger on the EMP table that monitors every row
that is changed and places this information into the AUDIT_TABLE.
Which type of trigger do you create?
After insert or update or delete on EMP for each row.
31. You need to create a stored procedure that deletes rows from a table.
The name of the table from which the rows are to be deleted is unknown
until run time. Which method do you implement while creating such
procedure?
Use DBMS_SQL packaged routines in the procedure to delete the rows.
32. Under which situation do you create a server-side procedure?
When the procedure needs to be used by many users accessing the same
schema objects on a local database.
By Satish Gonare
PL/SQL
NFS). 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
----open the file /tmp/utl_file.txt for writing. If the file dose not exist,
this will create it.
----If the file dose exist, this will over write it.
fileHandler := UTL_FILE.FOPEN('/tmp', 'utl_file.txt', 'w');
By Satish Gonare
PL/SQL
By Satish Gonare
PL/SQL
By Satish Gonare
PL/SQL
return a value.
found,sql%rowcount,sql%isopen)
By Satish Gonare
PL/SQL
No. you will get error as OTHERS handler must be last among the
exception handlers of a
block
46. Create a table using a pl/sql block.
BEGIN
EXECUTE IMMEDIATE CREATE TABLE NEW_1(AGE NUMBER);
END;
47. CURSOR WITH UPDATE CLAUSE IS USED TO PERFORM LOCKS ON
DATA.
48. Restrictions for overloading?
You cannot overload two subprograms if their parameters differ only in name
or mode. The following two procedures cannot be overload, for example;
Procedure overloadme(p_s in number);
Procedure overloadme(p_s out number);
You cannot overload two function based only on their return type.
Ex: Function overloadmetoo return date;
Function overloadmetoo return number;
Finally, the parameter of overload functions must differ by type familyyou
cannot overload on the same family.
49. Explain Bulk bind:
Sql statements in pl/sql blocks are sent to the sql engine to be executed. The
sql engine can in turn send data back to the pl/sql engine (as the result of a
query). In many cases, data to be inserted or updated in the database is first
put into a pl/sql collection, and then this collection is iterated over with a for
loop to send the information to the sql engine. This results in a context switch
between pl/sql and sql for each row in the collection.
Oracle 8i and higher allow you to pass all rows in a collection to the sql engine
in one operation, eliminating all but one context switch. This is known as bulk
bind, and is done with the FORALL statement.
50. Types of triggers.
There are three main kind of triggers :
a. DML
b.Instead of and
By Satish Gonare
PL/SQL
c. System triggers
51. Types of DML triggers
There are a total of 12 possible types; (9i ): 3 statements * 2 timing * 2 leveles
Statement: Insert, Delete, or Update.
Timing: Before or After
Level: Row or Statement.
52. NOTE:
Function:
If we ALTER a table on which a function is based, then the function
becomes INVALID.
If we ALTER a table and DROP a table column, function becomes
INVALID. If we are using that column in function body and we
invoke such function then function will give error as 'Function is
invalid'. Else function gets compiled and executed and it becomes
VALID.
If we DROP a table on which a function is based on, then function
becomes INVALID. If we recreate the same table and if we invoke
the function then function again gets compiled internally and
executed and it becomes VALID.
If we RENAME a table, function becomes INVALID. It becomes
VALID when we rename it to original one and invoke the function.
Same applies to Procedures and Packages.
VIEWS :(SAME above rules applies to view also)
If we alter a table on which a view is created then view becomes
INVALID. If we invoke such a view then view again gets compiled
and executed and then it becomes valid.
Create view demo1 as select * from demo; (if demo has two columns
col1, col2) Here view will be stored as select col1, col2 from demo in
data dictionary. So if we alter a table and add new columns then view
becomes invalid and after compilation it becomes valid and it will
show only two previous columns which it contains.
If we ALTER a table and drop a column which is there in select
statement of view then view becomes INVALID. If we invoke such a
By Satish Gonare
PL/SQL
view it will give 'view SANJAY.DEMO1" has errors'. else view gets
compiled and gets executed and becomes valid.
If we DROP a table then view becomes invalid. If we invoke such a
view it gives 'ORA-04063: view "SANJAY.DEMO1" has errors'. If we
recreate the same table then view becomes valid on invoking it.
If we RENAME a table view becomes INAVLID. It becomes VALID
when we rename it to original one and invoke the function.
TRIGGERS:
If we alter a table, then its respective trigger becomes INVALID. If that
trigger is invoked (as a result of insert, update, delete on table) then
trigger gets revalidated and becomes VALID.
If we alter a table and drop a column, then its respective trigger
becomes INVALID. If that trigger is invoked and if the dropped
column is referenced in trigger(:new, :old) then Trigger gives
error'SANJAY.INV2_STOCK2' is invalid and failed re-validation'.
Else it becomes valid.
If we rename a table then its respective triggers will become invalid.
When trigger is invoked it becomes valid and work as usual and
table_name will be changed in user_triggres data dictionary.(this is not
so in functions, views etc.)
If a table is dropped its trigger will also get dropped.
53. DIFFERENCE BETWEEN 8I & 9I & 10g
1. In 8i we use snapshots. Those are replaced by materialized views in 9i.
2. In 9I partitioning of tables introduced.
3. In 9i Timestamp datatype is introduced in pl/sql.
4. In 10g multiple inserts into different tables using a single statement is used.
5. In 10g regular expressions introduced.
6. Till 7i the max no. of columns in a table are 256, but this extended to 1000
in 8i.
7. Till 7i generally we use set operators these are replaced by joins & sub
queries in 8i.
8. In subprograms a compiler hint known as NOCOPY is added in 8i, but this
is not there in prior versions.
SYNTAXES
CURSORS
By Satish Gonare
PL/SQL
EXPLICIT CURSORS:
CURSOR cursor_name[parameter_list]
[RETURN return_type]
IS query
[FOR UPDATE[OF (column_list)][NOWAIT]];
Where,
Cursor _name can be any valid identifier
PARAMETER_LIST is optional and can be any valid parameter used
for query execution.
RETRUN clause is optional, specifies the type of data to be returned
as defined by return_type.
FOR UPDATE clause is optional, locks the records when cursor is
opened. Records are still available to other session as READ ONLY.
OPEN THE CURSOR
OPEN cursor_name[(parameter_values)];
open command prepares the cursor for use.
FETCH RECORDS FROM THE CURSOR
FETCH cursor_name into VARiable_name(s)| PL/SQL_record;
FETCH retrieves records from the context area into a variable so that it can be
used.
CLOSE THE CURSOR
CLOSE cursor_name;
always close the cursor to intentionally introducing a memory leak into code.
until the cursor is closed, the memory is not released. Whenever cursor
attributes are used then the block is turned to implicitly cursor. Then no
need to close the cursor.
ERROR HANDLING
WHEN exception_name THEN
sequence_of_statement1;
WHEN exception_name THEN
sequence_of_statement2;
WHEN OTHERS THEN
sequence_of_statement3;
END;
By Satish Gonare
PL/SQL
(when others will trap all exceptions , be there user defined or predefined)
SQLERM returns 1,SQLERM "User-defined Exception"
PRAGMA
EXCEPTION_INIT(exception_name,oracle_error_number);
pragma must be in declarative section,
pragma is complier directive(over ridding the usual process of oracle)
RAISE_APPLICATION_ERROR
(error_number,error_message,
[keep_errors)
This can is built-in function to create your own error messages
error_number- values between -20,000 and -20,999
error_message is the text associated with this error,(512-characters),
keep_errors is a boolean values.
PROCEDURES
CREATE OR REPLACE PROCEDURE procedure_name[parameter_list]
[arguments>(IN , OUT, IN OUT) , (NO COPY), TYPE]
AS or IS
/* Declarative section is here*/
BEGIN
/*Executable section is here*/
EXCEPTION
/*Exception section is here*/
END [procedure_name];
PROCEDURE_NAME is the name of the procedure to be created, argument is
the name of the procedure parameter, type is the type of the associated
parameter procedure_body is a pl/sql block that makes up the code of the
procedure.
FUNCTION
CREATE OR REPLACE FUNCTION function_name[parameter_list]
RETURN(return_type)
[arguments>(IN , OUT, IN OUT) , (NO COPY), TYPE]
AS or IS
/* Declarative section is here*/
BEGIN
By Satish Gonare
PL/SQL
as
part of an expression.
PACKAGE:
CREATE OR REPLACE PACAGE package_name
IS or AS
/*package_body*/
package_name is the name of the package.
elements within the package (procedure and function specifications, variables,
and so on) are the
declarations,
PACKAGE INITILAZATION
CREATE OR REPLACE PACKAGE BODY package_name[IS| AS]
-------------BEGIN
Initialize
tion_code;
END package_name;
Where package_name is the name of the package and initialization_code is the
code to be run .
TRIGGERS
CREATE [OR REPLACE] TRIGGER trigger_name
(before| after|instead of) triggering_event
[referencing_clause]
[WHEN trigger_condition]
[FOR EACH ROW]
By Satish Gonare
PL/SQL
trigger_body;
Where trigger_name is the name of the trigger, trigger_event specifies the
event that fires the trigger(possibly including a specific table or view), and
trigger_body is the main code for the trigger.The
refencing_clause
is
used to refer to the data in the row currently being modified by a different
name. The trigger _condition in the WHEN clause, if present, is evaluated
first, and the body of the trigger is executed only when this condition
evaluates to TRUE.
trigger_body is a pl/sql bolck, which must contain at least an executable
section.
where declarative and exception handling sections are optional. if there is a
declarative section,
DECLARE keyword.
CREATE [OR REPLACE] TRIGGER [SCHEMA.] trigger_name
(before | after)
(ddl_event_list | database_event_list)
ON (DATABASE | [schema.] SCHEMA)
[when_clause]
trigger_body;
system triggers fires on two different kinds of events :DDL or database.
DDL events include create, alter,or drop statements, while database events
include startup/shutdown of the server, logon/logoff of a user, and a server
error.
where ddl_event_list is one or more DDL events (separated by the or
keyword), and database_event_list is one or more database events (separated
by the OR keyword)
By Satish Gonare