Binu George on February 2nd, 2009

Execution of a cursor puts the results of the query into a set of rows called the result set, which can be fetched sequentially or non sequentially.
You can create Cursors which accepts parameters at runtime.

Read:Cursor | Oracle PL/SQL Cursors and example.
Read:ORA-01000: maximum open cursors exceeded.
Read:Oracle Cursors | OPEN ,FETCH and CLOSE Cursor statements.
Read:ORA-01001: invalid cursor

Example:

Step 1:

create table PERSON( PERSON_ID NUMBER(19) not null, AGE       NUMBER(10), FIRSTNAME VARCHAR2(255), LASTNAME  VARCHAR2(255));

SQL> insert into Person values(1,10,'Geek','Greek');

1 row inserted

SQL> insert into Person values(2,12,'Seek','Bells');

1 row inserted

SQL> insert into Person values(3,13,'Creek','Dells');

1 row inserted

SQL>SQL> insert into Person values(4,13,'Sreek','Sells');

1 row inserted

SQL>

Step 2:

Now write a small procedure to illustrate the cursor which accepts a parameter at run time.

create or replace procedure Param_Cursor as

CURSOR PERSON_CUR(pAge NUMBER) isSelect * from Person p where p.age = pAge;

BEGIN    for i in PERSON_CUR(13) loop       dbms_output.put_line(i.firstname);    end loop;

END Param_Cursor;

Step3:

Now run the procedure Param_Cursor.

SQL> set serveroutput on;SQL> exec Param_Cursor;CreekSreek

PL/SQL procedure successfully completed

SQL>

Technorati Tags:
, , , ,

 

Tags: , , , ,

Binu George on February 2nd, 2009

ORA-01001: invalid cursor error occurs when you tried to reference a cursor that does not yet exist.
A few scenarios given below.

1. FETCH cursor before opening the cursor.
2. CLOSE cursor before opening the cursor.
3. FETCH cursor after closing the cursor.

See the blow example:

When you write generic cursor you can either use FETCH..,OPEN… and CLOSE cursor statements Or you can use the FOR LOOP for iterating through the cursor. When you use FOR LOOP for iteration no need of Explicit use of FETCH..,OPEN… and CLOSE cursor statements.
The cursor will open automatically when entering FOR LOOP and will close the cursor once the loop ends.

Read:Cursor | Oracle PL/SQL Cursors and example.
Read:ORA-01000: maximum open cursors exceeded.
Read:Oracle Cursors | OPEN ,FETCH and CLOSE Cursor statements.

If you use CLOSE statement after the FOR LOOP Oracle will throw the error :
ORA-01001:Invalid Cursor

See the example below

create or replace procedure Param_Cursor as

CURSOR PERSON_CUR(pAge NUMBER) is
Select * from Person p where p.age = pAge;

BEGIN
    for i in PERSON_CUR(13) loop
       dbms_output.put_line(i.firstname);
    end loop;
 close PERSON_CUR;
END Param_Cursor;

Execute this:

SQL> exec Param_Cursor;

begin Param_Cursor; end;

ORA-01001: invalid cursor
ORA-06512: at "TEST.PARAM_CURSOR", line 10
ORA-06512: at line 1

SQL>

Technorati Tags:
, , , ,

 
Binu George on February 2nd, 2009

A cursor is a name for private SQL area.It is in private SQL area the parsed statement and other information for processing the statement are kept.
Execution of a cursor puts the results of the query into a set of rows called the result set, which can be fetched sequentially or non sequentially.

Read:Cursor | Oracle PL/SQL Cursors and example.
Read:ORA-01000: maximum open cursors exceeded.

When you write generic cursor you can either use FETCH..,OPEN… and CLOSE cursor statements as give below.


CREATE OR REPLACE PROCEDURE Generic_Cursor IS
<pre>CURSOR FETCH_INSERT ISSELECT * from PERSON;

new_Rec FETCH_INSERT%ROWTYPE;

BEGIN

 OPEN FETCH_INSERT;    LOOP      FETCH FETCH_INSERT INTO new_Rec;      EXIT when FETCH_INSERT%NOTFOUND;      DBMS_OUTPUT.put_line(new_Rec.FIRSTNAME);    END LOOP; CLOSE FETCH_INSERT;

END Generic_Cursor;

