Total Pageviews

Wednesday, 16 May 2012

Export Data Oracle to CSV


Follow below steps to create a CSV file from a employee table:



SQL> set colsep ";"
SQL> set heading off
SQL> set line 1000
SQL> spool employee.csv

SQL> select * from employee

      7782;CLARK     ;MANAGER  ;        10
      7839;KING      ;PRESIDENT;        11
      7934;MILLER    ;CLERK    ;        15

SQL>spool off

Monday, 14 May 2012

Setting up the .bash_profile in oracle 10g


# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi
### User specific environment and startup programs
export ORACLE_BASE=/home/oracle/10.2.0/product
export ORACLE_HOME=$ORACLE_BASE/db_1
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export CLASSPATH=$ORACLE_HOME/jre:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export TMP=/home/oracle/tempdir
export TEMPDIR=/home/oracle/tempdir
unset USERNAME

To make the changes in .bash_profile run the belowcommand:

[oracle@Sumit -SERVER ~]$ source ~/.bash_profile

Make sure all the enviornment variables are set properly.You can verify from below:


[oracle@sumit-server ~]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/oracle/10.2.0/product/db_1/bin
[oracle@sumit-server ~]$ echo $ORACLE_BASE
/home/oracle/10.2.0/product
[oracle@sumit-server ~]$ echo $ORACLE_HOME
/home/oracle/10.2.0/product/db_1
[oracle@sumit-server ~]$ echo $LD_LIBRARY_PATH
/home/oracle/10.2.0/product/db_1/lib
[oracle@sumit-server ~]$ echo $CLASSPATH
/home/oracle/10.2.0/product/db_1/jre:/home/oracle/10.2.0/product/db_1/jlib:/home/oracle/10.2.0/product/db_1/rdbms/jlib

Wednesday, 9 May 2012

MUTT command not Found - Crontab


I have tested that MUTT is working when we are sending a test email directly using the mutt command but when we are trying to use MUTT  inside a script scheduled through crontab it is giving the below error:

mutt -s "This is a test email  --" dba.tyagisumit@gmail.com < /home/oracle/sumit_scripts/myfile.result


"MUTT command not Found"

Solution: If script is working properly when you ran it independently using  SH comand and not working through crontab , it seems this is due to permission issue. Crontab tab do not have execute permission.

Workaround: Find the directory where MUTT is installed using the command below:

              Which mutt
            -- /usr/local/bin/mutt


use complete path inside your script like shown below:


/usr/local/bin/mutt -s "This is a test email  --" dba.tyagisumit@gmail.com < /home/oracle/sumit_scripts/myfile.result




Tuesday, 8 May 2012

Archive Log Mode

Lets start by checking the current archive mode.

SQL> SELECT LOG_MODE FROM  SYS.V$DATABASE;

LOG_MODE
------------
NOARCHIVELOG


Follow the below steps to alter the Archive mode:


#1. Shutdown Immediate

#2. STARTUP MOUNT

#3.ALTER DATABASE ARCHIVELOG;

#4. ALTER DATABASE OPEN;

To get the information about Archive mode:

SQL> Archive log list;

Database log mode : Archive Mode
Automatic archival  : Enabled
Archive destination : C:\oracle\product\10.2.0\log_archive_dest\ORCL2
Oldest online log sequence : 15
Next log sequence to archive : 17
Current log sequence : 17

SQL>

We can verify that we are infact in ARCHIVELOG mode: 

SQL> select log_mode from v$database;

LOG_MODE
------------
ARCHIVELOG

SQL> SELECT  DEST_NAME,STATUS,DESTINATION from V$ARCHIVE_DEST;
  •   Status can be valid or Invalid.
  •    Dest_Name :LOG_ARCHIVE_DEST_N  where N= 1 to 10.
  •    Destination: "C:\oracle\product\10.2.0\flash_recovery_area" Default location for 10g
Alter the Destination :

Alter system set log_archive_dest_1='location=C:\oracle\product\10.2.0\log_archive_dest\ORCL2';







ORA-01775: looping chain of synonyms


#1. you can run the query
SELECT table_owner, table_name, db_link
  FROM dba_synonyms 
 WHERE owner        = 'PUBLIC'
   AND synonym_name = <<synonym name>>
to see what the public synonym currently points at.
After debugging I figured out that the actual tables were misssing, which I was referring using synonyms. So I suggest - first check that whether the tables exists!! :-))


#2. Second possible reason: You have created a series of synonyms that resulted in a circular reference.

A circular reference can occur as follows:

Create SYNONYM syn1 for syn2;

