Monday, May 24, 2010

BULK UPDATE IN ORACLE 10G

BULK UPDATE IN ORACLE 10G:

It’s been quite a while that I wrote a post in blog.. .. here is the one I used to update large number of records based on bulk update concept in oracle 10g and wanted to share it with you all that how we can do bulk update when there are enormous records in a table to avoid space issues, wish this could help you all. Table name and column names are changed for confidentiality however the concept is fairly clear and simple.

Bulk Bind concept:

Method 1:

 CREATE OR REPLACE PROCEDURE UPDATEA_ALL_ROWS IS
CURSOR CR IS SELECT OBJECT_ID FROM TEST1;
TYPE O_I IS TABLE OF TEST1.OBJECT_ID%TYPE INDEX BY BINARY_INTEGER;
OBJ_1 O_I;
CNT NUMBER:=0;
BEGIN
FOR REC IN CR LOOP
CNT:=CNT+1;
OBJ_1(CNT):=REC.OBJECT_ID;
END LOOP;
FORALL I IN 1..CNT
UPDATE TEST1 SET OBJECT_ID2=OBJ_1(I) WHERE OBJECT_ID=OBJ_1(I);
END;


method 2:

create or replace PROCEDURE update_all_rows (limit_in IN PLS_INTEGER) is
type segtab is table of test1%rowtype;
type instab is table of test1.object_id%type index by binary_integer;
l_segtab segtab;
l_instab instab;
cursor cr_rec is
select *
from test1;
begin
open cr_rec;
loop
fetch cr_rec bulk collect into l_segtab limit limit_in;
if l_segtab.count > 0 then
for i in l_segtab.first .. l_segtab.last loop
l_instab(i) := l_segtab(i).object_id;
end loop;
forall i in l_segtab.first .. l_segtab.last
update test1 set object_id2 = l_instab(i) where object_id=l_instab(i);
end if;
dbms_output.put_line ('commit performed');
commit;
exit when cr_rec%notfound;
end loop;
close cr_rec;
end update_all_rows;



Execution:
SQL> execute update_all_rows(1000);
commit performed
commit performed
commit performed
commit performed
commit performed
commit performed

PL/SQL procedure successfully completed.

Method 3: using bulk binding and collections ( with no limit keyword)

create or replace PROCEDURE update_all_rows1  is
type test1_t is table of test1.object_id%type;
lst test1_t;
begin
select object_id bulk collect
into lst
from test1;

forall i in lst.first .. lst.last
update test1 set object_id2=lst(i) where object_id = lst(i);
end update_all_rows1;




please add anything that you would like to see here or anything else that makes this post complete... Have a Fun all the time....

Thursday, February 18, 2010

orainstRoot.sh and root.sh scripts execution in Oracle

Usage of orainstRoot.sh and root.Sh execution in Oracle 10g Installation:

The first Script that we run is orainstRoot.sh which is located in $ORACLE_BASE/oraInventory for example : /u01/opt/app/oracle/oraInventory

The script should be run as root user

The usage of this script is :

It creates the inventory pointer file (/etc/oraInst.loc), The file shows the inventory location and group it is linked to.
Changing groupname of the oraInventory directory to oinstall group.

The second Script is root.sh, which should be run as root user

root.sh script performs many things, namely
It changes or correctly sets the environment variables
copying of few files into /usr/local/bin , the files are dbhome,oraenv,coraenv etc.
creation of /etc/oratab file or adding database home and SID's entry into /etc/oratab file.

root.sh script is located in $ORACLE_HOME directory.

The best way what else operations are performed when we run these script is to pay attention when we execute those and read the script execution log.

hope it help you all a bit.


Cheers...

Friday, February 12, 2010

dbms_job interval time meaning and on how to set interval in dbms_job in pre- 10g databases.

Its been a month having not written a new post in my blog and thought I could post a message to keep my blog update to date and even if it is a trivial info. This post came as outcome of a discussion with a friend on how to change interval time in earlier version of oracle db and here is the post about it with few other additional info.