Or you can use the FOR LOOP for iterating through the cursor. When you use FOR LOOP for iteration no need of Explicit use of FETCH..,OPEN… and CLOSE cursor statements.
The cursor will open automatically when entering FOR LOOP and will close the cursor once the loop ends.

Example given below

<pre>CURSOR PERSON_CUR(pAge NUMBER) isSelect * from Person p where p.age = pAge;

BEGIN    for i in PERSON_CUR(13) loop       dbms_output.put_line(i.firstname);    end loop;

END Param_Cursor;

Also read:

Technorati Tags:
, , , ,

 
Binu George on February 2nd, 2009

Each user session can open multiple cursors up to the limit set by the initialization parameter OPEN_CURSORS.
If the number of open cursors exceeds this limit,oracle will throw the ‘ORA-01000: maximum open cursors exceeded‘ exception or error.

ORA-01000: maximum open cursors exceeded.

To make sure that the number of open cursors doesn’t exceeds the limit as specified in initialization parameter, close the cursors after the use.

Read: Cursor | Oracle PL/SQL Cursors and example.

For example when we use a Cursor object or resultset object,close it once the operation is over.
When we use JDBC programming ,this error is common. This is because ,the application developer is either missed of not closed the jdbc object used.The objects can be Statements,Result sets or even JDBC database connection objects.

Also Read:

Technorati Tags:
, , , ,

 
Binu George on February 2nd, 2009

A cursor is a name for private SQL area.It is in private SQL area where the parsed statement and other information for processing the statement are kept.
Execution of a cursor puts the results of the query into a set of rows called the result set, which can be fetched sequentially or nonsequentially.

A simple example of Cursor is given below.This uses a generic Cursor example,in which we OPEN the cursor , Fetch the records,do some operation then CLOSE the cursor.

Step1: Create a table PERSON and insert few records into it.

create table PERSON(PERSON_ID NUMBER(19) not null,AGE       NUMBER(10),FIRSTNAME VARCHAR2(255),LASTNAME  VARCHAR2(255));

SQL> insert into Person values(1,10,'Geek','Greek');

1 row inserted

SQL> insert into Person values(2,12,'Seek','Bells');

1 row inserted

SQL> insert into Person values(3,13,'Creek','Dells');

1 row inserted

SQL>SQL> insert into Person values(4,13,'Sreek','Sells');

1 row inserted

SQL&gt;

Step2: Create a Procedure for displaying the table data.

CREATE OR REPLACE PROCEDURE Generic_Cursor IS

CURSOR FETCH_INSERT ISSELECT * from PERSON;

new_Rec FETCH_INSERT%ROWTYPE;

BEGIN

 OPEN FETCH_INSERT;   LOOP     FETCH FETCH_INSERT INTO new_Rec;     EXIT when FETCH_INSERT%NOTFOUND;     DBMS_OUTPUT.put_line(new_Rec.FIRSTNAME);   END LOOP; CLOSE FETCH_INSERT;

END Generic_Cursor;

Step 3:Now test it using a small test program

SQL&gt; exec Param_Cursor;

begin Param_Cursor; end;

ORA-01001: invalid cursorORA-06512: at "TEST.PARAM_CURSOR", line 10ORA-06512: at line 1

SQL>

Also Read:

Technorati Tags:
, , , ,

 
Binu George on January 13th, 2009

We can use the ‘ALTER TABLE‘ statement in oracle to add columns to a table using ADD keyword.
It is fairly straight forward.
We can specify column details and constraints if any,
See the below example.

SQL> create table MYTABLE(name varchar2(100),age number);

Table created

Now let us use the ‘<span style=”font-weight: bold;”>ALTER TABLE</span>’ statement to add new columns to the table <span style=”font-weight: bold;”>MYTABLE</span>.

SQL> ALTER TABLE MYTABLE add(id number NOT NULL,address varchar2(100));

Table altered

SQL>

Related Articles,ALTER TABLE to ADD PRIMARY KEY in Oracle.

Also Read:

CREATE INDEX as part of CREATE TABLE statement.
Oracle Tables: Create Table as Select
Oracle/PLSQL: Create table with foreign key constraint
CREATE TABLE statement: create a table with composite primary key
Oracle PL/SQL:CREATE TABLE statement: create a table with primary key.
Oracle PL/SQL:Create Table
Oracle/PLSQL: Foreign Keys with ON DELETE CASCADE option

Technorati Tags:
, ,

 
Binu George on January 12th, 2009

