You are on page 1of 37

Oracle Supplied Packages

Satyam 2009

Using Supplied Packages

Oracle-supplied packages: Are provided with the Oracle server Extend the functionality of the database Enable access to certain SQL features normally restricted for PL/SQL

Satyam 2009

DYNAMIC SQL

PL/SQL

Satyam 2009

Dynamic SQL
It is an SQL statement that contains variables which can change at runtime.

It is a SQL statement and is stored as a character string


It enables to write general purpose code to be written

Satyam 2009

Dynamic SQL
It Enables data definition, data-control to be written and executed from PL/SQL.

It is written using either DBMS_SQL package or native dynamic SQL.


We can use Dynamic SQL to create a procedure that operates on a table whose name is not known until runtime

Satyam 2009

Dynamic SQL
In Oracle 8, and earlier, we have to use DBMS_SQL to write dynamic SQL

In Oracle 8i , we can use DBMS_SQL or native dynamic SQL .


The EXECUTE IMMEDIATE statement can perform dynamic single row queries

Satyam 2009

Dynamic SQL using EXECUTE IMMEDIATE


create or replace procedure del_all_rows(

p_tab_name in varchar2, p_rows_del out number)


is begin execute immediate 'delete from ' || p_tab_name; P_ROWS_DEL := SQL%ROWCOUNT; END;

Satyam 2009

EXAMPLE
create or replace procedure UPDATE_rows( p_tab_name in varchar2, COL1 IN VARCHAR2, VAL1 IN NUMBER,COL2 IN VARCHAR2,VAL2 IN NUMBER) is begin execute immediate 'UPDATE ' || P_TAB_NAME || ' SET ' || COL1 || ' = ' || VAL1 || ' WHERE ' || COL2 || ' = ' || VAL2 ; END;

Satyam 2009

Using DDL in PL/SQL program


Execute Immediate statement can also perform DDL operations

CREATE OR REPLACE PROCEDURE ddl_add_table(TNAME VARCHAR2) is


BEGIN EXECUTE IMMEDIATE 'CREATE TABLE ' || TNAME || '(EMPNO NUMBER(3) PRIMARY KEY, ENAME VARCHAR2(30), SAL NUMBER(8,2) )' ; END; /

Satyam 2009

DBMS_METADATA

Used to view the definition of objects like index, view, Table etc Example : 1. SELECT DBMS_METADATA.GET_DDL(OBJECT_TYPE,OBJECT_NAME) FROM DUAL;

SET LONG 1000 SELECT DBMS_METADATA.GET_DDL(TABLE,EMP) FROM DUAL

Satyam 2009

10

DBMS_LOB

PL/SQL

Satyam 2009

Agenda

Using Large Objects

Working with LOBs


Large Object Types Internal LOBs External LOBs Temporary LOBs The DBMS_LOB Package

Satyam 2009

12

Why Large objects

We can use LONG data type to store character data up to 2GB in length per row. In

place of LONG and LONG RAW , you can also use the LOB data types(BLOB,
CLOB, NCLOB and BFILE) for storage of long data up to 4GB in length.

Satyam 2009

13

What are LOBs?

Basically, LOBs (Large Objects) are designed to support large unstructured data such as text, graphic images, still video clips, full motion video, and sound waveforms. A typical employee record may be a few hundred bytes, but even small amounts of multimedia data can be thousands of times larger.

Satyam 2009

14

Types of LOBs

Oracle supports the following two types of LOBs: 1. CLOB 2. BLOB

They are stored in the database either in-line in the table or in a separate
segment or table space, such as BLOB(Binary LOB), CLOB (Character LOB) and, NCLOB (National Character LOB).

As the name signifies, BLOB holds binary data while the CLOB holds textual
data and the NCLOB holds, character data that corresponds to the national character set defined for the Oracle database.

Those stored as operating system files, such as BFILEs

Satyam 2009

15

Why Not Use LONGs?


