oracle supplied packages

37
© Satyam 2009 Oracle Supplied Packages

Upload: ankitjain1988

Post on 23-May-2017

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Oracle Supplied Packages

© Satyam 2009

Oracle Supplied Packages

Page 2: Oracle Supplied Packages

© Satyam 2009 2

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

Page 3: Oracle Supplied Packages

© Satyam 2009

DYNAMIC SQL

PL/SQL

Page 4: Oracle Supplied Packages

© Satyam 2009 4

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

Page 5: Oracle Supplied Packages

© Satyam 2009 5

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

Page 6: Oracle Supplied Packages

© Satyam 2009 6

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

Page 7: Oracle Supplied Packages

© Satyam 2009 7

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;

Page 8: Oracle Supplied Packages

© Satyam 2009 8

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;

Page 9: Oracle Supplied Packages

© Satyam 2009 9

Using DDL in PL/SQL program

Execute Immediate statement can also perform DDL operationsCREATE OR REPLACE PROCEDURE ddl_add_table(TNAME VARCHAR2) isBEGIN EXECUTE IMMEDIATE 'CREATE TABLE ' || TNAME || '(EMPNO NUMBER(3) PRIMARY KEY, ENAME VARCHAR2(30), SAL NUMBER(8,2) )' ;END;/

Page 10: Oracle Supplied Packages

© Satyam 2009 10

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

Page 11: Oracle Supplied Packages

© Satyam 2009

DBMS_LOB

PL/SQL

Page 12: Oracle Supplied Packages

© Satyam 2009 12

Agenda

Using Large ObjectsWorking with LOBsLarge Object Types Internal LOBs External LOBs Temporary LOBs The DBMS_LOB Package

Page 13: Oracle Supplied Packages

© Satyam 2009 13

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.

Page 14: Oracle Supplied Packages

© Satyam 2009 14

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.

Page 15: Oracle Supplied Packages

© Satyam 2009 15

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

Page 16: Oracle Supplied Packages

© Satyam 2009 16

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.

Page 17: Oracle Supplied Packages

© Satyam 2009 17

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).

Page 18: Oracle Supplied Packages

© Satyam 2009 18

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 properties that pertain to using database objects pertain to using internal LOBs

Page 19: Oracle Supplied Packages

© Satyam 2009 19

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

Page 20: Oracle Supplied Packages

© Satyam 2009 20

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

Page 21: Oracle Supplied Packages

© Satyam 2009 21

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.

Page 22: Oracle Supplied Packages

© Satyam 2009 22

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;

Page 23: Oracle Supplied Packages

© Satyam 2009 23

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

Page 24: Oracle Supplied Packages

© Satyam 2009 24

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;

Page 25: Oracle Supplied Packages

© Satyam 2009 25

Execution

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

Page 26: Oracle Supplied Packages

© Satyam 2009 26

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;

Page 27: Oracle Supplied Packages

© Satyam 2009 27

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

Page 28: Oracle Supplied Packages

© Satyam 2009

DBMS_JOB

Page 29: Oracle Supplied Packages

© Satyam 2009 29

DBMS_JOB PROCEDURES

Subprogram DescriptionSUBMIT Procedure Submits a new job to the job queue.REMOVE Procedure Removes specified job from the job queue.CHANGE Procedure Alters any of the user-definable parameters associated with a

job.WHAT Procedure Alters the job description for a specified job.NEXT_DATE Procedure Alters the next execution time for a specified job.INSTANCE Procedure Assigns a job to be run by a instance.INTERVAL Procedure Alters the interval between executions for a specified job.BROKEN Procedure Disables job execution.RUN Procedure Forces a specified job to run.

Page 30: Oracle Supplied Packages

© Satyam 2009 30

SUBMIT Procedure

This procedure submits a new job. It chooses the job from the sequence sys.jobseq.SyntaxDBMS_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);

Page 31: Oracle Supplied Packages

© Satyam 2009 31

Parameters

Parameter DescriptionJob Number of the job being run.What PL/SQL procedure to run.next_date Next date when the job will be run.interval 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.

no_parse 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 When a job is submitted, specifies which instance can run the job.

Force 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.

Page 32: Oracle Supplied Packages

© Satyam 2009 32

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

Page 33: Oracle Supplied Packages

© Satyam 2009 33

SUBMIT JOB

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

Page 34: Oracle Supplied Packages

© Satyam 2009 34

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 Descriptionjob Number of the job being run.

Example

EXECUTE DBMS_JOB.REMOVE(14144);

Page 35: Oracle Supplied Packages

© Satyam 2009 35

CHANGE Procedure

This procedure changes any of the user-settable fields in a job.SyntaxDBMS_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);

ExampleEXECUTE DBMS_JOB.CHANGE(14144, null, null, 'sysdate+3');

Page 36: Oracle Supplied Packages

© Satyam 2009 36

BROKEN Procedure

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

Parameter DescriptionJob Number of the job being run.Broken Job broken: IN value is FALSE.next_data 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.

Page 37: Oracle Supplied Packages

© Satyam 2009 37

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.SyntaxDBMS_JOB.RUN ( job IN BINARY_INTEGER, force IN BOOLEAN DEFAULT FALSE);

Parameter Descriptionjob Number of the job being run.force 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.

ExampleEXECUTE DBMS_JOB.RUN(14144);