PL-SQL Queries
PL-SQL Queries
cursor c1 is
select employee_id,job_id,salary,department_id from employees;
begin
open c1;
loop
fetch c1 into e_id,j_id,sal,dept;
exit when c1%notfound;
dbms_output.put_line(e_id||' '||j_id||' '||sal||' '||dept);
end loop;
close c1;
end;
/
cursor c1 is
select first_name from employees;
begin
open c1;
loop
fetch c1 into e_name;
exit when c1%notfound;
dbms_output.put_line(msg||'-'||e_name);
end loop;
end;
end;
/
7) The following program uses a nested basic loop to find the prime numbers from 2
to 100 −
DECLARE
i number(3);
j number(3);
BEGIN
i := 2;
LOOP
j:= 2;
LOOP
exit WHEN ((mod(i, j) = 0) or (j = i));
j := j +1;
END LOOP;
IF (j = i ) THEN
dbms_output.put_line(i || ' is prime');
END IF;
i := i + 1;
exit WHEN i = 50;
END LOOP;
END;
/
8) example of nested for loop
DECLARE
i number(1);
j number(1);
BEGIN
FOR i IN 1..3 LOOP
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END loop;
END loop;
END;
/
9) example of if,elsif,else
DECLARE
a number(3) := 100;
BEGIN
IF ( a = 10 ) THEN
dbms_output.put_line('Value of a is 10' );
ELSIF ( a = 20 ) THEN
dbms_output.put_line('Value of a is 20' );
ELSIF ( a = 30 ) THEN
dbms_output.put_line('Value of a is 30' );
ELSE
dbms_output.put_line('None of the values is matching');
END IF;
dbms_output.put_line('Exact value of a is: '|| a );
END;
/
---PL/SQL Queries---
1. Float Variable
declare
v_var float := 2.3;
begin
dbms_output.put_line(v_var);
end;
cursor c1 is
select unit_code_rcv from cem_smx_su_t
where item_no = v_item_no;
begin
open c1;
loop
fetch c1 into v_unit_code_rcv;
exit when c1%notfound;
dbms_output.put_line(v_unit_code_rcv);
end loop;
exception
when others then
dbms_output.put_line('user exception');
end;
cursor c1 is
select item_no,unit_type_rcv,transaction_time from cem_smx_su_t;
begin
open c1;
loop
fetch c1 into
r_martix_supplier.v_item_no,r_martix_supplier.v_unit_type_rcv,r_martix_supplier.v_t
ransaction_time;
exit when c1%notfound;
dbms_output.put_line(r_martix_supplier.v_item_no||'--'||
r_martix_supplier.v_unit_type_rcv||'--'||
r_martix_supplier.v_transaction_time);
end loop;
exception
when others then
dbms_output.put_line('user exception');
end;
9. While loop
declare
v_counter number := 10;
begin
while v_counter > 1 loop
dbms_output.put_line(v_counter);
v_counter := v_counter - 1;
end loop;
end;
10. IF/ELSIF/ELSE [NOTE: While taking user input as string, use quotes('')
(Example : '&priority')
declare
ticket_priority varchar2(10) := '&priority';--the user input is case sensitive
--CRITICAL/HIGH/MEDIUM/LOW
begin
if ticket_priority = 'CRITICAL' OR ticket_priority = 'HIGH' then
dbms_output.put_line('Solve the incident right now');
elsif ticket_priority = 'MEDIUM' then
dbms_output.put_line('Solve the incident within 5 hours');
elsif ticket_priority = 'LOW' then
dbms_output.put_line('Solve the incident within 24 hours');
else
dbms_output.put_line('Cant recognize the priority');
end if;
end;
begin
select CUSTOMS_INVOICE_NO, INVOICE_ID into v_custom_inv_no,v_invoice_id from
customs_invoice_header_t
where CUSTOMS_INVOICE_NO = '101229671';
if SQL%FOUND then
dbms_output.put_line(SQL%ROWCOUNT);
end if;
end;
14. CURSOR%ROWCOUNT
declare
cursor c1 is
select CUSTOMS_INVOICE_NO, INVOICE_ID from customs_invoice_header_t;
cursor_var c1%rowtype;
begin
open c1;
loop
fetch c1 into cursor_var;
exit when c1%notfound;
dbms_output.put_line(cursor_var.CUSTOMS_INVOICE_NO);
dbms_output.put_line(cursor_var.INVOICE_ID);
dbms_output.put_line('-----------');
end loop;
close c1;
end;
22. Collections
--The data type of index can be either a string type or PLS_INTEGER.
set SERVEROUTPUT on;
declare
type user_arr is table of varchar2(108)
index by PLS_INTEGER;
emp_arr user_arr;
begin
Note: The above select query runs again and again for each loop decreasing the
performace.
This can be handled by BULK COLLECT
forall i in v_emp.first..v_emp.last
update employees set emp_sal = emp_sal+1000 where emp_id = v_emp(i);
commit;
end;
26. Creating an Accounts table and pushing the data into Transactions table
whenever there is an update over Accounts table
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------