In Oracle7, most applications storing large amounts of unstructured data used the LONG or LONG RAW data type. Oracle8i and Oracle9i's support for LOB data types is preferred over support for LONG and LONG RAWs in Oracle7 in the following ways: LOB Capacity: With Oracle8 and Oracle8i, LOBs can store up to 4GB of data. This doubles the 2GB of data that LONG and LONG RAW data types could store.

Satyam 2009

16

The LOB Datatype

Oracle9i regards LOBs as being of two kinds depending on their location with regard to the database -- internal LOBs and -- external LOBs, also referred to as BFILEs (binary files).

Satyam 2009

17

Internal LOBs

Internal LOBs, as their name suggests, are stored inside database tablespaces in a way that optimizes space and provides efficient access. Internal LOBs participate in the transactional model of the server. You can recover internal LOBs in the event of transaction or media failure, and any changes to a internal LOB value can be committed or rolled back.

In other words, all the ACID pertain to using internal LOBs

properties that pertain to using database objects

Satyam 2009

18

Number of LOB columns in a table

An Oracle8, Oracle8i, or Oracle9i table can have multiple LOB columns. Each LOB column in the same table can be of a different type. In Oracle7 Release 7.3 and higher, tables are limited to a single LONG or LONG RAW column. Random piece-wise access: LOBs support random access to data, but LONGs support only sequential access

Satyam 2009

19

External LOBs

External LOBs (BFILES) are large binary data objects stored in operating system files outside database tablespaces. Apart from conventional secondary storage devices such as hard disks, BFILEs may also be located on tertiary block storage devices such as CD-ROMs, PhotoCDs and DVDs.

Internal LOBs Use Copy Semantics, External LOBs Use Reference Semantics
Copy semantics: Both LOB locator and value are copied Reference semantics: Only LOB locator is copied

Satyam 2009

20

Benefits of LOBs

1. LOB columns can reach the size of 4G.

2. You can store LOB data internally within a table or externally.


3. You can perform random access to the LOB data. 4. It is easier to do transformations on LOB columns. 5. You can replicate the tables that contain LOB columns.

Satyam 2009

21

Append

Create table test(t1 clob, t2 clob, s number primary key); insert into test values('this is my source ' ,'this is my destination',1); commit; program declare b1 clob; b2 clob; begin select t1 into b1 from test where s = 1 for update; select t2 into b2 from test; dbms_lob.append(b1,b2); commit; end;

Satyam 2009

22

substr

The method DBMS_LOB.SUBSTR can return only 4000 characters which is the limit of a varchar2 in Oracle. select s, dbms_lob.getlength(t1) len, dbms_lob.substr(t1,40,10) raw_data from test where s=1

Satyam 2009

23

Example

Write a procedure to read character by character from CLOB column create or replace procedure print_clob( p_clob in clob ) as l_offset number default 1; begin dbms_output.new_line; loop exit when l_offset > dbms_lob.getlength(p_clob); dbms_output.put( dbms_lob.substr( p_clob, 1, l_offset ) ); dbms_output.new_line; l_offset := l_offset + 1; end loop; end;

Satyam 2009

24

Execution

Write PLSQL code to execute above procedure declare y clob; begin select t1 into y from test; print_clob(y); end; /

Satyam 2009

25

READ

declare v_amount number:=1000; v_offset number:=10; v_buffer clob; clobvalue1 clob; begin select t1 into clobvalue1 from test where s = 1; dbms_lob.read(clobvalue1,v_amount,v_offset,v_buffer); dbms_output.put_line(v_buffer); end;

Satyam 2009

26

COPY

declare v_amount number:=3000; v_offset number:=1; v_buffer clob; clobvalue1 clob; clobvalue2 clob; begin select t1 into clobvalue1 from test where s = 3 for update; select t2 into clobvalue2 from test where s = 3; dbms_lob.read(clobvalue1,v_amount,v_offset,v_buffer); dbms_output.put_line(v_buffer); DBMS_LOB.COPY (clobValue1, clobValue2,2000,dbms_lob.getlength(clobvalue1)+2,1); dbms_output.put_line(clobvalue2); end; /

Satyam 2009

27

DBMS_JOB

Satyam 2009

DBMS_JOB PROCEDURES

