Monday, August 11, 2008

Subqueries in Oracle with example

Generally a query is an operation that retrieves data from one or more tables or views. If you define both query and subquery specifically then query is the top-level SELECT statement and the subquery is the nested within another SQL statement. So in simple terms we can say subquery is the query within a query.

A subquery answer multiple parts of the question. For example whenever you asked to find the student name of a department CSE then a simple query

SELECT name from student where dept_name='CSE';

is enough. But if you asked for find the student name who are in Arju's department. Then what you will do? First you have to determine the department where Arju belong to. This part is subquery. And then you have to determine the name of the students with Arju's department. Like,

SELECT name from student where dept_name in(SELECT dept_name from student where name='ARJU');


Here, the later part SELECT dept_name from student where name='ARJU' is subquery and SELECT name from student part is identified as query.

A subquery can contain another subquery. Oracle Database imposes no limit on the number of subquery levels in the FROM clause of the top-level query. You can nest up to 255 levels of subqueries in the WHERE clause.
Related Documents
http://arjudba.blogspot.com/2009/12/oracle-object-type-exercises-varray.html
http://arjudba.blogspot.com/2009/12/practice-oracle-joins-examples.html
http://arjudba.blogspot.com/2009/12/oracle-security-practices.html
http://arjudba.blogspot.com/2009/12/exercises-with-oracle-create-table-add.html
http://arjudba.blogspot.com/2009/12/oracle-database-creation-exercises.html
http://arjudba.blogspot.com/2009/12/basic-oracle-sql-exercise.html
http://arjudba.blogspot.com/2009/08/format-model-modifiers-fx-and-fm.html
http://arjudba.blogspot.com/2009/08/number-format-models-in-oracle.html
http://arjudba.blogspot.com/2009/08/format-models-in-oracle.html
http://arjudba.blogspot.com/2009/07/sql-decode.html
http://arjudba.blogspot.com/2009/07/how-to-know-row-of-table-belong-to.html
http://arjudba.blogspot.com/2009/06/how-to-know-which-objects-are-being.html
http://arjudba.blogspot.com/2009/06/ddl-with-wait-option-in-11g.html
http://arjudba.blogspot.com/2009/06/ora-00939-too-many-arguments-when-case.html
http://arjudba.blogspot.com/2009/03/oracle-datatype-internal-code.html
http://arjudba.blogspot.com/2009/03/how-to-know-list-of-constraints-and.html
http://arjudba.blogspot.com/2009/02/how-to-know-dependent-objectswhich.html
http://arjudba.blogspot.com/2009/02/how-to-search-stringkey-value-from.html
http://arjudba.blogspot.com/2009/02/how-to-know-when-tableobjects-ddlcode.html
http://arjudba.blogspot.com/2009/02/ora-00920-invalid-relational-operator.html
http://arjudba.blogspot.com/2009/01/adding-default-value-to-column-on-table.html
http://arjudba.blogspot.com/2009/01/ora-12838-cannot-readmodify-object.html
http://arjudba.blogspot.com/2009/01/ora-01779-cannot-modify-column-which.html
http://arjudba.blogspot.com/2009/01/updating-table-based-on-another-table.html
http://arjudba.blogspot.com/2009/01/ora-00054-resource-busy-and-acquire.html
http://arjudba.blogspot.com/2008/12/troubleshoot-ora-02292-ora-02449-and.html
http://arjudba.blogspot.com/2008/06/ora-00903-oracle-database-reserved.html
http://arjudba.blogspot.com/2008/06/hints-in-oracle.html
http://arjudba.blogspot.com/2008/06/examples-of-usage-of-composite-index.html
http://arjudba.blogspot.com/2008/06/find-indexes-and-assigned-columns-for.html
http://arjudba.blogspot.com/2008/06/reasons-for-using-alter-table-statement.html
http://arjudba.blogspot.com/2008/06/alter-table-rename-table-add-column.html
http://arjudba.blogspot.com/2008/06/ora-01830-date-format-picture-ends.html
http://arjudba.blogspot.com/2008/06/default-date-timestamp-and-timestamp.html
http://arjudba.blogspot.com/2008/06/create-temporary-table-in-oracle.html
http://arjudba.blogspot.com/2008/06/example-of-antijoin-semijoin-curtesian.html
http://arjudba.blogspot.com/2008/12/ora-02297-cannot-disable-constraint.html
http://arjudba.blogspot.com/2008/10/convert-decimal-to-hexadecimal-on.html
http://arjudba.blogspot.com/2008/10/how-to-generate-fibonacci-series-in.html
http://arjudba.blogspot.com/2008/10/same-sounded-words-in-oracle.html
http://arjudba.blogspot.com/2008/09/type-of-constraint-in-oracle.html
http://arjudba.blogspot.com/2008/09/how-to-move-lob-data-to-another.html
http://arjudba.blogspot.com/2008/08/subqueries-in-oracle-with-example.html
http://arjudba.blogspot.com/2008/08/how-to-monitor-alert-log-file-in-oracle.html
http://arjudba.blogspot.com/2008/08/solution-of-ora-01873-leading-precision.html
http://arjudba.blogspot.com/2008/07/literals-and-literal-types-in-oracle.html
http://arjudba.blogspot.com/2008/07/ora-01722-invalid-number.html
http://arjudba.blogspot.com/2008/07/ora-00936-missing-expression.html
http://arjudba.blogspot.com/2008/07/ora-01756-quoted-string-not-properly.html
http://arjudba.blogspot.com/2008/07/pls-00428-into-clause-is-expected-in.html
http://arjudba.blogspot.com/2008/07/schema-object-naming-rules.html
http://arjudba.blogspot.com/2008/06/datetime-and-interval-datatypes.html
http://arjudba.blogspot.com/2008/06/large-object-lob-datatypes-with-example.html
http://arjudba.blogspot.com/2008/06/history-of-sql.html
http://arjudba.blogspot.com/2008/06/what-is-sql.html