Create SYNONYM syn2 for syn3;
Create SYNONYM syn3 for syn1;



Multiplex the Control files in Oracle 10g

#1.Shut down the database.
#2.Make a copy of the existing control file to a different device by using operating system commands.

#3.Edit or add the CONTROL_FILES  parameter and specify names of all the control files.
#4. Start the database.


Monday, 7 May 2012

scp - secure copy (remote file copy program)

 scp -P 22 dump_file_name.dmp.gz oracle@IP_Address_remote_server:/home/oracle/

scp copies files between hosts on a network. It uses ssh  for data transfer, and uses the same authentication and provides the same security as ssh. Unlike rcp, scp will ask for passwords or passphrases if they are needed for authentication.


-p atribute Preserves modification times, access times, and modes from the original file.

Thursday, 3 May 2012

EXECUTE IMMEDIATE ORA-01031: insufficient privileges



An interesting experiment on EXECUTE IMMEDIATE Oracle Feature. It allows you to execute DYNAMIC SQL Statements inside the PLSQL Procedure. For SYNTAX and more information about this you can find out in Oracle Documentation.

Now we are going to Create one Database User and then we will write one PLSQL procedure with EXECUTE IMMEDIATE statement. Lets see,

– Login as SYSDBA and Create the user called ‘sumit’

SQL> create user sumit identified by sumit;

User created.

SQL> grant connect,resource to sumit;

Grant succeeded.

Database user ‘sumit’ has been created successfully and granted CONNECT, RESOURCE roles to sumit user.

The user called ‘sumit’ has been created and granted some roles. Now connect as sumit user and Create a PLSQL procedure with
EXECUTE IMMEDIATE Statement.

SQL> conn sumit/sumit
 Connected.


 /*  Create a procedure to create table called t with attributes id,name with

          EXECUTE IMMEDIATE Statement */

SQL> create or replace procedure p
   2             is
   3             begin
   4              EXECUTE IMMEDIATE ‘CREATE TABLE T(ID NUMBER, NAME VARCHAR2(20))’;
   5             end;
   6             /

Procedure created.

SQL>  — Created the procedure called “P”. Let me execute this procedure…
 SQL> execute p;
 BEGIN p; END;

*
 ERROR at line 1:
ORA-01031: insufficient privileges
 ORA-06512: at “sumit.P”, line 4
 ORA-06512: at line 1

Oops!, got an error “insufficient privileges” - Do we have this privilege?, YES. we have. It has been grouped under RESOURCE role and the RESOURCE role has been granted to this user (FYI: We have queried the Data Dictionary table to see the privileges that are mapped under RESOURCE role. SEE ABOVE). Then, What is stopping us?

Let me grant the CREATE TABLE privilege directly to the sumit user.

SQL> conn sys@orcl as sysdba
 Enter password: *******
 Connected.
 SQL> grant CREATE TABLE to sumit;

Grant succeeded.

SQL> conn sumit/sumit
 Connected.
 SQL> — Let me execute the procedure now
 SQL> execute p;

PL/SQL procedure successfully completed.

SQL> — Wow! Table ‘T’ got created.
 SQL> desc t;
  Name                                      Null?    Type
  —————————————————————————–
  ID                                                       NUMBER
  NAME                                                 VARCHAR2(20)

 The Conculsion is,

In STORED PROCEDURE, roles are DISABLED. Hence any PRIVILEGES granted by a ROLE to USER, will NOT be in effect. The PRIVILEGES MUST be directly granted to the user.

Or u can user the  "authid current_user" clause.
  

Wednesday, 2 May 2012

Jobs status in oracle

SELECT TO_CHAR(log_date, 'DD-MON-YY HH24:MM:SS') TIMESTAMP, job_name, status,
SUBSTR(additional_info, 1, 40) ADDITIONAL_INFO
FROM ALL_scheduler_job_run_details
ORDER BY log_date;

Scheduled Jobs in oracle 10g not running as per schedule timings

Today, I faced with strange situation for a client. I submitted a job in dba_jobs with a minute interval. This job was running simple stored procedure.
Job was not run every minute while manual run (with dbms_job.run) was successful.
Job_queue_process was 10 and _job_queue_inteval was 5 which means scheduler checks job for execution every 5 seconds.
Eventually found that background process cjq0 was the culprit.This background process is responsible for running scheduled job.It does initiate J00 processes for executing scheduled jobs.
This background process was hung, as the result it does not initiate any J00 process which means job was not run every minutes.


Resolution :
Kill cjq0 process from OS
alter system set job_queue_process=0;
alter system set job_queue_process=10;
Should fork new cjq0 process and all J00 process for running jobs.