Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Monday, June 6, 2011

Exception


What is Exception ?

   Whenever  error occurred in executable area (ie. in begin block) then exception will raise and program will terminate .
  
  
 Difference Between Calling and Called program
 ----------------------------------------------

                                A program contain many programs inside then it is called as calling program.
                               
                                A program called by another program then it is called as called program.
  
  

 Exception Works
================

                1.If exception  handled in both calling and called environment .Then  any exception arrives inside the called program then exception will be handled by itself
                 only(ie. called program) and program will not terminate  until exception raised in calling environment or execute completely.
               
                2.While handling exception in  calling environment but not in  called environment .Then any exception arrives inside the called program then exception will be
                  handled by called one and program terminates immediately.
                 
                 
                Check With Following Examples
                =================================
                 
Step 1: Drop Table If Exists
+++++++++++++++++++++++++++++

                drop table error;
                drop table tab_1;
                drop table tab_2;
                drop table tab_3;
               
               
Step 2: Create Table
+++++++++++++++++++++

                create table error ( value number, status varchar2(1000),st_date timestamp,nam_tab varchar2(50));
                create table tab_1 (a number , dup_num number, name varchar2(30));
                create table tab_2 (a number , dup_num number, name varchar2(30));
                create table tab_3 (a number , dup_num number, name varchar2(30));
               
Step 3: Drop Sequence
+++++++++++++++++++++++++++++

                drop sequence  test_seq;

Step 4 : create Sequence
++++++++++++++++++++++++

                create  sequence test_seq increment by 1 start with 1 maxvalue 100 nocache nocycle;


Step 5:= Create Packages use exception in both calling and called environment
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

create or replace package pack_one
as

      procedure  p1(p_a number);
                 
end pack_one;
/

create or replace package body pack_one
as

                                procedure p1(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);
                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_1
                                                where a = p_a;
                                               

                                               
                                                dbms_output.put_line('Step 1 ..........................');
                                               
                                                pack_two.p2(v_num);
                               
                                                dbms_output.put_line('Step 2 ..........................');
                                               
                                                pack_three.p3(v_num);
                                               
                                                dbms_output.put_line('Step 3 ..........................');
                                               
                               
                                Exception
                               
                                                when others then
                                               
                                                                v_error := substr(sqlerrm,1,100);
                                               
                                                               
                                               
                                                                insert into error values(test_seq.nextval ,v_error,sysdate,'pack_one.p1');
                                               
                                end p1;
                                               
                                 
end pack_one;
/

create or replace package pack_two
as

      procedure p2(p_a number);
                 
end pack_two;
/

create or replace package body pack_two
as

                                procedure p2(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);
                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_2
                                                where a = p_a;
                                               

                                               
                               
                                Exception
                               
                                                when others then
                                               
                                                                v_error := substr(sqlerrm,1,100);
                                               
                                                               
                                               
                                                                insert into error values(test_seq.nextval ,v_error,sysdate,'pack_one.p2');
                                               
                                end p2;
                                 
end pack_two;
/

create or replace package pack_three
as

      procedure p3(p_a number);
                 
end pack_three;
/
create or replace package body pack_three
as

                                procedure p3(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);

                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_3
                                                where a = p_a;
                               
                                Exception
                               
                                                when others then
                                               
                                                                v_error := substr(sqlerrm,1,100);
                                               
                                                               
                                               
                                                                insert into error values(test_seq.nextval ,v_error,sysdate,'pack_one.p3');
                                               
                                end p3;
                                 
end pack_three;
/


Step 6: Truncate Table rows only
+++++++++++++++++++++++++++++++++

Truncate table tab_1;
Truncate table tab_2;
Truncate table tab_3;



Step 7 : Insert Few Rows
++++++++++++++++++++++++++

Insert into tab_1 values(1,20,'Hello');
Insert into tab_2 values(2,3,'Hi');
Insert into tab_3 values(20,1,'Wow');

set serveroutput on
execute pack_one.p1(1);

Select * from error;
               
               
               
Step 8 : Create Package by  writing Exception only in calling package (eg.pack_one)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


create or replace package pack_one
as

      procedure  p1(p_a number);
                 
end pack_one;
/