How to Schedule or Automate Backup through Crontab

Schedule or Automate Backup is a needed thing almost in all environment. We can do automate or scheduling tasks through two ways, one is DBMS_SCHEDULER packager which will be discusses in another topic and another is OS scheduler. If you think about OS scheduler then on unix box use crontab and on windows box use scheduling jobs.

In this topic I have shown how we can take automate backup through crontab tool.

To avoid error Verify that ORACLE_HOME is set properly error like as below
Message file RMAN.msb not found
Verify that ORACLE_HOME is set properly

set proper ORACLE_HOME and ORACLE_SID in your backup file.
So I created my backup_job.sh as below.

bash-3.00$ vi backup_job.sh
#!/bin/bash
export ORACLE_HOME=/oracle/app/oracle/product/10.2.0/db_1
export ORACLE_SID=dbase
/oracle/app/oracle/product/10.2.0/db_1/bin/rman target sys/a@neptune:1522/dbase cmdfile=/oradata1/
backup/backup_job2.sh


If you don't set $ORACLE_HOME and don't give full path of rman then possibly in the output file you get rman:not found error.
/oradata1/backup/backup_job.sh: rman: not found

In the script I provided full path of rman.

The actual backup script are in backup_job2.sh and from backup_job.sh it will be called through rman.

bash-3.00$ vi backup_job2.sh
backup database format '/oradata1/backup/%U';


If you don't make these script executable or don't change permission to 777 then you likely get error in the output file as,
sh: /oradata1/backup/backup_job.sh: cannot execute
So change the permission as

bash-3.00$ chmod 777 backup_job.sh
bash-3.00$ chmod 777 backup_job2.sh

To write contents within crontab in vi editor issue,
bash-3.00$export EDITOR=vi
bash-3.00$ crontab -e
35 06 * * * /oradata1/backup/backup_job1.sh > /oradata1/backup/backupdata.log


By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

48 06 * * * /oradata1/backup/backup_job.sh > /oradata1/backup/backupdata.log 2>&1

Now I will discuss some aspects of cronjob.

Crontab Restrictions
-You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow.

-If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny.

-If only cron.deny exists and is empty, all users can use crontab.

-If neither file exists, only the root user can use crontab.

-The allow/deny files consist of one user name per line.

