Tuesday, December 11, 2012

Generate DDL scripts using "DBMS_METADATA" package


To Generate all Tablespace DDL's:

set long 2000;
select dbms_metadata.get_ddl('TABLESPACE',tb.name) from v$tablespace tb ;

for 1 tablespace:
select dbms_metadata.get_ddl('TABLESPACE','TABLESPACE_NAME') from dual;


To Generate DDL for a USER :

set long 5000 linesize 280;
select dbms_metadata.get_ddl('USER', 'USER_NAME')  from dual;


To Generate DDL for all PROFILE's :

set long 5000;
select dbms_metadata.get_ddl('PROFILE',a.profile) from dba_profiles a;

For 1 profile:
 select dbms_metadata.get_ddl('PROFILE','PROFILE_NAME') from dual;


To Generate DDL for any TABLE :

set long 5000;
select dbms_metadata.get_ddl('TABLE','TABLE_NAME','OWNER') from dual;


To Generate DDL for any INDEX :

select dbms_metadata.get_ddl('INDEX',’INDEX_NAME’,'OWNER') from dual;


To Generate all INDEX DDL’s on a TABLE:

select dbms_metadata.get_dependent_ddl('INDEX',’TABLE_NAME','OWNER') from dual;

The above command will generate all index ddl’s which are on table.

To Generate DDL for a Specific SEQUENCE :

set long 5000;
set linesize 200;
select dbms_metadata.get_ddl('SEQUENCE','SEQUENCE_NAME','OWNER_NAME') from dual;


To Generate DDL for a Specific TRIGGER:

set long 5000;
set linesize 200;
SQL> select dbms_metadata.get_ddl('TRIGGER','TRGGER_NAME','TRIGGER_OWNER') from dual;


Monday, October 15, 2012

Changing Default CHARACTER SET of the database



Here my goal is to change the default character set from WE8ISO8859P1 to AL32UTF8 on  LINUX
Remember, this was tested on my DEVELOPMENT standalone database (refer Oracle Doc before doing on PROD).

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL>

SQL> SELECT SUM (a.log_space + b.data_space + c.tempspace) "Total_DB_Size (G)"
   FROM (SELECT ROUND (SUM (BYTES/1024/1024/1024), 2) data_space  FROM dba_data_files) b,(SELECT ROUND (SUM (BYTES*members/1024/1024/1024), 2) log_space  FROM v$log) a,
  (SELECT NVL(ROUND(SUM(BYTES/1024/1024/1024),2), 0) tempspace FROM dba_temp_files) c;

Total_DB_Size (G)
-----------------
            11.29


Do a full backup of the database because the ALTER DATABASE CHARACTER SET statement cannot be rolled back.


SQL> select name from v$database;

NAME
---------
TEST


SQL > select value from NLS_DATABASE_PARAMETERS where Parameter='NLS_CHARACTERSET';

VALUE
----------------------------------------
WE8ISO8859P1

SQL> SELECT VALUE FROM NLS_DATABASE_PARAMETERS WHERE PARAMETER='NLS_NCHAR_CHARACTERSET';

VALUE
----------------------------------------
AL16UTF16


SQL> select * from v$nls_parameters where parameter like '%CHARACTERSET';

PARAMETER
----------------------------------------------------------------
VALUE
----------------------------------------------------------------
NLS_CHARACTERSET
WE8ISO8859P1

NLS_NCHAR_CHARACTERSET
AL16UTF16
16UTF16



SQL> select userenv('language') from dual;

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.WE8ISO8859P1

SQL> exit
$
$ expdp directory=TEST dumpfile=FULL_TEST_10112012.dmp logfile=FULL_TEST_10112012.log full=y

Full Database backup is taken, 

SQL> select distinct(nls_charset_name(charsetid)) CHARACTERSET,
decode(type#, 1, decode(charsetform, 1, 'VARCHAR2', 2, 'NVARCHAR2','UNKOWN'),
9, decode(charsetform, 1, 'VARCHAR', 2, 'NCHAR VARYING', 'UNKOWN'),
96, decode(charsetform, 1, 'CHAR', 2, 'NCHAR', 'UNKOWN'),
112, decode(charsetform, 1, 'CLOB', 2, 'NCLOB', 'UNKOWN')) TYPES_USED_IN
from sys.col$ where charsetform in (1,2) and type# in (1, 9, 96, 112) order by CHARACTERSET;

CHARACTERSET                             TYPES_USED_IN
---------------------------------------- -------------
AL16UTF16                                NCHAR
AL16UTF16                                NCLOB
AL16UTF16                                NVARCHAR2
WE8ISO8859P1                             CHAR
WE8ISO8859P1                             CLOB
WE8ISO8859P1                             VARCHAR2

6 rows selected.


Oracle note suggests that if the character set conversion has happened between  a 7/8 bit character set like WE8ISO8859P1, US7ASCII etc to a mutibyte character set like UTF8, AL32UTF8 etc,

then there will be data loss for clob columns which display the old character set.
So it is best to take a full back of the database, preferably using the tradional export utility.


SQL> ALTER DATABASE CHARACTER SET INTERNAL_USE AL32UTF8;
ALTER DATABASE CHARACTER SET INTERNAL_USE AL32UTF8
*
ERROR at line 1:
ORA-12719: operation requires database is in RESTRICTED mode


sql> shut immediate

SQL> startup Restrict

SQL> ALTER DATABASE CHARACTER SET INTERNAL_USE AL32UTF8;
 Database altered.

after changing the character set bounced the database

SQL> select distinct(nls_charset_name(charsetid)) CHARACTERSET,
decode(type#, 1, decode(charsetform, 1, 'VARCHAR2', 2, 'NVARCHAR2','UNKOWN'),
9, decode(charsetform, 1, 'VARCHAR', 2, 'NCHAR VARYING', 'UNKOWN'),
96, decode(charsetform, 1, 'CHAR', 2, 'NCHAR', 'UNKOWN'),
112, decode(charsetform, 1, 'CLOB', 2, 'NCLOB', 'UNKOWN')) TYPES_USED_IN
from sys.col$ where charsetform in (1,2) and type# in (1, 9, 96, 112) order by CHARACTERSET;


CHARACTERSET                             TYPES_USED_IN
---------------------------------------- -------------
AL16UTF16                                NCHAR
AL16UTF16                                NCLOB
AL16UTF16                                NVARCHAR2
AL32UTF8                                 CHAR
AL32UTF8                                 CLOB
AL32UTF8                                 VARCHAR2

6 rows selected.


SQL> select value from NLS_DATABASE_PARAMETERS where parameter='NLS_CHARACTERSET';

VALUE
--------------------------------------------------------------------------------
AL32UTF8


SQL>  select userenv('language') from dual;

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.AL32UTF8

SQL>  SELECT VALUE FROM NLS_DATABASE_PARAMETERS WHERE PARAMETER='NLS_NCHAR_CHARACTERSET';

VALUE
--------------------------------------------------------------------------------
AL16UTF16


SQL>  select * from v$nls_parameters where parameter like '%CHARACTERSET';

PARAMETER
----------------------------------------------------------------
VALUE
----------------------------------------------------------------
NLS_CHARACTERSET
AL32UTF8

NLS_NCHAR_CHARACTERSET
AL16UTF16


NOTE:   we can use new GUI based DMU (Database Migration Assistant for Unicode) tool to convert the NLS_CHARACTERSET of an existing database to AL32UTF8 or UTF8

How to Migrate a WE8ISO8859P1 DB to AL32UTF8 using DMU 1.2 - an example (Doc ID 1546507.1)



Monday, October 8, 2012

50,000 BLOG HITS

Thank you all Oracle lovers/users.........today is the day i need to celebrate that my blog has reached a 50K hits i thank once again all the oracle users whether it is useful or not useful to you but i keep on posting a new topics and issues what ever i face






Tuesday, October 2, 2012

Oracle Drop Database 11gR2 on LINUX


Dropping a database will remove all corresponding datafiles, redo log files and control files. The database must be mounted in exclusive and restricted mode.

Drop database command will not do any effect on archived log files and backups of the database. Drop database command will not delete the files on RAW disks.
If you created your database with DBCA, you can use the same DBCA tool to drop the database.

Example.

$export ORACLE_SID=DEVDB
$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Fri Sep 28 18:18:41 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>DROP DATABASE;

DROP DATABASE
*
ERROR at line 1:
ORA-01586: database must be mounted EXCLUSIVE and not open for this operation

NOTE :  Drop database does not work when the database is opened in normal open mode

SQL > shut immediate
SQL> connect / as sysdba
Connected to an idle instance.
SQL> startup mount
ORACLE instance started.

Total System Global Area 282352256 bytes
Fixed Size                  2132856 bytes
Variable Size            1644174848 bytes
Database Buffers         1140850688 bytes
Redo Buffers               36388864 bytes
Database mounted.

SQL> alter system enable restricted session;
System altered.

Or open the database using “startup mount Restrict” command

SQL> drop database;
Database dropped.


Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>

Now, all the Control files, redo log files and Datafiles are automatically removed but you can still see the Parameter file and archivelog files.

Note:     If your database is running on windows you have to remove the registry entries manually.

Thursday, September 27, 2012

Datapump PARTITION_OPTION parameter


PARTITION_OPTIONS=  NONE | DEPARTITION | MERGE

·        NONE:  The partitions are created exactly as they were on the system the export was taken from.
·        DEPARTITION: Each partition and sub-partition is created as a separate table, named using a combination of the table and (sub-) partition name.
·        MERGE: Combines all partitions into a single table.

Here I want to Import schema1 partitioned tables to schema2 non-partitioned tables, to do so
    1) Take data pump complete Export of schema1
    2) Import into schema2 as below