create or replace package body pack_one
as

                                procedure p1(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);
                                                v_seq number;
                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_1
                                                where a = p_a;
                                               

                                               
                                                dbms_output.put_line('Step 1 ..........................');
                                               
                                                pack_two.p2(v_num);
                               
                                                dbms_output.put_line('Step 2 ..........................');
                                               
                                                pack_three.p3(v_num);
                                               
                                                dbms_output.put_line('Step 3 ..........................');
                                               
                               
                                Exception
                               
                                                when others then
                                               
                                                                v_error := substr(sqlerrm,1,100);
                                               
                                                               
                                               
                                                                insert into error values(test_seq.nextval ,v_error,sysdate,'pack_one.p1');
                                               
                                end p1;
                                               
                                 
end pack_one;
/

create or replace package pack_two
as

      procedure p2(p_a number);
                 
end pack_two;
/

create or replace package body pack_two
as

                                procedure p2(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);
                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_2
                                                where a = p_a;
                                               

                                end p2;
                                 
end pack_two;
/

create or replace package pack_three
as

      procedure p3(p_a number);
                 
end pack_three;
/
create or replace package body pack_three
as

                                procedure p3(p_a number)
                                as
                                                v_num number;
                                                v_error varchar2(4000);

                                begin
                                               
                                                select dup_num
                                                into v_num
                                                from tab_3
                                                where a = p_a;
                               
                                               
                                end p3;
                                 
end pack_three;
/

Step 9: Truncate Table rows only
+++++++++++++++++++++++++++++++++

Truncate table tab_1;
Truncate table tab_2;
Truncate table tab_3;



Step 10 : Insert Few Rows
++++++++++++++++++++++++++

Insert into tab_1 values(1,20,'Hello');
Insert into tab_2 values(2,3,’Hi’);
Insert into tab_3 values(20,1,'Wow');

set serveroutput on
execute pack_one.p1(1);

Select * from error;

Prepared By
S Sivakurunath

Sunday, May 8, 2011

Difference Between View and Materialized View


Step 1 : Create Base Table

                                create table T1(KEY number,VAL varchar2(10));
                               
insert into t1 values(1,'a');
insert into t1 values(2,'b');
insert into t1 values(3,'c');
insert into t1 values(4,'');

                               
Step 2 : Create Ordinary View

                                create view v as  select * from   t1 ;

Step 3 : Create Materialized  View

                                create materialized view log on t1 with rowid;


                                create materialized view mv refresh fast with rowid as select * from   t1 ;

Step 4: Check for rowid similarity and difference in materialized view

                                select rowid from T1 order by rowid ;

                                select rowid from v order by rowid ;

                                select rowid from mv order by rowid ;

Step 5 := Update base table

                                update t1 set val = upper(val);

Step 6 :=  After DML try to select

                                select * from T1 order by rowid ;

                                select * from v order by rowid ;

                                select * from mv order by rowid ;

Step 7 :- Refersh your materialized View

                                execute dbms_mview.refresh( 'MV' );

Step 8 := Try to update Base table Via both the view

                                                update v set val = lower(val); -- View will be create
                                               
                                                update mv set val = lower(val); -- Here it won't
                                               
Stpe 9 := Drop all objects.

drop materialized view mv ;

drop view v ;

drop table t1;


Prepared By
Sivakurunath S

Wednesday, April 27, 2011

Diff of Unhandled and Handled exception


Step 1 : Create Table

 create table cust_table
 (
   NAME                                VARCHAR2(30),
   ORD_DT                            DATE,
   PUR_DATE                    DATE,
   QUANTITY                        NUMBER,
   ITEM_NO                         VARCHAR2(30)
  );
 
  Step 2 : Try to execute a block without exception part.
 
    begin
                                insert into cust_table(NAME,ORD_DT,PUR_DATE,QUANTITY,ITEM_NO) values('Sivakurunath',sysdate-1,sysdate,100,'ORA0001');
                               
                                insert into cust_table(NAME,ORD_DT,PUR_DATE,QUANTITY,ITEM_NO) values('Sivakumar',sysdate-1,sysdate,100,'ORA0002');
                               
                                update cust_table set NAME='eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
                                where ITEM_NO='ORA0002';
 end;
 /

  Step 3 : Do select ,Check how many rows inserted and updated.
 
 select * from cust_table;

 Step 4 : Try to execute same block with  exception.
 
  begin
 
 
 
                                insert into cust_table(NAME,ORD_DT,PUR_DATE,QUANTITY,ITEM_NO) values('Sivakurunath',sysdate-1,sysdate,100,'ORA0001');
                               
                                insert into cust_table(NAME,ORD_DT,PUR_DATE,QUANTITY,ITEM_NO) values('Sivakumar',sysdate-1,sysdate,100,'ORA0002');
                               
                                update cust_table set NAME='eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
                                where ITEM_NO='ORA0001';
                               
 exception
     when others then
        dbms_output.put_line(sqlerrm);
 end;
 /

 step 5 : Do select ,Check how many rows inserted and updated.

 select * from cust_table;


 step 6 : Find in which scenario transaction remain as a part of transaction (step 2 or step 4).