Simple technique to schedule jobs in oracle 10g is explained in earlier post http://mdvreddy.blogspot.com/2009/11/job-scheduling-in-oracle-10g.html but sometimes we may need to work with pre-10g Oracle databases which do not provide a mechanism to schedule jobs as we do in 10g database or above.

Just for the purpose of completion, I would show how we can create a scheduler job in earlier database (not from 10g onwards) is as follows

declare
testjob number;
begin
dbms_job.submit(job => testjob,
what => 'procedure1(1);'
next_date => sysdate+1,
interval => 'sysdate+1');
end;
/
The simple job creation demo has two date/time related parameters and a procedure execution call. The date related parameters in this call are next_date and interval and which are date datatype or the values we pass can be varchar or char but they should be in such away that they would be evaluated to a date format later. Next_Date should always be greater than or equal to sysdate.

This post especially written to help in setting next_date parameter value of dbms_job.sumbit_job procedure and thought it will help you all as well as it helps me when needed. How to change REPEAT_INTERVAL of scheduler job in oracle 10g can be seen http://mdvreddy.blogspot.com/2009/04/2009.html .

This post is all about how to set interval which meets the various conditions or time set values at which we want job to be executed. The calculations are very simple arithmetic.

SELECT SYSDATE,sysdate + 1/(60*60*24) AS SYSDATE_1SECONDAFTER , sysdate + 1/(60*24) AS SYSDATE_1MINUTEAFTER , sysdate + 30/(60*24) AS SYSDATE_30MINUTESAFTER,
SYSDATE+1/24 AS SYSDATE_HRAFTER ,SYSDATE+1 AS SYSDATE_1DAYAFTER FROM DUAL

The above query is just to show how we can pass date time value to interval and you may use any one the listed in query incase suits.

Few more examples:
Sysdate+1 – same time but tomorrow
Trunk(sysdate+1) – tomorrow, today midnight i.e. just before starting of new day , we can say 12 AM;
trunc(sysdate)+5/24 – 5 am in the morning.

Interval parameter is incremented on each job run and you may set NULL to interval if job should not run any further at all.

DBMS_JOB.CHANGE procedure can be used to change parmaeter values of job.

Final say: go for dbms_scheduler for creation on new jobs and it is rich in all aspects.

Thank for reading and welcome your posts as all the time.

dbms_job interval time meaning and on how to set interval in dbms_job in pre- 10g databases.

Its been a month having not written a new post in my blog and thought I could post a message to keep my blog update to date and even if it is a trivial info. This post came as outcome of a discussion with a friend on how to change interval time in earlier version of oracle db and here is the post about it with few other additional info.

Simple technique to schedule jobs in oracle 10g is explained in earlier post http://mdvreddy.blogspot.com/2009/11/job-scheduling-in-oracle-10g.html but sometimes we may need to work with pre-10g Oracle databases which do not provide a mechanism to schedule jobs as we do in 10g database or above.

Just for the purpose of completion, I would show how we can create a scheduler job in earlier database (not from 10g onwards) is as follows

declare
testjob number;
begin
dbms_job.submit(job => testjob,
what => 'procedure1(1);'
next_date => sysdate+1,
interval => 'sysdate+1');
end;
/
The simple job creation demo has two date/time related parameters and a procedure execution call. The date related parameters in this call are next_date and interval and which are date datatype or the values we pass can be varchar or char but they should be in such away that they would be evaluated to a date format later. Next_Date should always be greater than or equal to sysdate.

This post especially written to help in setting next_date parameter value of dbms_job.sumbit_job procedure and thought it will help you all as well as it helps me when needed. How to change REPEAT_INTERVAL of scheduler job in oracle 10g can be seen http://mdvreddy.blogspot.com/2009/04/2009.html .

This post is all about how to set interval which meets the various conditions or time set values at which we want job to be executed. The calculations are very simple arithmetic.