Crontab Commands
Before invoking crontab use export EDITOR=vi ;to specify a editor to open crontab file.
Now open crontab as below
-crontab -e :Edit your crontab file, or create one if it doesn't already exist.
-crontab -l :Display your crontab file.
-crontab -r :Remove your crontab file.
-crontab -v :Display the last time you edited your crontab file. (This option is only available on a few systems.)

Crontab syntax option
MINUTE HOUR DAY_OF_MONTH MONTH DAY_OF_WEEK USER COMMAND
for any field multiple value could assign, where they are separated by comma.

MINUTE accept value from 00 to 59
HOUR accept value from 00 to 23
DAY_OF_MONTH accept value from 01 to 31
MONTH accept value from 01 to 12
DAY_OF_WEEK accept value from 0 to 6 [0 for sunday, 1 for monday, ...]
--USER means that who will execute the command (optional/not common)
COMMAND the command for execution

How to monitor alert log file in Oracle

We know in a UNIX system with tail -f we can monitor alert log file to see how alert log file is populated. For example the latest happenings in alert log file can be seen by,
$tail -f background_dump_dest location/alert_$ORACLE_SID.ora

But in windows there is not tail command. We can monitor the alert log within oracle itself regardless of platform. This can be archived by following steps.

1)Create an External Table to read the alert log.

column a_log new_value ALOG noprint
column value new_value bkgd_dmp noprint

select 'alert_'||instance_name||'.log' a_log
from v$instance;

select value
from v$parameter
where name = 'background_dump_dest';

create or replace directory data_dir as '&&bkgd_dmp';

CREATE TABLE alert_t
(
text_line varchar2(255)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY data_dir
ACCESS PARAMETERS
(
records delimited by newline
fields
REJECT ROWS WITH ALL NULL FIELDS
)
LOCATION
(
'&&ALOG'
)
)
REJECT LIMIT unlimited;


2)Query from the External Table.

For example, you can then query the last 21 lines of the alert log in this manner:

select text_line
from (
select rownum rn, text_line
from alert_t
)
where rn between (select max(rn) - 20 from (select rownum rn from
alert_t))
and (select max(rn) from (select rownum rn from alert_t));


Related Documents
What is Alert Log?
What is Server-Generated Alerts?

Sunday, August 10, 2008

ORA-28031: maximum of 148 enabled roles exceeded

Error Description
While connect to database as a normal user it fails with
ORA-28031: maximum of 148 enabled roles exceeded

Cause of The Problem
In a database at one time 148 user-defined roles can be enabled. Enabling more than 148 user-defined roles causes ORA-28031.

Solution of The Problem
At first time you can think about max_enabled_roles parameter but max_enabled_roles parameter has nothing to do if you enable 148 user-defined roles.

In fact this parameter is deprecated. It is retained for backward compatibility only. You can see this parameter settings by,

SQL> show parameter max_enable

NAME TYPE VALUE
------------------------------------ --------------------------------- ------------------------------
max_enabled_roles integer 150

Though this parameter is set to 150 and you have enabled 148 user defined roles but additional user-defined roles can't be enabled because of the actual number of roles that users can enable is 148. So max_enabled_roles is 2 plus 148, because each user has two additional roles, PUBLIC and the user's own role.

As there is limit to 148 so you have to disable fewer roles or drop some roles and reorganize your database role policy.

You can enable or disable roles by using the SET ROLE statement for your current session.

If you want to enable all roles except manager and clerk role then use,

SET ROLE ALL EXCEPT manager, clerk;

Remember roles listed in the EXCEPT clause must be roles granted directly to you. They cannot be roles granted to you through other roles.

Tuesday, August 5, 2008

Solution of ORA-01873: the leading precision of the interval is too small

Error Description
Running the following query produce error ORA-01873.
SQL> select count(*) from log_book where created < SYSDATE-interval '270' day;select count(*) from log_book where created < SYSDATE-interval '270' day*ERROR at line 1:ORA-01873: the leading precision of the interval is too smallCause of The Problem
When Passing Datetime Interval with Precision > 2 will raise this error. In above we gave 270 which is 3 bytes and hence error occurs. This is as a result of the default scale (2) being smaller than the maximum (9).