CREATE INDEX statement is used to create Indexes using table columns.An index allows faster retrieval of records.It is mainly used as a performance-tuning method.An index creates an entry for each value that appears in the indexed columns.
You can create indexes explicitly using the SQL statement CREATE INDEX or as a part of CREATE TABLE script.

SYNTAX:

CREATE [UNIQUE] INDEX indexON table(column1,.. column_n)[ STORAGE CLAUSE ];

For example let us create a table MYTABLE;

SQL> CREATE TABLE MYTABLE (name varchar2(50),age number,id number);

Table created

Now create an unique index for MYTABLE using coulmns name and id.

SQL> CREATE UNIQUE INDEX MY_IDX ON MYTABLE(name,id);

Index created

SQL>

Now each row in the table will be uniquely indexed using the index MY_IDX.This will help faster retrieval of records from huge tables.
The above two steps can be combined into one step process as follows.

SQL> CREATE TABLE MYTABLE (name varchar2(50),age number,id number, CONSTRAINT my_Constraint unique(name,id) USING INDEX (CREATE UNIQUE INDEX  MY_IDX on MYTABLE(name,id)));

SQL> Table created

Related Articles,Oracle Tables: Create Table as Select
Oracle/PLSQL: Create table with foreign key constraint
CREATE TABLE statement: create a table with composite primary key
Oracle PL/SQL:CREATE TABLE statement: create a table with primary key.
Oracle PL/SQL:Create Table

Technorati Tags:
, , , ,

 
Binu George on January 9th, 2009

CREATE TABLE AS SELECT statement can be used when we need to extract part or full data from a table and store it into another table based on certain conditions .
We can create a table based on a query and insert the results of the query into the table
For example if we need the details of all employees whose salary is above certain limit and store it into another table,we can use CREATE TABLE AS SELECT statement.

The following example illustrates this.
Let us create an EMPLOYEE table which holds the employee information like name ,id and salary.

SQL> create table EMPLOYEE(name VARCHAR2(50),emp_id NUMBER,salary NUMBER(8,2));

Table created

Now insert some records in EMPLOYEE table.

SQL> insert into EMPLOYEE values('A',1,12345);insert into EMPLOYEE values('B',2,13345);insert into EMPLOYEE values('C',3,10345);insert into EMPLOYEE values('D',4,14345);
4 row inserted

SQL> select * from EMPLOYEE order by salary;

NAME  EMP_ID SALARY
C     3   10345.00 
A     1   12345.00 
B     2   13345.00
D     4   14345.00

Now let us create a table for employees whose salary is above 13000.

SQL> CREATE TABLE EMPLOYEE_MAX AS SELECT * from EMPLOYEE WHERE EMPLOYEE.salary>13000;
Table created

Now query the EMPLOYEE_MAX table.

SQL> select * from EMPLOYEE_MAX;

NAME EMP_ID SALARY
B    2   13345.00
D    4   14345.00

The new table has created with records based on the criteria in WHERE clause.

Also ReadOracle/PLSQL: Create table with foreign key constraint
CREATE TABLE statement: create a table with composite primary key
Oracle PL/SQL:CREATE TABLE statement: create a table with primary key.
Oracle PL/SQL:Create Table
Oracle/PLSQL: Foreign Keys with ON DELETE CASCADE option
ORA-02449: unique/primary keys in table referenced by foreign
ORA-02292: integrity constraint violated – child record found
ORA-02291: integrity constraint violated – parent key not Found
Oracle/PLSQL: Foreign Keys | Oracle Referential Integrity

Technorati Tags:

 
Binu George on January 9th, 2009

Usage Create table statement with foreign key constraint will create table with referential integrity.
Foreign Key holds the reference to another table column value.It is also known as references constraint.
A foreign key means that values in one table must also appear in another table.A references constraint is only applied at SQL ‘insert’ and ‘delete’ times.
A foreign key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.

Let us create an EMPLOYEE table which holds the employee information like name ,id and salary.
Here emp_id is the primary key column.

SQL> create table EMPLOYEE(name VARCHAR2(50),emp_id NUMBER,salary NUMBER(8,2), CONSTRAINT EMPLOYEE_ID_PK PRIMARY KEY (emp_id));

Table created

Now create an Employee address table which holds the address details of the employees.The address table is linked to EMPLOYEE table through the foreign key emp_id;

SQL> create table EMPLOYEE_ADDRESS(address varchar2(200),emp_id number,CONSTRAINT EMP_ADD_FK FOREIGN KEY (emp_id) REFERENCES EMPLOYEE(emp_id));

