0% found this document useful (0 votes)
5 views6 pages

Cursors

The document explains the concept of cursors in databases, which serve as temporary memory for DML operations. It details two types of cursors: Implicit Cursors, automatically allocated by SQL Server during DML operations, and Explicit Cursors, created by users for row-by-row data fetching. The document provides examples of creating and manipulating tables using both types of cursors.

Uploaded by

23211a3234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Cursors

The document explains the concept of cursors in databases, which serve as temporary memory for DML operations. It details two types of cursors: Implicit Cursors, automatically allocated by SQL Server during DML operations, and Explicit Cursors, created by users for row-by-row data fetching. The document provides examples of creating and manipulating tables using both types of cursors.

Uploaded by

23211a3234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Cursors

Cursor is a Temporary Memory or Temporary Work Station. It is Allocated


by Database Server at the Time of Performing DML(Data Manipulation
Language) operations on the Table by the User. Cursors are used to store
Database Tables.
There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors. These are
explained as following below.
1. Implicit Cursors: Implicit Cursors are also known as Default Cursors of SQL
SERVER. These Cursors are allocated by SQL SERVER when the user
performs DML operations.
2. Explicit Cursors: Explicit Cursors are Created by Users whenever the user
requires them. Explicit Cursors are used for Fetching data from Table in Row-
By-Row Manner.

Implicit Cursors:
SQL> CREATE TABLE Emp(
2 EmpID INT PRIMARY KEY,
3 Name VARCHAR(50),
4 Country VARCHAR(50),
5 Age int(2),
6 Salary int(10)
7 );
Age int(2),
*
ERROR at line 5:
ORA-00907: missing right parenthesis

SQL> CREATE TABLE Emp(EMPID int primary key,name


varchar(10),country varchar(10),age int,salary int);

Table created.

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 VALUES (1, 'Shubham', 'India','23','30000');
1 row created.

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 VALUES(2, 'Aman ', 'Australia','21','45000'),
3 (3, 'Naveen', 'Sri lanka','24','40000'),
4 (4, 'Aditya', 'Austria','21','35000'),
5 (5, 'Nishant', 'Spain','22','25000');
VALUES(2, 'Aman ', 'Australia','21','45000'),
*
ERROR at line 2:
ORA-00933: SQL command not properly ended

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 values(2, 'Aman ', 'Australia','21','45000');

1 row created.

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 values (3, 'Naveen', 'Sri lanka','24','40000');

1 row created.

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 values(4, 'Aditya', 'Austria','21','35000');

1 row created.

SQL> INSERT INTO Emp (EmpID, Name,Country, Age, Salary)


2 values(5, 'Nishant', 'Spain','22','25000');
1 row created.

SQL> select * from emp;

EMPID NAME COUNTRY AGE SALARY


---------- ---------- ---------- ---------- ----------
1 Shubham India 23 30000
2 Aman Australia 21 45000
3 Naveen Sri lanka 24 40000
4 Aditya Austria 21 35000
5 Nishant Spain 22 25000

SQL> set serveroutput on;


SQL> DECLARE
2 total_rows number;
3 BEGIN
4 UPDATE Emp
5 SET Salary = Salary + 1500;
7 total_rows := SQL%ROWCOUNT;
9 dbms_output.put_line(total_rows || ' rows updated.');
10 END;
11 /
5 rows updated.

PL/SQL procedure successfully completed.

SQL> select * from emp;

EMPID NAME COUNTRY AGE SALARY


---------- ---------- ---------- ---------- ----------
1 Shubham India 23 31500
2 Aman Australia 21 46500
3 Naveen Sri lanka 24 41500
4 Aditya Austria 21 36500
5 Nishant Spain 22 26500

Example 2:
In the same way for customer table
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+

DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2500.00 |
| 2 | Khilan | 25 | Delhi | 2000.00 |
| 3 | kaushik | 23 | Kota | 2500.00 |
| 4 | Chaitali | 25 | Mumbai | 7000.00 |
| 5 | Hardik | 27 | Bhopal | 9000.00 |
| 6 | Komal | 22 | MP | 5000.00 |
+----+----------+-----+-----------+----------+
Explicit cursors:

Create Customer table in the same way or use above customer table.

Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+

DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/
When the above code is executed at the SQL prompt, it produces the following
result −
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP

PL/SQL procedure successfully completed.

You might also like