Solution of The Problem

If you give precision greater than 2 then you explicitly give the value of the precision. Here 270 is 3 bytes. So we will give 3 after day like,
SQL>select count(*) from log_book where created < SYSDATE-interval '270' day(3);

As default scale is 2 so,
SELECT count(*) from log_book where created < SYSDATE-interval '27' day; will not raise error.

In order to use more than 2 we explicitly need to specify scale. For 4 bytes,
SELECT count(*) from log_book where created < SYSDATE-interval '2711' day(4);
Related Documents
http://arjudba.blogspot.com/2009/12/oracle-object-type-exercises-varray.html
http://arjudba.blogspot.com/2009/12/practice-oracle-joins-examples.html
http://arjudba.blogspot.com/2009/12/oracle-security-practices.html
http://arjudba.blogspot.com/2009/12/exercises-with-oracle-create-table-add.html
http://arjudba.blogspot.com/2009/12/oracle-database-creation-exercises.html
http://arjudba.blogspot.com/2009/12/basic-oracle-sql-exercise.html
http://arjudba.blogspot.com/2009/08/format-model-modifiers-fx-and-fm.html
http://arjudba.blogspot.com/2009/08/number-format-models-in-oracle.html
http://arjudba.blogspot.com/2009/08/format-models-in-oracle.html
http://arjudba.blogspot.com/2009/07/sql-decode.html
http://arjudba.blogspot.com/2009/07/how-to-know-row-of-table-belong-to.html
http://arjudba.blogspot.com/2009/06/how-to-know-which-objects-are-being.html
http://arjudba.blogspot.com/2009/06/ddl-with-wait-option-in-11g.html
http://arjudba.blogspot.com/2009/06/ora-00939-too-many-arguments-when-case.html
http://arjudba.blogspot.com/2009/03/oracle-datatype-internal-code.html
http://arjudba.blogspot.com/2009/03/how-to-know-list-of-constraints-and.html
http://arjudba.blogspot.com/2009/02/how-to-know-dependent-objectswhich.html
http://arjudba.blogspot.com/2009/02/how-to-search-stringkey-value-from.html
http://arjudba.blogspot.com/2009/02/how-to-know-when-tableobjects-ddlcode.html
http://arjudba.blogspot.com/2009/02/ora-00920-invalid-relational-operator.html
http://arjudba.blogspot.com/2009/01/adding-default-value-to-column-on-table.html
http://arjudba.blogspot.com/2009/01/ora-12838-cannot-readmodify-object.html
http://arjudba.blogspot.com/2009/01/ora-01779-cannot-modify-column-which.html
http://arjudba.blogspot.com/2009/01/updating-table-based-on-another-table.html
http://arjudba.blogspot.com/2009/01/ora-00054-resource-busy-and-acquire.html
http://arjudba.blogspot.com/2008/12/troubleshoot-ora-02292-ora-02449-and.html
http://arjudba.blogspot.com/2008/06/ora-00903-oracle-database-reserved.html
http://arjudba.blogspot.com/2008/06/hints-in-oracle.html
http://arjudba.blogspot.com/2008/06/examples-of-usage-of-composite-index.html
http://arjudba.blogspot.com/2008/06/find-indexes-and-assigned-columns-for.html
http://arjudba.blogspot.com/2008/06/reasons-for-using-alter-table-statement.html
http://arjudba.blogspot.com/2008/06/alter-table-rename-table-add-column.html
http://arjudba.blogspot.com/2008/06/ora-01830-date-format-picture-ends.html
http://arjudba.blogspot.com/2008/06/default-date-timestamp-and-timestamp.html
http://arjudba.blogspot.com/2008/06/create-temporary-table-in-oracle.html
http://arjudba.blogspot.com/2008/06/example-of-antijoin-semijoin-curtesian.html
http://arjudba.blogspot.com/2008/12/ora-02297-cannot-disable-constraint.html
http://arjudba.blogspot.com/2008/10/convert-decimal-to-hexadecimal-on.html
http://arjudba.blogspot.com/2008/10/how-to-generate-fibonacci-series-in.html
http://arjudba.blogspot.com/2008/10/same-sounded-words-in-oracle.html
http://arjudba.blogspot.com/2008/09/type-of-constraint-in-oracle.html
http://arjudba.blogspot.com/2008/09/how-to-move-lob-data-to-another.html
http://arjudba.blogspot.com/2008/08/subqueries-in-oracle-with-example.html
http://arjudba.blogspot.com/2008/08/how-to-monitor-alert-log-file-in-oracle.html
http://arjudba.blogspot.com/2008/08/solution-of-ora-01873-leading-precision.html
http://arjudba.blogspot.com/2008/07/literals-and-literal-types-in-oracle.html
http://arjudba.blogspot.com/2008/07/ora-01722-invalid-number.html
http://arjudba.blogspot.com/2008/07/ora-00936-missing-expression.html
http://arjudba.blogspot.com/2008/07/ora-01756-quoted-string-not-properly.html
http://arjudba.blogspot.com/2008/07/pls-00428-into-clause-is-expected-in.html
http://arjudba.blogspot.com/2008/07/schema-object-naming-rules.html
http://arjudba.blogspot.com/2008/06/datetime-and-interval-datatypes.html
http://arjudba.blogspot.com/2008/06/large-object-lob-datatypes-with-example.html
http://arjudba.blogspot.com/2008/06/history-of-sql.html
http://arjudba.blogspot.com/2008/06/what-is-sql.html