Prepared By
Sivakurunath S

Monday, November 8, 2010

Create simple procedure in PL/SQL Oracle

Subprograms

Subprograms are named PL/SQL blocks that can be called with a set of parameters.
There are two types of blocks in PL/SQL:
•    Procedures
•    Functions

Structure Of Oracle Procedure(Anonymous block)

DECLARE         (optional)
        /* Variable Block                     */
    BEGIN             (mandatory)
        /* Executable Statements / Queries     */
    EXCEPTION    (optional)
        /* Exception Action                 */
END;                (mandatory)
/

Syntax of Oracle Procedure

CREATE [OR REPLACE] PROCEDURE procedure_name
 [(parameter1 [mode1] datatype1,
  parameter2 [mode2] datatype2,
  . . .)]
IS|AS
PL/SQL Block;

Structure Of Oracle Procedure(Named block)

CREATE [OR REPLACE] PROCEDURE procedure_name          (mandatory)
        /* Variable Block                     */
    BEGIN             (mandatory)
        /* Executable Statements / Queries     */
    EXCEPTION    (optional)
        /* Exception Action                 */
END;                (mandatory)
/

Modes:

•    IN: procedure must be called with a value for the parameter. Value cannot be changed
•    OUT: procedure must be called with a variable for the parameter. Changes to the parameter are seen by the user (i.e., call by reference)
•    IN OUT: value can be sent, and changes to the parameter are seen by the user

Default Mode is:

IN

Consider Table

Table Name: Example
NAME             VARCHAR2(10)
NUM              NUMBER(3)
                                                                                                                                                                               

Table Data

Select * from example;
NAME       NUM                    
---------- ----------------------
NAMEONE    1                      
NAMETWO    2                      
NAMETHREE  3                      
NAMEFOUR   4                      
NAMEFIVE   5                      
NAMESIX    6                      
NAMESEVEN  7                      
test -100  100                    
test -200  200                    
test -300  300                    
test -500  500        
11 rows selected

Simple Procedure For Get Name From Example Table

Create or replace procedure p_getname
(v_num IN example.num%TYPE,
v_name OUT example.name%TYPE)
/*    v_num     - Input Parameter            */
/*    v_name     - Output Parameter            */
IS
BEGIN
    select name
    into v_name
    from example
    where v_num = num;
END;
/

Calling the Procedure

set serveroutput on;
declare
    getname  example.name%TYPE;
begin
    p_getname(1,getname);
    dbms_output.put_line('-----------');
    dbms_output.put_line(getname);
end;
/

Sample Output:

anonymous block completed
-----------
NAMEONE

Wednesday, July 14, 2010

Step By Step Example For Object Type In Oracle

Step By Step Example For Object Type In Oracle

Create Object Type

-----------------------------------------------------------------------------------
SQL> CREATE TYPE ObjectPersonType AS OBJECT (
  2    ID       NUMBER,
  3    FNAME    VARCHAR2(20),
  4    LNAME    VARCHAR2(25),
  5    PHONE    VARCHAR2(20),
  6    MAP MEMBER FUNCTION get_idno RETURN NUMBER,
  7    MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY ObjectPersonType ));
  8  /

Type created.
-----------------------------------------------------------------------------------

here,  object variables are,
ID       NUMBER,
FNAME    VARCHAR2(20),
LNAME    VARCHAR2(25),
PHONE    VARCHAR2(20)

and
Object Memeber Functions,Procedures are
MAP MEMBER FUNCTION get_idno RETURN NUMBER
-- Return Self ID Number,
MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY ObjectPersonType )
-- In & Out Is Own Type Parameter

-----------------------------------------------------------------------------------

Create/ Replace Type Body

SQL> CREATE OR REPLACE TYPE BODY ObjectPersonType AS
  2    MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
  3    BEGIN
  4      RETURN ID;
  5    END;
  6    MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY ObjectPersonType ) IS
  7            BEGIN
  8      -- use the PUT_LINE procedure of the DBMS_OUTPUT package to display details
  9              DBMS_OUTPUT.PUT_LINE(TO_CHAR(ID) || ' ' || FNAME || ' ' || LNAME);
  10      DBMS_OUTPUT.PUT_LINE(PHONE);
 11    END;
 12  END;
 13/

