CREATE TABLE customer_11 (
customer_id NUMBER(10) PRIMARY KEY, -- Unique customer identifier
customer_name VARCHAR2(50) NOT NULL, -- Name of the customer
email VARCHAR2(100), -- Email address
phone_number VARCHAR2(15) -- Contact phone number
);
commit;
INSERT INTO customer_11 (customer_id, customer_name, email,
phone_number)
VALUES (1, 'John Doe', '[email protected]', '+1234567890');
INSERT INTO customer_11 (customer_id, customer_name, email,
phone_number)
VALUES (2, 'Jane Smith', '[email protected]', '+9876543210');
declare
lv_name varchar2(10);
begin
select customer_name into lv_name from customer_11 where
customer_id = 2;
dbms_output.put_line(lv_name);
end;
Alter table customer_11 modify phone_number number(100);
Insert into customer_12(select * from customer_11);
%type When you want a variable to have the same type as a
database column.
It’s a same datatype present in table and that datatype I want use
that time use it %type
-- %type and %rowtype
DECLARE
lv_name customer_11.customer_name%TYPE; -- Declares a variable
with the same type as emp.empno
BEGIN
lv_name := 'Jane Smith'; -- Assigns a value to the variable
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || lv_name);
END;
/
Type means only one column takan from database
Rowtype all the columns
( When you want to handle all columns of a table row in a single
variable.)
DECLARE
lv_record customer_11%ROWTYPE; -- Declares a record for all columns
of the emp table
BEGIN
SELECT * INTO lv_record
FROM customer_11
WHERE CUSTOMER_ID = 2;
-- Accessing individual columns of the record
DBMS_OUTPUT.PUT_LINE('CUSTOMER Email: ' || lv_record.email);
DBMS_OUTPUT.PUT_LINE('CUSTOMER Phone: ' ||
lv_record.phone_number);
END;
Control statements:
1. If statement
a. IF-THEN
b. IF-THEN-ELSE
c. IF-THEN-ELSIF
IF-THEN: Used for a single condition check.
Syntax:
IF condition THEN
-- Code to execute if condition is true
END IF;
/
Example :
BEGIN
IF 5 > 3 THEN
DBMS_OUTPUT.PUT_LINE('5 is greater than 3');
END IF;
END;
/
IF-THEN-ELSE: Used to handle one true and one false condition.
Syntax
IF condition THEN
-- Code to execute if condition is true
ELSE
-- Code to execute if condition is false
END IF;
Example :
BEGIN
IF 10 > 20 THEN
DBMS_OUTPUT.PUT_LINE('10 is greater than 20');
ELSE
DBMS_OUTPUT.PUT_LINE('10 is not greater than 20');
END IF;
END;
IF-THEN-ELSIF: Allows multiple condition checks in sequence.
Syntax:
IF condition1 THEN
-- Code to execute if condition1 is true
ELSIF condition2 THEN
-- Code to execute if condition2 is true
ELSE
-- Code to execute if none of the conditions are true
END IF;
Example :
BEGIN
IF 10 > 20 THEN
DBMS_OUTPUT.PUT_LINE('10 is greater than 20');
ELSIF 10 = 10 THEN
DBMS_OUTPUT.PUT_LINE('10 is equal to 10');
ELSE
DBMS_OUTPUT.PUT_LINE('No conditions are true');
END IF;
END;
/
2. Case Statement
Syntax :
CASE
WHEN condition1 THEN
-- Code to execute if condition1 is true
WHEN condition2 THEN
-- Code to execute if condition2 is true
ELSE
-- Code to execute if no conditions are true
END CASE;
Example :
BEGIN
CASE
WHEN 10 > 20 THEN
DBMS_OUTPUT.PUT_LINE('10 is greater than 20');
WHEN 10 = 10 THEN
DBMS_OUTPUT.PUT_LINE('10 is equal to 10');
ELSE
DBMS_OUTPUT.PUT_LINE('No conditions are true');
END CASE;
END;
/
Simple LOOP: Useful for infinite loops or when termination conditions are
complex.
WHILE LOOP: Best for conditions where the number of iterations isn't
fixed in advance.
FOR LOOP: Ideal for iterating over a fixed range of values.
Nested Loops: Helpful for processing hierarchical data or performing
operations on multiple levels.
Simple Loop :
Syntax
LOOP
-- Code to execute
EXIT WHEN condition;
END LOOP;
Example :
DECLARE
i NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Value of i: ' || i);
i := i + 1;
EXIT WHEN i > 5; -- Exit the loop when i exceeds 5
END LOOP;
END;
While Loop –
Syntax :
WHILE condition LOOP
-- Code to execute
END LOOP;
Example :
DECLARE
i NUMBER := 1;
BEGIN
WHILE i <= 5 LOOP
DBMS_OUTPUT.PUT_LINE('Value of i: ' || i);
i := i + 1;
END LOOP;
END;
For Loop –
Syntax:
FOR counter_variable IN start_value..end_value LOOP
-- Code to execute
END LOOP;
Example:
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('Value of i: ' || i);
END LOOP;
END;
The cursor is defined as a private work area where the SQL statement
( Select & DML ) is executed.
1. Implicit Cursor
2. Explicit Cursor
Implicit Cursor
Automatically created by Oracle whenever a DML operation (INSERT,
UPDATE, DELETE) or a SELECT statement (returning a single row) is
executed.
Explicit Cursor
Defined and managed explicitly by the programmer for SELECT
statements that return multiple rows.
Declare the cursor.
Open the cursor.
Fetch rows from the cursor.
Close the cursor.
Exception Example demo : Lv_name scalar variable
If I want more than one rows
We have 2 ways
1. Composite Variable
CLEAR SCREEN
declare
lv_phone varchar2(20);
lv_name varchar2(10);
begin
select phone_number , CUSTOMER_NAME into lv_phone, lv_name from
customer_11; --WHERE customer_id =1;
dbms_output.put_line(lv_phone||' - '||lv_name );
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('NO DATA FOUND');
WHEN too_many_rows THEN
DBMS_OUTPUT.PUT_LINE('TO MANY ROWS');
end.
If we fetch the record more than 1 row from the be table – Cursor (It’s a
looping statement )
BIND variable - &
Isopen return boolean value – yes or no
Found return boolean value – yes or no
Notfound return boolean value – yes or no ( true or false )
Rowcount its returns how many records
ROWCOUNT
DELETE FROM employees WHERE department_id = 10;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows deleted.');
begin
update customer_11 set phone_number ='+9191464236' where
CUSTOMER_ID = &CUSTOMER_ID;
if sql%notfound then
DBMS_OUTPUT.PUT_LINE('now rows updated');
else
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ||' - ROWS UPADTED');
END IF;
end;
FOUND
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 20;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE('Salaries updated.');
END IF;
NOTFOUND
UPDATE employees SET salary = salary * 1.1 WHERE department_id =
999;
IF SQL%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('No rows updated.');
END IF;
set SERVEROUTPUT on;
declare
lv_custname customer_11.CUSTOMER_NAME%type;
CURSOR cur1 is select CUSTOMER_NAME from customer_11;
begin
open cur1;
fetch cur1 into lv_custname ;
dbms_output.put_line('CUSTOMER_NAME - '||lv_custname);
fetch cur1 into lv_custname ;
dbms_output.put_line('CUSTOMER_NAME - '||lv_custname);
fetch cur1 into lv_custname ;
dbms_output.put_line('CUSTOMER_NAME - '||lv_custname);
close cur1;
end;
-- all records
Select * from xxv_employee;
declare
lv_jobName xxv_employee.job_title%type;
cursor cursor_employee is select job_title from xxv_employee;
begin
open cursor_employee;
loop
fetch cursor_employee into lv_jobName;
exit when cursor_employee%notfound;
dbms_output.put_line(lv_jobName);
end loop;
dbms_output.put_line('Total number of records --> '||cursor_employee
%rowcount);
close cursor_employee;
end;
/
Two table – Info
-- Declare a variable to hold the department name fetched from the
cursor.
DECLARE
lv_deptname xxv_department.department_name%TYPE;
-- Define a cursor `all_info` to fetch distinct department names by joining
the `xxv_employee`
-- and `xxv_department` tables.
CURSOR all_info IS
SELECT DISTINCT DEPARTMENT_NAME
FROM xxv_employee emp, xxv_department dept
WHERE emp.department_id = dept.department_id;
-- The cursor retrieves distinct department names by matching
department IDs between the `xxv_employee`
select distinct
-- and `xxv_department` tables.
DEPARTMENT_NAME
from
BEGIN
xxv_employee emp,
-- Open the cursor to start fetching data.
xxv_department dept
OPEN all_info;
where
emp.department_id =
dept.department_id;
-- Loop to process each record fetched by the cursor.
LOOP
-- Fetch the current record into the variable `lv_deptname`.
FETCH all_info INTO lv_deptname;
-- Exit the loop when there are no more records to fetch.
EXIT WHEN all_info%NOTFOUND;
-- Output the fetched department name to the console.
DBMS_OUTPUT.PUT_LINE(lv_deptname);
END LOOP;
-- Close the cursor after all records have been processed.
CLOSE all_info;
END;
/ The BULK COLLECT keyword tells
Oracle to fetch all the rows at once
(instead of fetching them one-by-
one) and store the result into the
collection.
New page
DECLARE
-- Declare a collection type 'EMAIL_TYPE' which is a table of
VARCHAR2(50)
-- to store email addresses (maximum 50 characters per email).
TYPE EMAIL_TYPE IS TABLE OF VARCHAR2(50);
-- Declare a variable 'LV_EMAIL' of type 'EMAIL_TYPE' and
initialize it as an empty collection.
LV_EMAIL EMAIL_TYPE := EMAIL_TYPE();
BEGIN
-- Fetch all email addresses from the 'customer_11' table into
the 'LV_EMAIL' collection.
-- The BULK COLLECT operator allows fetching multiple rows at
once, which will be stored in 'LV_EMAIL'.
SELECT EMAIL
BULK COLLECT INTO LV_EMAIL
FROM customer_11;
-- Loop through the 'LV_EMAIL' collection from the first element
to the last element.
-- LV_EMAIL.FIRST returns the first index (usually 1), and
LV_EMAIL.LAST returns the last index.
FOR i IN LV_EMAIL.FIRST..LV_EMAIL.LAST LOOP
-- Print the email address at the current index 'i' from the
collection 'LV_EMAIL'.
DBMS_OUTPUT.PUT_LINE(LV_EMAIL(i)); -- Corrected the typo
from 'LV_MAIL' to 'LV_EMAIL'
END LOOP;
END;
/
For Loop
-- for loop
declare
cursor dept_cursor is select department_name , location from
xxv_department;
begin
for rec in dept_cursor -- rec composite data type
loop
dbms_output.put_line(rec.department_name||' - '||rec.location);
end loop;
end;