Error 45 initializing SQL*Plus Internal error

If you are executing normal SQL script then review the script. I ran one SQL script and I saw after executing the script at the end it generated Error 45 initializing SQL*Plus Internal error. I review the script and I saw at the end of it there are some spool output. So I deleted that I ran again and it solved the problem.

However there is related oracle bug when fired whenever you run a Sql Script From OCFS. This bug retained from SQL*Plus - Version 9.0.1.0 to 10.1.0.2. When SQL script file is on a OCFS filesystem created on shared device then executing that script from SQL*Plus will fire the bug.

You can reproduce the bug by following steps.

1.Create a test.sql script on OCFS filesystem containing sql:
startup nomount

2. Check that test.sql script is in correct directory:
$ ls -l /ocfs/scripts
total 1
-rw-r--r-- 1 oracle dba 17 Mar 25 03:12 test.sql

3. Invoke SQL*Plus:
$ sqlplus /nolog

SQL*Plus: Release 10.1.0.2.0 - Production on Wed Aug 25 18:17:41 2004
Copyright (c) 1982, 2004, Oracle. All rights reserved.

4. Connect to database:
SQL> connect / as sysdba
Connected.

5. Execute script:
SQL> @/ocfs/scripts/test.sql
Error 45 initializing SQL*Plus
Internal error

Solution of The Problem
If this happens while you execute your script from OCFS then just copy the SQL script file to a local disk and then execute the script.

How to know database initialization parameter in oracle

1)SHOW PARAMETER option: If you use SQL*Plus then you can use SHOW PARAMETER parameter_name to display the values of parameters currently in use. Note that with toad or tora you can't use it as it is SQL*plus option.

SQL>SHOW PARAMETERS sga_target;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
sga_target big integer 160M

2)Query from V$PARAMETER:
You can query value column of this view to display the values of parameters currently in effect.

SQL> SELECT VALUE FROM v$PARAMETER WHERE name='sga_target';

VALUE
--------------------------------------------------------------------------------
167772160

3)Query from V$PARAMETER2:
You can query value column of this view to display the values of parameters currently in effect. It is easier to distinguish list parameter values in this view because each list parameter value appears as a row.

4)Query from V$SPPARAMETER: You can query value column of this view to display the current contents of the server parameter file. The view returns FALSE values in the ISSPECIFIED column if a server parameter file is not being used by the instance.

5)Query from V$SYSTEM_PARAMETER:
In this view it displays what will be the value if a new session inherits parameter values from the instance-wide values.

6)Query from V$SYSTEM_PARAMETER2: It displays the information about initialization parameters that are currently in effect for the instance, with each list parameter value appearing as a row in the view. A new session inherits parameter values from the instance-wide values.

Related Documents
Difference between V$parameter and v$spparameter