Wednesday, January 19, 2011

Predefined Exceptions



An internal exception is raised automatically if your PL/SQL program violates an Oracle rule or exceeds a system-dependent limit. PL/SQL predefines some common Oracle errors as exceptions. For example, PL/SQL raises the predefined exception NO_DATA_FOUND if a SELECT INTO statement returns no rows.You can use the pragma EXCEPTION_INIT to associate exception names with other Oracle error codes that you can anticipate. To handle unexpected Oracle errors, you can use the OTHERS handler. Within this handler, you can call the functions SQLCODE and SQLERRM to return the Oracle error code and message text. Once you know the error code, you can use it with pragma EXCEPTION_INIT and write a handler specifically for that error.PL/SQL declares predefined exceptions globally in package STANDARD. You need not declare them yourself. Can wirte handlers for predefined exceptions by using the SQLCODE.
ZERO_DIVIDE -A program attempts to divide a number by zero.
VALUE_ERROR- An arithmetic, conversion, truncation, or size-constraint error occurs. For example, when your program selects a column value into a character variable, if the value is longer than the declared length of the variable, PL/SQL aborts the assignment and raises VALUE_ERROR. In procedural statements, VALUE_ERROR is raised if the conversion of a character string into a number fails. (In SQL statements, INVALID_NUMBER is raised.)

Wednesday, December 15, 2010

Where Current OF

  • If you plan on updating or deleting records that have been referenced by a Select For Update statement, you can use the Where Current Of statement.
  • When the session opens a cursor with the FOR UPDATE clause, all rowsin the return set will hold row-level exculsive locks. Other sessions can only query the rows, but they cannot update, delete, or select withFOR UPDATE.
  • Allows the developer to lock a set of Oracle rows for the duration of a transaction
  • Subqueries cannot have this clause

Monday, September 13, 2010

Explain Plan

An execution plan defines how Oracle finds or writes the data For example, an important decision that Oracle has to take is if it uses indexes or not. And if there are more indexes, which of these is used. All this is contained in an execution plan. SQL statement EXPLAIN PLAN to determines this. The general syntax of EXPLAIN PLAN is:
explain plan for sql-statement;

If you do an EXPLAIN PLAN, Oracle will analyze the statment and fill a special table with the Execution plan for that statement. You can indicate which table has to be filled with the following SQL command:

explain plan into table_name for sql-statement;

If you omit the INTO TABLE_NAME clause, Oracle fills a table named PLAN_TABLE by default.

The Plan Table
The plan table is the table that Oracle fills when you have it explain an execution plan for an SQL statement. You must make sure such a plan table exists. Oracle ships with the script UTLXPLAN.SQL which creates this table, named PLAN_TABLE (which is the default name used by EXPLAIN PLAN). If you like, however, you can choose any other name for the plan table, as long as you have been granted insert on it and it has all the fields as here.
Option tells more about how an operation would be done. For example, the operation TABLE ACCESS can have the options: FULL or BY ROWID or many others.

Full in this case means, that the entire table is accessed (takes a long time if table is huge) whereas BY ROWID means, Oracle knows where (from which block) the rows are to be retrieved, which makes the time to access the table shorter.

FULL - means that the entire table is accessed.

sql*plus automatically explains the plan for you if autotrace is enabled.

INDEX (RANGE SCAN) - basically means, that the index was used, but that it can return more than one row

INDEX (UNIQUE SCAN) - means, that this index is used, and it sort of guarantees that this index returnes exactly one rowid

NESTED LOOPS - For each relevant row in the first table (driving table), find all matching rows in the other table (probed table).

SORT (AGGREGATE) - Whenever a result set must be sorted, the operation is sort. If this sort is used to return a single row (for example max or min) the options is AGGREGATE

Saturday, July 10, 2010

Row Level Security

Oracle Row level security (known as Virtual Private Databse- also known as fine grained access control), which comes as part of Oracle Enterprise Edition. The database can be set up the way that users authorized to access data not only at a table but at the each record level as well.
Transparently modifying requests for data to present a partial view of the tables to the users based on a set of defined criteria.

Oracle Label Security - optional add-on for providing easy to use interface for row-level security. no coding needed. Enables to enfore security, directly on tables, views and synonyms. Allows to define which rows users may have access to.

VPD was introduced in Oracle 8i Version 8.1.5 as a new solution to enfore granular access control
of data at server level.

Oracle 9i Expanded the features.
- Oracle Policy manager
- partitioned fine-grained access control
- global application context
- VPD support of synonyms

Oracle 10g makes the following three major enhancements in VPD

Column level Privacy
It Increases performance by limiting the number of queries that the database rewrites.
rewrite occur when the statement references relevant columns. this feature also leads to more
privacy.

Cutomization
with the introduction of four new types of policies, you can customize VPD to always
enfore the same predicate with a static policy or you can have VPD predicates that change
dynamically with a non-static policy.

Shared Policies
you can apply a single VPD policy to multiple objects, and therfore reduce administration
costs.

Why use VPD

Protect confidential and secret information. Control the delivery of the data to the right people.

VPD Components
- Application context
- PL/SQL Function
- Security Policies

Application Context
Holds Environment variables
- Application name
- USername
Gathers information using dbms_session.set_context
How VPD Works
The virtual private database enabled by associating one or more security policies with tables or views. Direct or indirect access to a table with an attached security policy causes the database to consult a pl/sql function that implements the policy. The policy function returns an access condition known as as predicate (a WHERE Clause), which the database appends to the user's sql statements, thus dynamically modifying the user's data access.
Can implement VPD by writing a stored procedure to append a SQL predicate to each sql statement that controls row level access for that statement.
VPD Policy can be implemented as a pl/sql function. VPD policy function that automatically adds WHERE clause to an incoming select query to limit the data access.

Thursday, July 1, 2010

Connect by Clause

  • The start with connect by clause can be used to select data that has a hierarchical relationship. Recurse condition can make use of the keyword perior.
  • Start with specifies the rows to be identified as a root
  • Siblings by preserves any ordering specified in the hierarchical query clause and then applies the order_by_clause to the siblings of the hierarchy

Thursday, June 24, 2010

Select Day count Excluding Saturday and Sunday for the Date range

SELECT
COUNT(DECODE(TO_CHAR(TO_DATE('Start Date','DD/MM/YYYY')+LEVEL-1,'D'),7,NULL,1,NULL,1)) FROM DUAL
CONNECT BY LEVEL <= TO_DATE('End Date','DD/MM/YYYY') -TO_DATE('Start Date','DD/MM/YYYY')+1;

Select Numbers without any Base Tables

SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 100