Type body created.
-----------------------------------------------------------------------------------

Create New Table Using Object Type (ObjectPersonType)

Description:
Table Name Is PERSONLIST,
Fields are, RECORDDATE as Date & DETAILS As ObjectPersonType(Using Defined Object Type)

CREATE TABLE PERSONLIST (
  RECORDDATE   DATE,
  DETAILS              ObjectPersonType);

Table created.
-----------------------------------------------------------------------------------

Insert Values For PERSONLIST

INSERT INTO PERSONLIST VALUES (
                SYSDATE,  ObjectPersonType (25, 'Raaj', 'Malik','9876700001'));

1 row created.

INSERT INTO PERSONLIST VALUES (
                SYSDATE,  ObjectPersonType (21, 'Boss', 'Sivaji','9876700002'));

1 row created.
-----------------------------------------------------------------------------------

Select Statements For Object Type


SQL> SELECT * FROM PERSONLIST;

RECORDDAT
---------
DETAILS(ID, FNAME, LNAME, PHONE)
--------------------------------------------------------------------------------
13-JUL-10
OBJECTPERSONTYPE(25, 'Raaj', 'Malik', '9876700001')

13-JUL-10
OBJECTPERSONTYPE(21, 'Boss', 'Sivaji', '9876700002')


SQL> SELECT DETAILS FROM PERSONLIST;

DETAILS(ID, FNAME, LNAME, PHONE)
--------------------------------------------------------------------------------
OBJECTPERSONTYPE(25, 'Raaj', 'Malik', '9876700001')
OBJECTPERSONTYPE(21, 'Boss', 'Sivaji', '9876700002')


SQL> SELECT DETAILS FROM PERSONLIST WHERE RECORDDATE=( SELECT MAX(RECORDDATE) FROM PERSONLIST);

DETAILS(ID, FNAME, LNAME, PHONE)
--------------------------------------------------------------------------------
OBJECTPERSONTYPE(21, 'Boss', 'Sivaji', '9876700002')

SQL> SELECT c.DETAILS.get_idno() FROM PERSONLIST c;

C.DETAILS.GET_IDNO()
--------------------
                  25
                  21

SQL> SELECT Obj.DETAILS.FNAME FROM PERSONLIST Obj;

DETAILS.FNAME
--------------------
Raaj
Boss


SQL> SELECT DETAILS FROM PERSONLIST Obj WHERE Obj.DETAILS.FNAME='Raaj';

DETAILS(ID, FNAME, LNAME, PHONE)
--------------------------------------------------------------------------------
OBJECTPERSONTYPE(25, 'Raaj', 'Malik', '9876700001')


SQL> DECLARE
  Obj OBJECTPERSONTYPE;
BEGIN
  SELECT DETAILS INTO Obj FROM PERSONLIST WHERE RECORDDATE=( SELECT MAX(RECORDDATE) FROM PERSONLIST);
  Obj.display_details();
END;
/
21 Boss Sivaji
9876700002

PL/SQL procedure successfully completed.
--------------------------------------------------------------------------------




Saturday, June 26, 2010

Get Oracle Version ( SQL )

Select Database Version
select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod
PL/SQL Release 10.2.0.2.0 - Production
CORE    10.2.0.2.0      Production
TNS for Solaris: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production  

 


Select Full Product Details
select * from product_component_version;

PRODUCT
----------------------------------------------------------------
VERSION
----------------------------------------------------------------
STATUS
----------------------------------------------------------------
NLSRTL
10.2.0.2.0
Production

Oracle Database 10g Enterprise Edition
10.2.0.2.0
Prod

PRODUCT
----------------------------------------------------------------
VERSION
----------------------------------------------------------------
STATUS
----------------------------------------------------------------

PL/SQL
10.2.0.2.0
Production

TNS for Solaris:
10.2.0.2.0

PRODUCT
----------------------------------------------------------------
VERSION
----------------------------------------------------------------
STATUS
----------------------------------------------------------------
Production





Select Version With Banner

select * from v$version where banner like 'Oracle%';
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod



Select Version With Banner

select * from v$version where banner like 'PL/SQL%';
BANNER
----------------------------------------------------------------
PL/SQL Release 10.2.0.2.0 - Production