SELECT SYSDATE,sysdate + 1/(60*60*24) AS SYSDATE_1SECONDAFTER , sysdate + 1/(60*24) AS SYSDATE_1MINUTEAFTER , sysdate + 30/(60*24) AS SYSDATE_30MINUTESAFTER,
SYSDATE+1/24 AS SYSDATE_HRAFTER ,SYSDATE+1 AS SYSDATE_1DAYAFTER FROM DUAL

The above query is just to show how we can pass date time value to interval and you may use any one the listed in query incase suits.

Few more examples:
Sysdate+1 – same time but tomorrow
Trunk(sysdate+1) – tomorrow, today midnight i.e. just before starting of new day , we can say 12 AM;
trunc(sysdate)+5/24 – 5 am in the morning.

Interval parameter is incremented on each job run and you may set NULL to interval if job should not run any further at all.

DBMS_JOB.CHANGE procedure can be used to change parmaeter values of job.

Final say: go for dbms_scheduler for creation on new jobs and it is rich in all aspects.

Thank for reading and welcome your posts as all the time.

Tuesday, January 5, 2010

HOW TO DO TRANSPORT TABLESPACE IN ORACLE 10G

HOW TO DO TRANSPORT TABLESPACE IN ORACLE 10G:

Transportable Tablespace demo:
================================
Transportable Tablespace method is one of the fastest method to move data/tablespaces between/around oracle databases and can also move a tablespace across same and different platforms.
We can perform entire database transport between platforms using same concept.
what platforms a tablespace can be moved is seen using v$transportable_platform and main befit of this is fastest move.
Note:
char,nchar sets (character sets) should be similar in both source and target systems.
there should not be tablespace with same name in target db system, either tablespace name should be renamed in source or target system.
all the objects should be in same tablespace and incase any object resides in tablespace points another object which resides in another tablespace, then both tablespaces should be included.i.e. it should be self containing.
Ensure that the both source and target systems use same endian settings otherwise converting of datafiles using RMAN is needed

This can be done on either the source platform or the target platform db system.
RMAN> CONVERT TABLESPACE ‘tablespacename’
TO PLATFORM = ‘targetplatformname’
DB_FILE_NAME_CONVERT = ‘source.dbf’, ‘target.dbf’
---

Transportable Tablespace demo:

On source system:
-----------------

CREATE TABLESPACE TABLESPACE2
DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\TABLESPACE2.DBF'
SIZE 10M REUSE AUTOEXTEND ON MAXSIZE 50M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;


CREATE TABLE SCOTT.items(
itemtype VARCHAR2(5) NOT NULL PRIMARY KEY
,description VARCHAR2(20) NOT NULL
)
TABLESPACE TABLESPACE2;


CREATE TABLE scott.ctype (
ctype VARCHAR2(5) NOT NULL PRIMARY KEY
,description VARCHAR2(20) NOT NULL
)
TABLESPACE TABLESPACE2;

insert data into two tables;

create a oracle directory
mkdir d:\ttexports;
create directory ttexports as 'd:\ttexports';
GRANT READ, WRITE ON DIRECTORY ttxports TO PUBLIC;

expdp system/xxxx job_name=ttexport1 directory=ttexports dumpfile=ttexport1.dmp logfile=ttsexport1.log transport_tablespaces=tablespace2 transport_full_check=true;

exp file=exp1.dmp log=exp1.log TRANSPORT_TABL
ESPACE=y TABLESPACES=TABLESPACE2;
or
exp USERID='sys/**** AS SYSDBA' TRANSPORT_TABLESPACE=y TABLESPACES=TABLESPACE2 FILE=TABLESPACE2.dmp

copy all the dump fles and dbf files to move target system

alter tablespace tablespace2 read write;

move all the dump files along with db files to target system... and move dbf files to target db folder..

on target system:
------------------
create a oracle directory
mkdir d:\ttimports;
create directory ttimports as 'd:\ttimports';
GRANT READ, WRITE ON DIRECTORY ttimports TO PUBLIC;

IMPDP JOB_NAME = TTSIMPORT1 DIRECTORY = ttimports DUMPFILE = TTEXPORT1.dmp LOGFILE = ttsimport1.log TRANSPORT_DATAFILES = d:\ttimports\TABLESPACE2.DBF