Table created

Now let us insert one record in EMPLOYEE table.

SQL> insert into EMPLOYEE values('ABCD',1,12345);

1 row inserted

Now let us add address for this particular employee in EMPLOYEE_ADDRESS table.

SQL> insert into EMPLOYEE_ADDRESS values('abcd efgh',1);

1 row inserted

Now while entering the data in EMPLOYEE_ADDRESS table the data should be already present in EMPLOYEE table.Otherwise foreign key violation will result.

SQL> insert into EMPLOYEE_ADDRESS values('abcd efgh',8);
ORA-02291: integrity constraint (TEST.EMP_ADD_FK) violated - parent key not found

ORA-02291: integrity constraint (TEST.EMP_ADD_FK) violated – parent key not found

Also if we delete a row from the EMPLOYEE table and if a reference exists in EMPLOYEE_ADDRESS table oracle will throw an error showing reference integrity dependency.

SQL> delete from EMPLOYEE where emp_id =1;
ORA-02292: integrity constraint (TEST.EMP_ADD_FK) violated - child record found

ORA-02292: integrity constraint (TEST.EMP_ADD_FK) violated – child record found
Also Read,CREATE TABLE statement: create a table with composite primary key

Oracle PL/SQL:CREATE TABLE statement: create a table with primary key.
Oracle PL/SQL:Create Table
Oracle/PLSQL: Foreign Keys with ON DELETE CASCADE option
ORA-02449: unique/primary keys in table referenced by foreign keys
ORA-02292: integrity constraint violated – child record found
ORA-02291: integrity constraint violated – parent key not Found
Oracle/PLSQL: Foreign Keys | Oracle Referential Integrity
Oracle/PLSQL: Composite Primary Key
Oracle/PLSQL: Primary Key and Composite Primary Key
Technorati Tags:
, ,

 
Binu George on January 9th, 2009

CREATE TABLE statement can be used to create table objects in database. It is possible to add constraints like primary key ,foreign key while table creation.Primary key is the unique identifier for a row of data.One table cannot contain duplicate primary key values.Primary key also can be a combination of columns (COMPOSITE Primary Key).

The below given example uses CREATE TABLE statement to create a table with a single column primary key.

SQL> create table MYTABLE(
name VARCHAR2(50),
id NUMBER,
salary NUMBER(8,2),
CONSTRAINT MYTABLE_ID PRIMARY KEY (id)
);

Table created

Now let us INSERT few records into MYTABLE.

SQL> insert into MYTABLE values ('CCC',1,2548.21);
SQL> insert into MYTABLE values ('ADS',2,3548.21);
SQL> insert into MYTABLE values ('GDS',2,1548.21);

SQL> select * from MYTABLE ORDER BY SALARY;

NAME   ID  SALARY

GDS   2    1548.21
CCC   1    2548.21
ADS   2    3548.21

The below given example uses CREATE TABLE statement to create a table with a multiple column primary key (COMPOSITE Primary KEY).

SQL> drop table mytable;
Table dropped

SQL> create table MYTABLE(
name VARCHAR2(50),
id NUMBER,
salary NUMBER(8,2),
CONSTRAINT MYTABLE_NAME_ID_PK PRIMARY KEY (name,id)
);

Table created

Now let us INSERT few records into MYTABLE.

SQL> insert into MYTABLE values ('CCC',1,2548.21);
SQL> insert into MYTABLE values ('ADS',2,3548.21);
SQL> insert into MYTABLE values ('GDS',2,1548.21);

SQL> select * from MYTABLE ORDER BY SALARY;

NAME   ID  SALARY
GDS   2    1548.21
CCC   1    2548.21
ADS   2    3548.21

>Also Read,

Oracle PL/SQL:CREATE TABLE statement: create a table with primary key.
Oracle PL/SQL:Create Table
Oracle/PLSQL: Foreign Keys with ON DELETE CASCADE option
ORA-02449: unique/primary keys in table referenced by foreign keys
ORA-02292: integrity constraint violated – child record found
ORA-02291: integrity constraint violated – parent key not Found
Oracle/PLSQL: Foreign Keys | Oracle Referential Integrity
Oracle/PLSQL: Composite Primary Key
Oracle/PLSQL: Primary Key and Composite Primary Key

Technorati Tags:
, ,

 
SEO Powered by Platinum SEO from Techblissonline