NOTE :    Here “schema2” is not there but creates automatically during the Datapump import process

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL>


$ impdp dumpfile=xxx.dmp directory=EXP remap_schema=schema1:schema2 PARTITION_OPTIONS=merge

Import: Release 11.2.0.2.0 - Production on Thu Sep 27 20:38:11 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Username: / as sysdba

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYS"."SYS_IMPORT_FULL_05" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_FULL_05":  /******** AS SYSDBA dumpfile=xxx.dmp directory=EXP remap_schema= schema1:schema2 PARTITION_OPTIONS=merge
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/SEQUENCE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCHEMA2"."TABLE1":"SYS_P23561"  14.92 KB      34 rows

--------      OUTPUT TRIMMED          ------------------

Job "SYS"."SYS_IMPORT_FULL_05" completed with 12 error(s) at 20:39:53


A new schema2 will be created and all the partitioned tables are created as Single tables (non-partitioned)


Tuesday, September 18, 2012

STEPS TO CREATE ORACLE DATABASE MANUALLY ON LINUX



Step 1:
Prepare the database creation script. Following is my script "testdb.sql"

CREATE DATABASE TESTDB
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 '/opt/oracle/TESTDB/redo01.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 2 '/opt/oracle/TESTDB/redo02.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 3 '/opt/oracle/TESTDB/redo03.log'  SIZE 50M BLOCKSIZE 512
-- STANDBY LOGFILE
DATAFILE
  '/opt/oracle/TESTDB/system01.dbf' SIZE 200M,
 '/opt/oracle/TESTDB/users01.dbf' SIZE 100M
sysaux datafile '/opt/oracle/TESTDB/sysaux01.dbf' SIZE 200M
undo tablespace undotbs1
datafile '/opt/oracle/TESTDB/undotbs01.dbf' SIZE 100M
CHARACTER SET WE8MSWIN1252
;

NOTE :  You can get this similar script , when you perform ‘ALTER DATABASE BACKUP CONTROLFILE TO TRACE’, we can edit this script.

Step 2:
Create all the necessary directories.
Oracle:/opt/oracle $ mkdir TESTDB
Similarly create all necessary directories 


Step 3:
Prepare the init file(pfile) [initTESTDB.ora]
TESTDB.__db_cache_size=436207616
TESTDB.__java_pool_size=4194304
TESTDB.__large_pool_size=4194304
TESTDB.__oracle_base='/opt/oracle'#ORACLE_BASE set from environment
TESTDB.__pga_aggregate_target=432013312
TESTDB.__sga_target=641728512
TESTDB.__shared_io_pool_size=0
TESTDB.__shared_pool_size=184549376
TESTDB.__streams_pool_size=0
*.audit_file_dest='/opt/oracle/admin/TESTDB/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/opt/oracle/TESTDB/control01.ctl','/opt/oracle/fra/TESTDB/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='TESTDB'
*.db_recovery_file_dest='/opt/oracle/fra'
*.db_recovery_file_dest_size=4227858432
*.diagnostic_dest='/opt/oracle/'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=TESTDBXDB)'
#*.local_listener='LISTENER_TESTDB'
*.memory_target=1073741824
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS'

Step 4:
Now perform the following steps:

$ export ORACLE_SID=TESTDB
$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.3.0 - Production on Thu May 22 17:35:28 2008
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
Connected to an idle instance.
SQL >
SQL> startup nomount  pfile=/u01/app/oracle/product/10.2.0/db_1/dbs/initTESTDB.ora
ORACLE instance started.
Total System Global Area 603979776 bytes
Fixed Size 1263176 bytes
Variable Size 167774648 bytes
Database Buffers 427819008 bytes
Redo Buffers 7122944 bytes
SQL> @testdb.sql
Database created.

Step 5:
So your database is created. Now just run the catalog.sql and catproc.sql scripts.
You will find the in $ cd $ORACLE_HOME/rdbms/admin

SQL> @$ORACLE_HOME/rdbms/admin/catalog.sql
SQL> @$ORACLE_HOME/rdbms/admin/catproc.sql

This will create all dictionary views, now verify

SQL> select name from v$database;
NAME
---------
TESTDB

Okay, now your database is ready to use.


Steps to change database name only



$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 – Production

Now, I want to change only database name from DUMMYDB to DEMODB for my standalone dev database

Follow the same steps from my last post Steps to Rename database name and DBID

NOTE: This is tested on my Standalone Development database, for RAC please see [Doc ID 464922.1]

STEP 1 ) Shut down the database and open database in mount

SQL> select name from v$database;

NAME
---------
DUMMYDB

SQL> select instance_name,status from v$instance;

INSTANCE_NAME       STATUS
----------------                ------------
DUMMYDB                       OPEN


SQL> shut immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size                  2233336 bytes
Variable Size             683674632 bytes
Database Buffers          377487360 bytes
Redo Buffers                5541888 bytes
Database mounted.
SQL>


Step 2) Invoke the DBNEWID utility (nid) specifying the new database name in DBNAME and set=YES

$ nid  TARGET=/   dbname=DEMODB  SETNAME=YES

DBNEWID: Release 11.2.0.2.0 - Production on Tue Sep 18 14:00:18 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Connected to database DUMMYDB (DBID=471263051)

Connected to server version 11.2.0

Control Files in database:
    /opt/oracle/oradata/TESTDB/control01.ctl
    /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl

Change database name of database DUMMYDB to DEMODB? (Y/[N]) => Y

Proceeding with operation
Changing database name from DUMMYDB to DEMODB
    Control File /opt/oracle/oradata/TESTDB/control01.ctl - modified
    Control File /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl - modified
    Datafile /opt/oracle/oradata/TESTDB/system01.db - wrote new name
    Datafile /opt/oracle/oradata/TESTDB/users01.db - wrote new name
    Datafile /opt/oracle/oradata/TESTDB/sysaux01.db - wrote new name
    Datafile /opt/oracle/oradata/TESTDB/undotbs01.db - wrote new name
    Control File /opt/oracle/oradata/TESTDB/control01.ctl - wrote new name
    Control File /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl - wrote new name
    Instance shut down

Database name changed to DEMODB.
Modify parameter file and generate a new password file before restarting.
Succesfully changed database name.
DBNEWID - Completed succesfully.


Step 3) Make changes to pfile and password file and opened the database with resetlogs option

$ cd $ORACLE_HOME/dbs

Edit pfile   ---->  initDEMODB.ora

Vi   initDEMODB.ora

Change ‘dbname



NOTE: database need not to be open with RESTLOGS option

$ export ORACLE_SID=DEMODB

$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Tue Sep 18 14:03:10 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size                  2233336 bytes
Variable Size             616565768 bytes
Database Buffers          444596224 bytes
Redo Buffers                5541888 bytes
Database mounted.
Database opened.
SQL> select name from v$database;

NAME
---------
DEMODB

Now, your database name has been changed :-)

See:  How to Change the DBID, DBNAME Using NID Utility in version 10gR2 onwards (Doc ID 863800.1)



Monday, September 17, 2012

Steps to Change database name and DBID


$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 – Production

Here I’m renaming TESTDB database name to DEMODB database including DBID


STEP 1 ) Shut down the database and open database in mount

SQL> select name from v$database;

NAME
---------
TESTDB

SQL> select instance_name,status from v$instance;

INSTANCE_NAME    STATUS
----------------            ------------
TESTDB                        OPEN


SQL> shut immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size                  2233336 bytes
Variable Size             683674632 bytes
Database Buffers          377487360 bytes
Redo Buffers                5541888 bytes
Database mounted.
SQL>


Step 2) Invoke the DBNEWID utility (nid) specifying the new database name in DBNAME

$ nid target=/  dbname=DEMODB

DBNEWID: Release 11.2.0.2.0 - Production on Mon Sep 17 14:45:30 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Connected to database TESTDB (DBID=2577169847)

Connected to server version 11.2.0

Control Files in database:
    /opt/oracle/oradata/TESTDB/control01.ctl
    /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl

Change database ID and database name TESTDB to DEMODB? (Y/[N]) => y

Proceeding with operation
Changing database ID from 2577169847 to 3790527754
Changing database name from TESTDB to DEMODB
    Control File /opt/oracle/oradata/TESTDB/control01.ctl - modified
    Control File /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl - modified
    Datafile /opt/oracle/oradata/TESTDB/system01.db - dbid changed, wrote new name
    Datafile /opt/oracle/oradata/TESTDB/sysaux01.db - dbid changed, wrote new name
    Datafile /opt/oracle/oradata/TESTDB/users01.db - dbid changed, wrote new name
    Datafile /opt/oracle/oradata/TESTDB/undotbs01.db - dbid changed, wrote new name
    Control File /opt/oracle/oradata/TESTDB/control01.ctl - dbid changed, wrote new name
    Control File /opt/oracle/app/fast_recovery_area/TESTDB/control02.ctl - dbid changed, wrote new name
    Instance shut down

Database name changed to DEMODB.
Modify parameter file and generate a new password file before restarting.
Database ID for database DEMODB changed to 3790527754.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.

Step 3) Make changes to pfile and password file and opened the database with resetlogs option

$ cd $ORACLE_HOME/dbs

Edit pfile   ---->  initDEMODB.ora

Vi   initDEMODB.ora

Change ‘dbname’

$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Mon Sep 17 19:28:23 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> STARTUP MOUNT
ORACLE instance started.

Total System Global Area 1068937216 bytes
Fixed Size                  2233336 bytes
Variable Size             616565768 bytes
Database Buffers          444596224 bytes
Redo Buffers                5541888 bytes
Database mounted.

SQL> alter database open resetlogs;
Database altered.

SQL> select name from v$database;

NAME
---------
DEMODB

Also see:  

How to Change the DBID, DBNAME Using NID Utility in version 10gR2 onwards (Doc ID 863800.1)


Thursday, August 16, 2012

ORA-12162: TNS:net service name is incorrectly specified


oracle@oradev801:/opt/oracle INT$  sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Thu Aug 16 16:57:18 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

ERROR:
ORA-12162: TNS:net service name is incorrectly specified
  
Enter user-name:
  
Strange!!
I thought it is a problem with the TNS entry, but after doing some research I came to know that it was a problem with ORACLE_SID, May be ORACLE_SID is not properly set or it was not exported.

oracle@oradev801:/opt/oracle INT$  export ORACLE_SID=XXXXXX
oracle@oradev801:/opt/oracle INT$  echo $ORACLE_SID
XXXXXX
oracle@oradev801:/opt/oracle INT$  sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Thu Aug 16 16:57:18 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>  select instance_name from v$instance;

INSTANCE_NAME
----------------
XXXXXX


On WINDOWS:  set ORACLE_SID=XXXXXX


Wednesday, August 8, 2012

ORA-12906, DROPPING DEFAULT TEMPORARY TABLESPACE


First find the Default temporary tablespace in your database

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL>
SQL > select  property_value  from  database_properties  Where  property_name = 'DEFAULT_TEMP_TABLESPACE';

PROPERTY_VALUE
--------------------------------------------------------------------------------
TEMP


Check the size of TEMP tablespace:

SQL > SELECT tablespace_name, file_name, bytes/1024/1024/1024 GB FROM dba_temp_files WHERE tablespace_name like 'TEMP%';

TABLESPACE_NAME         FILE_NAME                                                   GB
--------------------------     --------------------------------                        ------------
  TEMP                        /opt/oracle/oradata/temp01.dbf                        6


For Oracle 8 and above, the following query will return all users and their SIDs which are doing a sort:

 SELECT   b.tablespace, b.segfile#, b.segblk#, b.blocks, a.sid, a.serial#, a.username, a.osuser, a.status  FROM     v$session a,v$sort_usage b  WHERE    a.saddr = b.session_addr  ORDER BY b.tablespace, b.segfile#, b.segblk#, b.blocks;


To see USED and FREE space of it:

SQL> select SUM(bytes_used)/1024/1024/1024 GBused, SUM(bytes_free)/1024/1024/1024 GBfree from  v$temp_space_header;

  GBUSED     GBFREE
------------    ---------
   1.25                 4.75

Now, try to drop the Default temp tablespace

SQL> DROP TABLESPACE temp;
DROP TABLESPACE temp
*
ERROR at line 1:
ORA-12906: cannot drop default temporary tablespace

In order to drop and recreate the default temp tablepsace first thing is to create another temporary tablespace and make that tablespace as default tablespace for the database and then drop the old default tablespace(TEMP)

SQL>   CREATE TEMPORARY TABLESPACE temp_new TEMPFILE  '/opt/oracle/oradata/temp_new01.dbf' size 5G reuse  AUTOEXTEND ON NEXT 1M MAXSIZE unlimited EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;
Tablespace created.

SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp_new; 
Database altered.

Now drop the TEMP tablespace

SQL> DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;
Tablespace dropped.

Monday, August 6, 2012

Find the delay of standby from primary


Here is a quick way to find the current timestamp from the current SCN.

On primary:

SQL> select to_char(CURRENT_SCN) from v$database;

TO_CHAR(CURRENT_SCN)
————————————
12345678909

SQL> select scn_to_timestamp(48133107062) from dual;

SCN_TO_TIMESTAMP(12345678909)
—————————————————
19-JUN-11 08.40.27.000000000 AM

SQL>


And do the same on standby database, then find the difference in time.

If you set the delay parameter then find it in the parameter file

Thanks !!

Thursday, August 2, 2012

import only one table ROWS


TASK :  I want to import 1 schema table rows from a full or full schema dump

SCHEMA: Chandra
Table     :  Demo
Dumpfile : dummy.dmp
Directory = TEST

$ impdp dumpfile=dummy.dmp directory=TEST content=data_only tables=DEMO logfile=impdp_demo.log

Import: Release 11.2.0.2.0 - Production on Thu Aug 2 14:59:52 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Username: / as sysdba

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39002: invalid operation
ORA-39166: Object SYS.DEMO was not found.

REASON  : Here we are running this import as sysdba user so oracle will find and import the sys.table rows , since it is not found thrown an error above

$ impdp dumpfile=dummy.dmp directory=TEST content=DATA_ONLY tables=DEMO logfile=impdp_demo.log schemas=chandra

Import: Release 11.2.0.2.0 - Production on Thu Aug 2 15:00:46 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Username: / as sysdba

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
UDI-00010: multiple job modes requested, schema and tables

REASON : I tried to import table rows by using both SCHEMAS at the same time


$ impdp dumpfile=dummy.dmp directory=TEST content=DATA_ONLY tables=DEMO logfile=impdp_demo.log

Import: Release 11.2.0.2.0 - Production on Thu Aug 2 15:03:50 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Username: Chandra
Password : *********

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_02" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_02":  /******** AS SYSDBA dumpfile=chandra_0731.dmp directory=EXPORT_DIR content=data_only tables=demo logfile=impdp_0802.log
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "CHANDRA"."DEMO"                692.9 KB   15700 rows
Job "SYS"."SYS_IMPORT_TABLE_02" successfully completed at 15:03:58


Here it searched for Chandra.demo table and imported rows

(or)

 impdp dumpfile=dummy.dmp directory=TEST content=DATA_ONLY tables=chandra.DEMO logfile=impdp_demo.log
 Import: Release 11.2.0.2.0 - Production on Thu Aug 2 15:03:50 2012

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Username: / as sysdba

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYS"."SYS_IMPORT_TABLE_02" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLE_02":  /******** AS SYSDBA dumpfile=chandra_0731.dmp directory=TEST content=data_only tables=chandra.demo logfile=impdp_0802.log
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "CHANDRA"."DEMO"                692.9 KB   15700 rows
Job "SYS"."SYS_IMPORT_TABLE_02" successfully completed at 15:03:58

NOTE :   schema “CHANDRA” from which you are importing should have ‘imp_full_database’ role granted, also READ and WRITE permissions on TEST Directory

Thank you !!! :-) 

Auto Scroll Stop Scroll