PLSQL Queries Examples
PLSQL Queries Examples
PLSQL BASICS
posted Apr 29, 2012, 4:29 PM by srinivas aithagani [ updated Apr 29, 2012, 6:07 PM ]
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE('HELLO WORLD');
3 END;
4 /
HELLO WORLD
Procedure created.
1 DECLARE
2 V_EMPNO EMP.EMPNO%TYPE:=&ENTEREMPNO;
3 V_ENAME EMP.ENAME%TYPE;
4 V_SAL EMP.SAL%TYPE;
5 V_DEPTNO EMP.DEPTNO%TYPE;
6 V_COMM EMP.COMM%TYPE;
7 BEGIN
8 SELECT ENAME,SAL,DEPTNO,COMM INTO V_ENAME,V_SAL,V_DEPTNO,V_COMM
9 FROM EMP WHERE EMPNO=V_EMPNO;
10 DBMS_OUTPUT.PUT_LINE(V_ENAME||' '||V_SAL||' '||V_DEPTNO||' '||V_COMM);
11* END;
SQL> /
Enter value for enterempno: 7839
KING 5000 10
SQL> /
Enter value for enterempno: 7566
JONES 2975 20
SQL> /
Enter value for enterempno: 7839
ENAME : KING
SAL : 5000
JOB : PRESIDENT
SQL> /
Enter value for enterempno: 7902
ENAME : FORD
SAL : 3000
JOB : ANALYST
PL/SQL procedure successfully completed.
Procedure created.
SQL> ED
Wrote file afiedt.buf
1 DECLARE
2 V_EMPNO EMP.EMPNO%TYPE:=&ENTEREMPNO;
3 TYPE EMPRECORDTYPE IS RECORD
4 (
5 V_ENAME EMP.ENAME%TYPE,
6 V_DEPTNO EMP.DEPTNO%TYPE,
7 V_JOB EMP.JOB%TYPE
8 );
9 EMPRECORD EMPRECORDTYPE;
10 BEGIN
11 SELECT ENAME,DEPTNO,JOB
12 INTO
13 EMPRECORD.V_ENAME,EMPRECORD.V_DEPTNO,EMPRECORD.V_JOB
14 FROM EMP
15 WHERE EMPNO=V_EMPNO;
16 PRINT('----THE DETAILS YOU ENTERED ARE--');
17 PRINT('ENAME'||' '||EMPRECORD.V_ENAME);
18 PRINT('DEPTNO'||' '||EMPRECORD.V_DEPTNO);
19 PRINT('JOB'||' '||EMPRECORD.V_JOB);
20* END;
SQL> /
Enter value for enterempno: 7566
----THE DETAILS YOU ENTERED ARE--
ENAME JONES
DEPTNO 20
JOB MANAGER
i number;
begin
for i in 1..50
loop
if mod(i,2)<>0 then
end if;
end loop;
begin
:g_monthly_salary:=&p_annual_salary/12;
end;
(OR Method 2)
declare
v_sal number:=&p_annual_salary;
begin
:g_monthly_salary:=v_sal/12;
end;
COMPUTE A 10% BONUS FOR THE EMPLOYEE WITH THE EMPLOYEE_ID 7559 AND
ASSIGN THE COMPUTED VALUE TO THE V_BONUS VARIABLE BY USING THE PL/SQL
BLOCK
1. declare
begin
begin
empno=7369;
end;
begin
end;
begin
:g_monthly_salary:=&p_annual_salary/12;
end;
(OR Method 2)
declare
v_sal number:=&p_annual_salary;
begin
:g_monthly_salary:=v_sal/12;
end;
HOW TO COMPUTE AND PRINT THE MONTHLY SALARY ON TO THE SCREEN USING THE
ORACLE SUPPLIED PACKAGE PROCEDURE BASED UPON THE ANNUAL SALARY
SUPPLIED BY THE USER
1. SQL> define p_annual_salary=50000;
declare
begin
g_monthly_salary:=v_sal/12;
end;
HOW TO CREATE AN ANONYMOUS BLOCK TO OUTPUT THE PHRASE "MY PL/SQL BLOCK
WORKS" TO THE SCREEN
1. SQL> variable g_message varchar2(30)
begin
end;
HOW TO CREATE A BLOCK THAT DECLARES TWO VARIABLES. ASSIGN THE VALUE OF
THESE ISQL*PLUS HOST VARIABLES AND PRINT THE RESULTS OF THE PL/SQL
VARIABLES TO THE SCREEN
1. SQL> variable g_char varchar2(30)
begin
:g_num:=42;
end;
HOW TO RETURN THE SUM OF THE SALARIES FOR ALL EMPLOYEES IN THE SPECIFIED
DEPARTMENT
1. declare
2. v_deptno number:=30;
3.
4. v_sum_sal number;
5.
6. begin
7.
8. select deptno,sum(sal) into v_deptno,v_sum_sal from emp_dup
9.
10. where deptno=v_deptno group by deptno;
11.
12. dbms_output.put_line('THE SUM OF SALARY OF DEPTNO30 WAS'||
v_sum_sal);
13.
14. end;
HOW TO UPDATE THE EMPLOYEE SALARY WITH INCREASE OF 5000 THAN EARLIER
AMOUNT AND WITH JOB AS MANAGER
1. declare
2. p emp_dup%rowtype;
3.
4. begin
5.
6. select * into p from emp_dup where job='manager';
7.
8. update emp_dup set sal=p.sal+5000 where job='manager';
9.
10. end;
HOW TO DELETE ROWS FOR THE DEPARTMENT 20 AND PRINT THE NUMBER OF ROWS
DELETED USING SQL CURSOR ATTRIBUTES
SQL> variable rows_deleted varchar2(30)
1. begin
2. delete from emp_dup where deptno=20;
3.
4. :rows_deleted:=(sql%rowcount||' '||'rows_deleted');
5.
6. end;
SQL> print max_deptno;
HOW TO INSERT INTO A NEW DEPARTMENT INTO THE TABLE ****A) USING THE DEFINE
COMMAND PROVIDE THE DEPARTMENT NAME AS NEW_DEPARTMENT ****B)USING THE
ISQL*PLUS SUBSTITUTION VARIABLE PASS THE LOCATION
SQL> define department_name=’EDUCATION’;
1. declare
2. v_deptno number:=50;
3.
4. v_dname varchar2(30):='&department_name';
5.
6. begin
7.
8. insert into dept(deptno,dname) values(v_deptno,v_dname);
9.
end;
10.
SQL> select * from dept;
HOW TO CREATE A PL/SQL BLOCK THAT DELETES THE DEPARTMENT NUMBER 20 ****A)
USING THE DEFINE COMMAND PROVIDE THE DEPARTMENT NUMBER. ****B) USING THE
ISQL*PLUS SUBSTITUTION VARIABLE PASS THE VALUE TO PL/SQL BLOCK. ****C) PRINT
TO THE SCREEN NUMBER OF ROWS AFFECTED.
SQL> define p_deptno=50;
SQL> variable g_result varchar2(30)
1. declare
2. v_deptno number:=&p_deptno;
3.
4. result varchar2(30);
5.
6. begin
7.
8. delete from dept where deptno=v_deptno;
9.
10. result:=(sql%rowcount||(row(s) deleted');
11.
12. :g_result:=result;
13.
end;
14.
SQL> print g_result;
SQL> select * from dept;
HOW TO CREATE A PL/SQL BLOCK SUCH THAT IF THE EMPLOYEE NAME WAS 'KEVIN'
THEN SET THE MANAGER_ID TO 150
1. declare
2. p emp_dup%rowtype;
3.
4. begin
5.
6. select * into p from emp_dup where ename='KEVIN';
7.
8. if p.ename ='KEVIN' then
9.
10. p.mgr:=150;
11.
12. update emp_dup set mgr=p.mgr where ename='KEVIN';
13.
14. end if;
15.
end;
16.
SQL> select * from emp_dup where ename=’KEVIN’;
r emp_dup%rowtype;
begin
if r.ename='KEVIN' then
r.job:='SA_CSR';
r.deptno:=100;
end if;
end;
SQL> select * from emp_dup where ename=’KEVIN’;
HOW TO CREATE A PL/SQL BLOCK THAT WILL SET THE DEPT_NO TO 100 FOR
EMPLOYEE NAMED 'KEVIN' AND ALSO THE EMPLOYEE SALARY IS LESS THAN 10000
1. declare
p emp_dup%rowtype;
begin
p.deptno:=100;
end;
SQL> select * from emp_dup where ename=’KEVIN’;
HOW TO SET THE MGR_ID TO 100 FOR EMPLOYEE NAMED "KEVIN" IF THE DEPTNO WAS
100 OR THE HIREDATE IS GRATER THAN THE '01-JAN-2000'
1. declare
p emp_dup%rowtype;
begin
p.mgr:=100;
end if;
end;
SQL> select * from emp_dup where ename=’KEVIN’;
begin
value_appraisal :='EXCELLENT';
elsif value_grade='B' then
value_appraisal :='GOOD';
value_appraisal :='BAD';
end if;
end;
HOW TO CREATE A PL/SQL BLOCK THAT WILL INSERT NUMBERS INTO MESSAGE
TABLE. A) INSERT THE NUMBERS 1 TO 20,EXCLUDING 16 AND 18 B) COMMIT BEFORE
THE END OF THE BLOCK C) SELECT THE MESSAGES TABLE TO VERIFY THAT YOUR
PL/SQL BLOCK WORKED
1. begin
loop
end loop;
for i in 19..20
loop
end loop;
commit;
end;
SQL> select * from messages;
HOW TO RETRIEVE THE FIRST 10 RECORDS OF THE EMP_CSR TABLE BY USING THE
PL/SQL CURSORS
1. declare
r emp_cursor%rowtype;
begin
loop
end loop;
close emp_cursor;
end;
HOW TO RETRIEVE THE DETAILS OF THE EMPLOYEES WHOSE DEPARTMENT WAS 100
USING THE CURSORS
1. declare
r emp_dup%rowtype;
begin
loop
end loop;
close dept_cursor;
end;
1. declare
r emp_20%rowtype;
begin
loop
close emp_20 ;
end;
HOW TO RETRIEVE THE EMPLOYEE NUMBER AND NAME FROM THE EMPLOYEE TABLE
AND INSERT INTO TEMP_LIST TABLE BY USING CURORS AND RECORDS
1. declare
emp_record temp_insert%rowtype;
begin
loop
end loop;
close temp_insert;
end;
1. declare
2. v_deptid number;
3.
4. v_loc varchar2(15):='NRT';
5.
6. v_dname varchar2(15):='CREDIT';
7.
8. v_counter number:=1;
9.
10. begin
11.
12. select max(deptno) into v_deptid from dept;
13.
14. loop
15.
16. insert into dept(deptno,dname,loc)
values((v_deptid+v_counter),v_dname,v_loc);
17.
18. v_counter:=v_counter+1;
19.
20. exit when v_counter>3;
21.
22. end loop;
23.
24. end;
1. declare
2. v_counter number:=1;
3.
4. v_ename varchar2(15):='GVREDDY';
5.
6. v_job varchar2(15):='SRINU';
7.
8. v_empno number;
9.
10. begin
11.
12. select max(empno) into v_empno from emp_dup;
13.
14. while v_counter<=3 loop
15.
16. insert into emp_dup(empno,ename,job) values((v_empno+v_counter),
17.
18. v_ename,v_job);
19.
20. v_counter:=v_counter+1;
21.
22. end loop;
23.
24. end;
SQL> SELECT * FROM EMP_DUP;
1. declare
2. v_counter number:=1;
3.
4. v_ename varchar2(15):='GVREDDY';
5.
6. v_job varchar2(15):='SRINU';
7.
8. v_empno number;
9.
10. begin
11.
12. select max(empno) into v_empno from emp_dup;
13.
14. while v_counter<=3 loop
15.
16. insert into emp_dup(empno,ename,job) values((v_empno+v_counter),
17.
18. v_ename,v_job);
19.
20. v_counter:=v_counter+1;
21.
22. end loop;
23.
end;
24.
SQL> SELECT * FROM EMP_DUP;
What are the Advantages of using PLSQL over
Traditional SQL
PLSQL has support for SQL and supports Object-Oriented Programming. It’s integration with
Oracle provides better performance, portability and higher productivity.
PLSQL Supports the declaration and manipulation of object types and collections.
PLSQL Allows the calling of external functions and procedures.
PLSQL Contains new libraries of built in packages.
With PL/SQL , an multiple sql statements can be processed in a single command
line statement.
Developers and Database Administrators use PLSQL coding on a daily basis, whether for
application development, finding problems, fine-tuning solutions to those problems, or other
critical Database Administration tasks. Our aim here is to provide answers to PLSQL questions
that Database Administrators come across daily.
HOW TO RETRIEVE THE FIRST 10 RECORDS OF THE EMP_DUP TABLE BY USING THE
CURSORS
1. declare
r emp_cursor%rowtype;
begin
loop
close emp_cursor;
end;
1. declare
r emp_dup%rowtype;
begin
loop
end loop;
close dept_cursor;
end;
HOW TO RETRIEVE THE FIRST 20 RECORD OF THE EMPLOYEE TABLE USING CURSORS
1. declare
2. cursor emp_20 is select empno,ename from emp_dup;
3.
4. r emp_20%rowtype;
5.
6. begin
7.
8. open emp_20;
9.
10. loop
11.
12. fetch emp_20 into r;
13.
14. dbms_output.put_line('THE EMPLOYEE DETAILS WAS'||r.empno||' '||
r.ename);
15.
16. exit when emp_20%rowcount>=20;
17.
18. end loop;
19.
20. dbms_output.put_line('THE NO OF RECORDS DISPLAYED ARE'||
emp_20%rowcount);
21.
22. close emp_20;
23.
24. end;
HOW TO RETRIEVE THE FIRST FIVE EMPLOYEES WITH A JOB HISTORY USING CURSORS
1. declare
r emp_job%rowtype;
begin
loop
||r.job||'from'||r.hiredate);
exit when emp_job%rowcount>5;
end loop;
close emp_job;
end;
HOW TO UPDATE THE SALARY BY 10% FOR THOSE WHOSE SALARIES ARE LESS THAN
5000 USING THE CURSORS
1. declare
begin
loop
end if;
end loop;
end;
HOW TO GET TO DISPLAY DEPT AND EMP TABLES TOGETHER USING CURSORS
1. declare
begin
loop
dbms_output.put_line('departmentnumber:'||emp_record.deptno||
'department name'||emp_record.dname);
end if;
end loop;
loop
if emp_record.deptno<>30 then
dbms_output.put_line(emp_record.ename||emp_record.job||
emp_record.hiredate
||emp_record.sal);
end if;
end loop;
loop
if emp_record.deptno=30 then
dbms_output.put_line('departmentnumber:'||emp_record.deptno||
'department name'||emp_record.dname);
end if;
end loop;
if emp_record.deptno=30 then
dbms_output.put_line(emp_record.ename||emp_record.job||
emp_record.hiredate
||emp_record.sal);
end if;
end loop;
end;
HOW TO RETRIEVE THE DETAILS OF THE EMPLOYEES WHOSE DEPARTMENT WAS 100
USING THE CURSORS
1. declare
r emp_dup%rowtype;
begin
loop
end loop;
close dept_cursor;
end;
HOW TO RETRIEVE THE 20 RECORD OF THE EMPLOYEE TABLE USING CURSORS
1. declare
2. cursor emp_20 is select empno,ename from emp_dup;
3.
4. r emp_20%rowtype;
5.
6. begin
7.
8. open emp_20;
9.
10. loop
11.
12. fetch emp_20 into r;
13.
14. dbms_output.put_line('THE EMPLOYEE DETAILS WAS'||r.empno||' '||
r.ename);
15.
16. exit when emp_20 %rowcount>=20;
17.
18. end loop;
19.
20. dbms_output.put_line('THE NO OF RECORDS DISPLAYED ARE'||
emp_20%rowcount);
21.
22. close emp_20 ;
23.
24. end;
HOW TO RETRIEVE THE EMPLOYEE NUMBER AND NAME FROM THE EMPLOYEE TABLE
AND INSERT INTO TEMP_LIST TABLE BY USING CURORS AND RECORDS
1. declare
2. cursor temp_insert is select empno,ename from emp_dup;
3.
4. emp_record temp_insert%rowtype;
5.
6. begin
7.
8. open temp_insert;
9.
10. loop
11.
12. fetch temp_insert into emp_record;
13.
14. exit when temp_insert%notfound;
15.
16. insert into temp_list(empid,tname)
values(emp_record.empno,emp_record.ename);
17.
18. end loop;
19.
20. close temp_insert;
21.
22. end;
SQL SERVER