imp USERID='system/xxxx AS SYSDBA' TRANSPORT_TABLESPACE=y DATAFILES='d:\ttimports\TABLESPACE2.DBF' TABLESPACES=TABLESPACE2 FILE=TTEXPORT1.dmp

login into target db system scott user

select * from tab;
select * from items;
select * from ctype;

you will be able to see the data from tables

HOW TO DO TRANSPORT TABLESPACE IN ORACLE 10G

HOW TO DO TRANSPORT TABLESPACE IN ORACLE 10G:

Transportable Tablespace demo:
================================
Transportable Tablespace method is one of the fastest method to move data/tablespaces between/around oracle databases and can also move a tablespace across same and different platforms.
We can perform entire database transport between platforms using same concept.
what platforms a tablespace can be moved is seen using v$transportable_platform and main befit of this is fastest move.
Note:
char,nchar sets (character sets) should be similar in both source and target systems.
there should not be tablespace with same name in target db system, either tablespace name should be renamed in source or target system.
all the objects should be in same tablespace and incase any object resides in tablespace points another object which resides in another tablespace, then both tablespaces should be included.i.e. it should be self containing.
Ensure that the both source and target systems use same endian settings otherwise converting of datafiles using RMAN is needed

This can be done on either the source platform or the target platform db system.
RMAN> CONVERT TABLESPACE ‘tablespacename’
TO PLATFORM = ‘targetplatformname’
DB_FILE_NAME_CONVERT = ‘source.dbf’, ‘target.dbf’
---

Transportable Tablespace demo:

On source system:
-----------------

CREATE TABLESPACE TABLESPACE2
DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\TABLESPACE2.DBF'
SIZE 10M REUSE AUTOEXTEND ON MAXSIZE 50M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;


CREATE TABLE SCOTT.items(
itemtype VARCHAR2(5) NOT NULL PRIMARY KEY
,description VARCHAR2(20) NOT NULL
)
TABLESPACE TABLESPACE2;


CREATE TABLE scott.ctype (
ctype VARCHAR2(5) NOT NULL PRIMARY KEY
,description VARCHAR2(20) NOT NULL
)
TABLESPACE TABLESPACE2;

insert data into two tables;

create a oracle directory
mkdir d:\ttexports;
create directory ttexports as 'd:\ttexports';
GRANT READ, WRITE ON DIRECTORY ttxports TO PUBLIC;

expdp system/xxxx job_name=ttexport1 directory=ttexports dumpfile=ttexport1.dmp logfile=ttsexport1.log transport_tablespaces=tablespace2 transport_full_check=true;

exp file=exp1.dmp log=exp1.log TRANSPORT_TABL
ESPACE=y TABLESPACES=TABLESPACE2;
or
exp USERID='sys/**** AS SYSDBA' TRANSPORT_TABLESPACE=y TABLESPACES=TABLESPACE2 FILE=TABLESPACE2.dmp

copy all the dump fles and dbf files to move target system

alter tablespace tablespace2 read write;

move all the dump files along with db files to target system... and move dbf files to target db folder..

on target system:
------------------
create a oracle directory
mkdir d:\ttimports;
create directory ttimports as 'd:\ttimports';
GRANT READ, WRITE ON DIRECTORY ttimports TO PUBLIC;

IMPDP JOB_NAME = TTSIMPORT1 DIRECTORY = ttimports DUMPFILE = TTEXPORT1.dmp LOGFILE = ttsimport1.log TRANSPORT_DATAFILES = d:\ttimports\TABLESPACE2.DBF

imp USERID='system/xxxx AS SYSDBA' TRANSPORT_TABLESPACE=y DATAFILES='d:\ttimports\TABLESPACE2.DBF' TABLESPACES=TABLESPACE2 FILE=TTEXPORT1.dmp

login into target db system scott user

select * from tab;
select * from items;
select * from ctype;

you will be able to see the data from tables