plsql - 1
plsql - 1
);
commit;
INSERT INTO customer_11 (customer_id, customer_name, email,
phone_number)
declare
lv_name varchar2(10);
begin
dbms_output.put_line(lv_name);
end;
It’s a same datatype present in table and that datatype I want use
that time use it %type
DECLARE
BEGIN
END;
/
DECLARE
BEGIN
FROM customer_11
WHERE CUSTOMER_ID = 2;
END;
Control statements:
1. If statement
a. IF-THEN
b. IF-THEN-ELSE
c. IF-THEN-ELSIF
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;
/
Syntax
IF condition THEN
ELSE
END IF;
Example :
BEGIN
IF 10 > 20 THEN
ELSE
END IF;
END;
Syntax:
IF condition1 THEN
ELSE
END IF;
Example :
BEGIN
IF 10 > 20 THEN
ELSIF 10 = 10 THEN
ELSE
END IF;
END;
/
2. Case Statement
Syntax :
CASE
ELSE
END CASE;
Example :
BEGIN
CASE
WHEN 10 = 10 THEN
ELSE
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.
Simple Loop :
Syntax
LOOP
-- Code to execute
END LOOP;
Example :
DECLARE
i NUMBER := 1;
BEGIN
LOOP
i := i + 1;
END LOOP;
END;
While Loop –
Syntax :
-- Code to execute
END LOOP;
Example :
DECLARE
i NUMBER := 1;
BEGIN
i := i + 1;
END LOOP;
END;
For Loop –
Syntax:
-- Code to execute
END LOOP;
Example:
BEGIN
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
Explicit Cursor
We have 2 ways
1. Composite Variable
CLEAR SCREEN
declare
lv_phone varchar2(20);
lv_name varchar2(10);
begin
dbms_output.put_line(lv_phone||' - '||lv_name );
EXCEPTION
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
ROWCOUNT
begin
if sql%notfound then
else
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
IF SQL%NOTFOUND THEN
END IF;
set SERVEROUTPUT on;
declare
lv_custname customer_11.CUSTOMER_NAME%type;
begin
open cur1;
dbms_output.put_line('CUSTOMER_NAME - '||lv_custname);
dbms_output.put_line('CUSTOMER_NAME - '||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;
begin
open cursor_employee;
loop
dbms_output.put_line(lv_jobName);
end loop;
close cursor_employee;
end;
/
Two table – Info
DECLARE
lv_deptname xxv_department.department_name%TYPE;
CURSOR all_info IS
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
DBMS_OUTPUT.PUT_LINE(lv_deptname);
END LOOP;
CLOSE all_info;
END;
DECLARE
BEGIN
SELECT EMAIL
FROM customer_11;
-- Print the email address at the current index 'i' from the
collection 'LV_EMAIL'.
END LOOP;
END;
/
For Loop
-- for loop
declare
begin
loop
dbms_output.put_line(rec.department_name||' - '||rec.location);
end loop;
end;