Subprogram SUBMIT Procedure REMOVE Procedure CHANGE Procedure WHAT Procedure NEXT_DATE Procedure INSTANCE Procedure INTERVAL Procedure BROKEN Procedure

Description Submits a new job to the job queue. Removes specified job from the job queue. Alters any of the user-definable parameters associated with a job. Alters the job description for a specified job. Alters the next execution time for a specified job. Assigns a job to be run by a instance. Alters the interval between executions for a specified job. Disables job execution.

RUN Procedure

Forces a specified job to run.

Satyam 2009

29

SUBMIT Procedure

This procedure submits a new job. It chooses the job from the sequence sys.jobseq. Syntax DBMS_JOB.SUBMIT ( job OUT BINARY_INTEGER, what IN VARCHAR2, next_date IN DATE DEFAULT sysdate, interval IN VARCHAR2 DEFAULT 'null', no_parse IN BOOLEAN DEFAULT FALSE, instance IN BINARY_INTEGER DEFAULT any_instance, force IN BOOLEAN DEFAULT FALSE);

Satyam 2009

30

Parameters
Parameter
Job What next_date interval no_parse

Description
Number of the job being run. PL/SQL procedure to run. Next date when the job will be run. Date function that calculates the next time to run the job. The default is NULL. This must evaluate to a either a future point in time or NULL. A flag. The default is FALSE. If this is set to FALSE, then Oracle parses the procedure associated with the job. If this is set to TRUE, then Oracle parses the procedure associated with the job the first time that the job is run. For example, if you want to submit a job before you have created the tables associated with the job, then set this to TRUE.

instance Force

When a job is submitted, specifies which instance can run the job. If this is TRUE, then any positive integer is acceptable as the job instance. If this is FALSE (the default), then the specified instance must be running; otherwise the routine raises an exception.

Satyam 2009

31

EXAMPLE

create or replace procedure testproc is vsal number; begin update emp set sal = 9000 where empno = 7521; select sal into vsal from emp where empno = 7521; insert into em1 values(vsal); commit; end; /

Satyam 2009

32

SUBMIT JOB

begin dbms_job.submit(:jobno,'testproc;',sysdate,'sysdate+(1/(24 * 60))'); commit; end;

Satyam 2009

33

REMOVE Procedure

This procedure removes an existing job from the job queue. This currently does not stop a running job. Syntax DBMS_JOB.REMOVE ( job IN BINARY_INTEGER );

Parameter job

Description Number of the job being run.

Example
EXECUTE DBMS_JOB.REMOVE(14144);

Satyam 2009

34

CHANGE Procedure

This procedure changes any of the user-settable fields in a job. Syntax DBMS_JOB.CHANGE ( job IN BINARY_INTEGER, what IN VARCHAR2, next_date IN DATE, interval IN VARCHAR2, instance IN BINARY_INTEGER DEFAULT NULL, force IN BOOLEAN DEFAULT FALSE); Example EXECUTE DBMS_JOB.CHANGE(14144, null, null, 'sysdate+3');

Satyam 2009

35

BROKEN Procedure
This procedure sets the broken flag. Broken jobs are never run. Syntax DBMS_JOB.BROKEN ( job IN BINARY_INTEGER, broken IN BOOLEAN, next_date IN DATE DEFAULT SYSDATE);

Parameter Job Broken next_data

Description Number of the job being run. Job broken: IN value is FALSE. Date of the next refresh.

Note: If you set job as broken while it is running, Oracle resets the job's status to normal after the job completes. Therefore, only execute this procedure for jobs that are not running.
Satyam 2009 36

RUN Procedure

This procedure runs job JOB now. It runs it even if it is broken. Running the job recomputes next_date. See view user_jobs. Syntax DBMS_JOB.RUN ( job IN BINARY_INTEGER, force IN BOOLEAN DEFAULT FALSE); Parameter job force Description Number of the job being run. If this is TRUE, then instance affinity is irrelevant for running jobs in the foreground process. If this is FALSE, then the job can be run in the foreground only in the specified instance.

Example EXECUTE DBMS_JOB.RUN(14144);


Satyam 2009 37

You might also like