SQL Queries
SQL Queries
SQL Queries
http://expertanswercenter.techtarget.com/eac/knowledgebaseCategory/0,295197,sid63_tax29681
4_idx0_off50,00.html
http://scarlet.ucc.nau.edu/oracle9.0.1/nav/docindex.htm
http://scarlet.ucc.nau.edu/oracle9.0.1/appdev.901/a89856/toc.htm
http://scarlet.ucc.nau.edu/oracle9.0.1/server.901/a90125/toc.htm
CONSTRAINTS
Except Not null all others can be defined as Both table and column
level.
2. DEFAULT CONSTRAINT
UNIQUE
PRIMARY KEY
4. CHECK CONSTRAINT
5. REFERENCES
ENAME VARCHAR2(10),
MGR NUMBER(4) REFERENCES EMP9(ENO));
2. Display the details of all employees who are joined before 'FORD'
SELECT * FROM EMPLOYEES EMP, DEPARTMENT DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO AND EMP.JOINED_DATE<(
SELECT JOINED_DATE FROM EMPLOYEES EMP
WHERE EMP.DEPTNAME=’FORD’)
3. Display the Names of all the employees who are all getting the
salary Greater than the salary of 'SMITH' along with the
difference in salary
select ename,sal-(select sal from TEMP_EMP where ename='SMITH')
"DIFF SAL" from TEMP_EMP where sal>(select sal from TEMP_EMP
where ename='SMITH')
where sal in ( select sal from (select DISTINCT(sal) from TEMP_EMP order by sal desc) where
rownum<=&n)
-- SELECT DISTINCT (a.sal) FROM TEMP_EMP A
WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM TEMP_EMP B WHERE a.sal<=b.sal)
-- SELECT sal FROM (SELECT ROWNUM rn, sal FROM (SELECT DISTINCT (sal) FROM
temp_emp ORDER BY sal desc)) x WHERE x.rn = :n;
7. How do i get Greatest salaray of dept group from employee (we have to tables EMP and DEPT. for exp: dept 10 have
10000,dept 20 have 5000 and dept 30 have 15000 salaries, i need dept 30 group salary(means sum of that group is 15000). )
-- select deptno,sal1
from (select deptno , sum(sal) sal1 from temp_emp group by deptno order by sal1 desc)
where rownum<=1
TRUNCATE DELETE
It is a DDL statement It is a DML statement
It is a one way trip,cannot ROLLBACK One can Rollback
Doesn't have selective features (where clause) Has
Doesn't fire database triggers Does
It requires disabling of referential constraints. Does not require
SUBQUERIES
5. DISPLAY ALL THE DETAILS WHERE SAL IS GREATER THAN MILLER'S SALARY
AND WHO ARE IN SALES DEPARTMENT.
5
Select * from emp where dname = ‘sales’ and basal > (select
basal from emp where ename = ‘milers’);
6. DISPLAY ALL THE DETAILS WHO HAVE JOINED IN SAME DATE AS THAT OF
JAMES.
Select * from emp where doj = (select doj from emp where ename =
‘james’);
Select * from emp where dname = any(select dname from emp where
dname = ‘sales’ or dname = ‘research’);
Select * from emp where dname = any(select dname from emp where
Dname like(‘sales’,’research’));
ROWID
ROWNUM
CORRELATED SUBQUERY
SELECT * FROM DEPT WHERE ROWID NOT IN(SELECT ROWID FROM DEPT
WHERE ROWNUM<=(SELECT COUNT(*)-&N FROM DEPT));
SELECT * FROM EMP WHERE SAL> ANY(SELECT SAL FROM EMP WHERE
DEPTNO=20);
SELECT * FROM EMP WHERE SAL> ANY (SELECT SAL FROM EMP WHERE
DEPTNO=20);
DECLARE
NAME EMP.ENAME%TYPE;
SALARY EMP.SAL%TYPE;
DESIG EMP.JOB%TYPE:='&JOB';
BEGIN
SELECT ENAME,SAL,JOB INTO NAME,SALARY ,DESIG FROM EMP WHERE
JOB=DESIG;
DBMS_OUTPUT.PUT_LINE(NAME||' '||SALARY||' '||DESIG);
END;
16 CURSOR LOCKS
Answers:
DECLARE
cursor c1 is select ename,hiredate from emp;
name varchar(20);
hdate date;
begin
open c1;
loop
fetch c1 into name,hdate;
exit when c1%NOTFOUND;
dbms_output.put_line(name||' '||hdate);
end loop;
close c1;
end;
DECLARE
cursor c1 is select ename,sal,DEPTNO from emp
WHERE DEPTNO=10;
name varchar(20);
sl number;
dno number;
begin
open c1; loop
fetch c1 into name,sl,dno ;
IF c1%notFOUND THEN
DBMS_OUTPUT.PUT_LINE('CURSOR IS NOT FOUND');
exit;
elsif c1%found then
DBMS_OUTPUT.PUT_LINE('FOUND'||' '||C1%ROWCOUNT);
--INSERT INTO TEMP VALUES(NAME,SL,DNO);
END IF;
end loop;
close c1;
end;
4 B
DECLARE
cursor c1 is select * from emp
WHERE DEPTNO=&deptno;
a emp%rowtype;
begin
open c1;
LOOP
fetch c1 into a;
9
IF c1%FOUND THEN
DBMS_OUTPUT.PUT_LINE(A.ENAME||' '||A.SAL||' '||A.DEPTNO);
else
EXIT;
END IF;
END LOOP;
close c1;
end;
4 C
DECLARE
cursor c1 is select * from emp
WHERE DEPTNO=&deptno;
a emp%rowtype;
begin
open c1;
loop
fetch c1 into a;
IF not c1%FOUND THEN
exit;
else
DBMS_OUTPUT.PUT_LINE(a.ename||' '||a.sal);
END IF;
end loop;
close c1;
end;
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
A C1%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
IF A.JOB='CLERK' THEN
UPDATE EMP SET SAL=A.SAL+200 WHERE EMPNO=A.EMPNO;
COMMIT;
ELSIF A.JOB='MANAGER' THEN
DBMS_OUTPUT.PUT_LINE('ROW INSERTED');
INSERT INTO TEMP VALUES(A.ENAME,A.JOB,A.SAL);
COMMIT;
ELSIF A.JOB='ANALYST' THEN
DBMS_OUTPUT.PUT_LINE('ROW DELETED');
DELETE FROM EMP WHERE EMPNO=A.EMPNO;
COMMIT;
ELSIF A.JOB='SALESMAN' AND A.SAL*12>5000 THEN
DBMS_OUTPUT.PUT_LINE('ROW SALESMAN UPDATED');
UPDATE EMP SET SAL=SAL+300 WHERE EMPNO=A.EMPNO;
COMMIT;
ELSE
DBMS_OUTPUT.PUT_LINE('NO CHANGES');
10
END IF;
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
A C1%ROWTYPE;
BEGIN
IF C1%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('CURSOR ALREADY OPEN');
ELSE
DBMS_OUTPUT.PUT_LINE('CURSOR IS YET TO BE OPENED');
OPEN C1;
END IF;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(A.SAL||' '||A.ENAME||' '||A.JOB);
END LOOP;
CLOSE C1;
END;
6. B
IF NOT C1%ISOPEN
OPEN C1;
ELSE
DBMS_OUTPUT.PUT_LINE('CURSOR IS NOT OPEN');
END IF;
DECLARE
cursor c1 is select * from emp;
a c1%rowtype;
begin
open c1;
loop
fetch c1 into a;
exit when c1%rowcount>6;
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job||'
'||C1%ROWCOUNT);
11
end loop;
close c1;
end;
DECLARE
cursor c1 is select * from emp;
a c1%rowtype;
begin
open c1;
loop
fetch c1 into a;
exit when c1%notfound;
if c1%rowcount<=6 then
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job);
end if;
end loop;
close c1;
end;
DECLARE
cursor c1 is select * from emp order by sal desc;
a c1%rowtype;
begin
open c1;
loop
fetch c1 into a;
exit when c1%rowcount>6;
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job||'
'||C1%ROWCOUNT);
end loop;
close c1;
end;
DECLARE
cursor c1 is select * from emp ;
a c1%rowtype;
begin
open c1;
loop
fetch c1 into a;
exit when c1%notfound;
if mod(c1%rowcount,2)=0 then
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job);
end if;
end loop;
close c1;
end;
DECLARE
cursor c1 is select * from emp ;
a c1%rowtype;
n number;
begin
n:=&n;
open c1;
loop
fetch c1 into a;
exit when c1%notfound;
if c1%rowcount=n then
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job);
end if;
end loop;
close c1;
end;
declare
cursor c2 is select * from emp;
begin
for a in c2 loop
dbms_output.put_line(a.ename||' '||a.sal||' '||a.job);
end loop;
end;
DECLARE
CURSOR C1( z NUMBER) IS SELECT * FROM EMP WHERE DEPTNO=z;
A C1%ROWTYPE;
BEGIN
OPEN C1(&DEPTNO);
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(A.ENAME||' '||A.DEPTNO||' '||A.SAL);
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR C1( A VARCHAR) IS SELECT * FROM EMP WHERE JOB=A;
A C1%ROWTYPE;
BEGIN
OPEN C1('&AJOB');
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(A.JOB||' '||A.ENAME||' '||A.EMPNO||' ||A.SAL);
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR C1(A NUMBER,B VARCHAR) IS SELECT * FROM EMP
WHERE DEPTNO=A AND JOB=B;
A C1%ROWTYPE;
BEGIN
OPEN C1(&ADETNO,'&BJOB');
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(A.ENAME||' '||A.JOB||' '||A.SAL||'
||A.DEPTNO);
END LOOP;
CLOSE C1;
END;
16 CURSOR LOCKING
14
DECLARE
CURSOR C1 IS SELECT * FROM EMP FOR UPDATE OF SAL;
A C1%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(A.JOB||' '||A.ENAME||' '||A.SAL||' '||
A.DEPTNO);
IF A.JOB='CLERK' THEN
UPDATE EMP SET JOB='FDC' WHERE empno=a.empno;
end if;
end loop;
close c1;
end;
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
A C1%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(C1%ROWCOUNT||' '||SQL%ROWCOUNT||' '||A.ENAME);
END LOOP;
CLOSE C1;
END;
(YOU WILL NOT GET THE OUTPUT BECAUSE SQL%ROWCOUNT WILL RETURN A VALUE
ONLY WHEN THE PREVIOUS DML OPERATION IS SUCCESSFULLY COMPLETLE)
DECLARE
BEGIN
DELETE FROM EMP WHERE DEPTNO=10;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
END;
DECLARE
A EMP%ROWTYPE;
BEGIN
SELECT * INTO A FROM EMP WHERE EMPNO=7788;
DBMS_OUTPUT.PUT_lINE('AFTER SELECT SQLCOUNT'|| ' '||SQL%ROWCOUNT);
UPDATE EMP SET DEPTNO=99 WHERE DEPTNO=10;
DBMS_OUTPUT.PUT_lINE('AFTER UPDATE SQLCOUNT'|| ' '||SQL%ROWCOUNT);
END;
OUTPUT IS
AFTER SELECT SQLCOUNT 1
AFTER UPDATE SQLCOUNT 3
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
A C1%ROWTYPE;
N NUMBER;
BEGIN
OPEN C1;
SELECT MAX(SAL) INTO N FROM EMP;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(N);
SELECT MAX(SAL) INTO N FROM EMP WHERE SAL<N;
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR C1 IS SELECT JOB,COUNT(JOB) XY ,SUM(SAL) AB FROM EMP
GROUP BY JOB;
A C1%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO A;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('________________________________________________')
;
DBMS_OUTPUT.PUT_LINE(' JOB '||' NUMBER '||' SUM OF SAL
16
');
DBMS_OUTPUT.PUT_LINE(A.JOB||' '|| A.XY ||' '||
A.AB);
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR T1 IS SELECT * FROM TEST1;
TEST_REC T1%ROWTYPE;
BEGIN
FOR TEST IN T1
LOOP
UPDATE TEST1 SET AREA=400 WHERE RADIUS=50;
IF SQL%ROWCOUNT >0 THEN
DBMS_OUTPUT.PUT_LINE('RECORD IS ALREADY EXISTS');
ELSE
DBMS_OUTPUT.PUT_LINE('THERE IS NO RECORD');
END IF;
END LOOP;
END;
DECLARE
RDA AREAS.RADIUS%TYPE;
BEGIN
SELECT RADIUS INTO RDA FROM AREAS WHERE RADIUS =345;
EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('RECORD NOT
FOUND');
END;
Database objects
TABLES,VIEWS,SYNONYM,INDEX,CLUSTER,SEQUENCE,PROCEDURE,FUNCTION
PACKAGE,TRIGGER
VIEWS
Virtual table changes reflected in table & viceversa no data of
its own, no redundancy and security. A logical table based on one or
more tables or views.
FORCED VIEWS
SYNONYMS
1).Public synonym
2).Private synonym
INDEXES
SEQUENCES
CLUSTERS
tcl commands
grant, revoke
SQL> SAVEPOINT A
2 ;
Savepoint created.
19
1 row created.
SQL> SAVEPOINT B;
Savepoint created.
1 row created.
7 rows selected.
Rollback complete.
6 rows selected.
20
Triggers:
DATABASE TRIGGERS
10. WRITE A DATABASE TRIGGER WHICH SHOULD NOT DELETE FROM EMP TABLE
IF THE DAY IS SUNDAY.
12. WRITE A DATABASE TRIGGERS WHICH INSERTS THE EMPNO AND DEPTNO
INTO TEMP TABLE(AFTER INSERT)
13. WRITE A DATABASE TRIGGER USING INSERTING AND UPDATING WE SHOULD NOT
INSERT ON SUNDAY AND WE SHOULD NOT UPDATE ON LAST DAY OF THE MONTH.
14. UPDATE EMP TABLE AND CHANGE OLD DEPTNO TO NEW DEPTNO. IF OLD DEPTNO
IS 20 THEN DELETE FROM DEPT WHERE DEPTNO IS EQUAL TO NEW DEPTNO
15. WRITE A DATABASE TRIGGER TO DELETE FROM DEPT TABLE .AFTER DELETING
INSERT THE OLD VALUES TO A TABLE.
16. WRITE A DATABASE TRIGGER WHICH SHOULD NOT DELETE THE ROW IF THE
MONTH IS APRIL.
4.WRITE A DATABASE TRIGGER WHICH ACTS JUST LIKE PRIMARY KEY AND DOES
NOT
ALLOW DUPLICATE VALUES
DECLARE
A NUMBER;
BEGIN
SELECT COUNT(*) INTO A FROM EMP WHERE EMPNO=:NEW.EMPNO;
IF A >=1 THEN
RAISE_APPLICATION_eRROR(-20500,'THE PRI KEY RULE IS
VOILATED');
ELSIF A=0 THEN
PRINT('RECORD IS INSERTED');
END IF;
END;
1 row created.
7.WRITE A DATABASE TRIGGER TO UPDATE THE SAL FIELD.IF SAL IS MORE THAN
5000 OR LESS THAN 1000 THEN RAISE THE ERROR.(BEFORE UPDATE).
end if;
END;
9.WRITE A DATABASE TRIGGER WHICH SHOULD NOT DELETE FROM EMP TABLE IF
THE DAY IS SUNDAY.
if to_char(sysdate,'dy')='mon' then
raise_application_error(-20001,'TO DAY IS SUNDAY ');
end if;
end if;
IF UPDATING THEN
if to_char(sysdate)=LAST_DAY(SYSDATE) then
raise_application_error(-20001,'TO DAY IS MONTHY
LAST DAY so no updating ');
end if;
END IF;
END;
13.UPDATE EMP TABLE AND CHANGE OLD DEPTNO TO NEW DEPTNO. IF OLD DEPTNO
IS 20 THEN DELETE FROM DEPT WHERE DEPTNO IS EQUAL TO NEW DEPTNO
when (new.job='MANAGER')
declare
lo number;
hi number;
begin
select losal,hisal into lo,hi from salgrade where grade=5;
if :new.sal>lo and :new.sal<hi then
print('insert success');
else
raise_application_error(-20500,'INsert operation not allowed');
end if;
end;
declare
dummy varchar(20);
begin
select user into dummy from dual;
if dummy <>'GEETHA' then
raise_application_error(-20002,'today no transaction');
else
db('a proper user ');
end if;
exception
when no_data_found then
db('no data found');
end;
A NUMBER;
BEGIN
SELECT COUNT(*) INTO A FROM EMP WHERE DEPTNO=:NEW.DEPTNO;
IF A>=1 THEN
RAISE_APPLICATION_ERROR(-20101,'PRIMARY KEY VIOLATION');
ELSE
DBMS_OUTPUT.PUT_LINE('VALUE INSERTED');
END IF;
END;
EXCEPTION
---------
PRE-DEFINED :
-----------
NO_DATA_FOUND
TOO_MANY_ROWS
CURSOR_ALREADY_OPEN
INVALID_CURSOR
ZERO_ERROR 100/0
STORAGE_ERROR
PROGRAM_ERROR
LOGIN_DENIED
USER-DEFINED EXCEPTION
----------------------
28
---------------------------------------------------------------------
VALUE_ERROR:
-----------
DECLARE
S1 NUMBER;
BEGIN
A:=1000;
SELECT SAL INTO S1 FROM EMP WHERE EMPNO=700;
DBMS_OUTPUT.PUT_LINE(S1);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO SUCH EMPLOYEE');
WHEN VALUE_ERROR THEN
A:=100;
UPDATE EMP SET SAL=SAL+A;
END;
---------------------------------------------------------------------
USER-DEFINED EXCEPTION
----------------------
DECLARE
A1 EXCEPTION;
S1 EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO S1 FROM EMP WHERE EMPNO=7900;
RAISE A1;
EXCEPTION
WHEN A1 THEN
UPDATE EMP SET SAL=S1+2000 WHERE EMPNO=7900;
END;
--------------------------------------------------------------------
DECLARE
RAD NUMBER;
AREA2 NUMBER;
BEGIN
SELECT RADIUS,AREA INTO RAD,AREA2 FROM TEST1 WHERE RADIUS IN(10,20);
DBMS_OUTPUT.PUT_LINE(RAD||AREA2);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('TOO MANY ROWS');
END;
29
1.
WRITE A FUCTION TO DISPLAY THE SYSTEM TIME AND THE CORRESPONDING
PL/SQL BLOCK.
2.
(STORED FUNCTION)
WRITE A FUNCTION TO DISPLAY THE JOB WHEN THE NUMBER IS PASSED
WRITE A FUNCTION TO ACCEPT NAME AND PRINT JOB
3.
WRITE A FUCTION TO ADD 2 NUMBERS.
(STORED FUNCTION)
WRITE A FUNCTION TO CHECK WHETHER NUMBER IS EVEN OR ODD
4.
(LOCAL FUNCTION)
WRITE A FUNCTION TO GET SALARY WHEN NUMBER IS PASSED.
5.
WRITE A FUNCTION TO ILLUSTRATE IN OUT PARAMETERS.
6.
EXAMPLE FOR IN PARAMETER
7.
WRITE A FUNCTION TO ILLUSTRATE OUT PARAMETER.
WRITE A FUNCTION TO DISPLAY NAME,JOB AND SALARY WHEN NUMBER IS
PASSED
8.
IF ANNSAL>10000 TAX IS 10%
IF ANNSAL>15000 TAX IS 20%
IF ANNSAL>20000 INCOME TAX IS 30%
ELSE
INCOME TAX IS ZERO.
9.
WRITE A FUNCTION TO FIND THE FACTORIAL OF A NUMBER
10.
WRITE A BLOCK TO CALCULATE GROSS SALARY WHEN NUMBER IS PASSED.
GROSS=BASIC+HRA(30%)+DA(50%)-PF(10%)
11.
WRITE A FUNCTION TO RETURN THE AVERAGE SALARY OF THE
EMPLOYEES.IF THE
NUMBER OF EMPLOYEES ARE MORE THAN 2 THEN ONLY CALCULATE THE
AVERAGE.
30
12.
write a pl/sql block to delete a record. If delete operation
is
successful return 1 else return 0.
13.
write a pl/sql block to pass the employee number and the
increment
amount.FUNCTION SHOULD RETURN THE NET SALARY.
14.
write a pl/sql block to illustrate default as in parameter.
************************************************************************
***********
FUNCTIONS
~~~~~~~~~
1.
WRITE A FUCTION TO DISPLAY THE SYSTEM TIME AND THE CORRESPONDING
PL/SQL BLOCK.
DECLARE
AB VARCHAR(30);
BEGIN
AB:=TIME1;
DBMS_OUTPUT.PUT_lINE(AB);
END;
________________________________________________________________________
_
2.(STORED FUNCTION)
WRITE A FUNCTION TO DISPLAY THE JOB WHEN THE NUMBER IS PASSED
DECLARE
A VARCHAR(10);
BEGIN
A:=SAL(&EMPNO);
DBMS_OUTPUT.PUT_LINE(A);
END;
_____________________
S
CREATE OR REPLACE FUNCTION FUN(N VARCHAR2)
RETURN VARCHAR2 IS
J VARCHAR2(10);
BEGIN
SELECT JOB INTO J FROM EMP WHERE ENAME=N;
RETURN J;
END;
Declare
A varchar2(25);
B varchar2(25);
Begin
A:= fun(&b);
Dbms_output.put_line(a);
End;
________________________________________________________________________
_
3.(STORED FUNCTION)
WRITE A FUNCTION TO CHECK WHETHER NUMBER IS EVEN OR ODD
DECLARE
NO_TEST NUMBER:=&NO_TEST;
MES VARCHAR(25);
BEGIN
MES:=EV_ODD(NO_tEST);
DBMS_OUTPUT.PUT_LINE(MES);
END;
______________________
RETURN NUMBER IS
RES NUMBER;
BEGIN
RES:=N1+N2;
RETURN RES;
END;
________________________________________________________________________
_
4.(LOCAL FUNCTION)
WRITE A FUNCTION TO GET SALARY WHEN NUMBER IS PASSED.
DECLARE
A NUMBER;
FUNCTION F1(N NUMBER) RETURN NUMBER
IS
SALARY EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO SALARY FROM EMP WHERE EMPNO=N;
RETURN SALARY;
END;
BEGIN
A:=F1(&N);
DBMS_OUTPUT.PUT_LINE(A);
END;
________________________________________________________________________
_
5.
WRITE A FUNCTION TO ILLUSTRATE IN OUT PARAMETERS.
declare
n1 number :=10;
n2 number;
begin
n2 :=fun(n1);
dbms_output.put_line(n2);
end;
33
________________________________________________________________________
_
6.
EXAMPLE FOR IN PARAMETER
DECLARE
SALARY NUMBER(7,2);
BEGIN
SALARY:=FUN1(7788);
DBMS_OUTPUT.PUT_lINE(SALARY);
END;
________________________________________________________________________
_
7.
--WRITE A FUNCTION TO ILLUSTRATE OUT PARAMETER.
--WRITE A FUNCTION TO DISPLAY NAME,JOB AND SALARY WHEN NUMBER IS PASSED
DECLARE
A EMP.EMPNO%TYPE:=&EMPNO;
B VARCHAR(30);
C VARCHAR(30);
D NUMBER(8,2);
BEGIN
B:=FUN5(A,D,C);
DBMS_OUTPUT.PUT_LINE(A||' '||B||' '||C||' '||d);
END;
new 2: A EMP.EMPNO%TYPE:=7900;
Commit complete.
7900 JAMES CLERK 950
________________________________________________________________________
_
8.
DECLARE
IT NUMBER;
BEGIN
IT:=INCOMETAX(&EMPNO);
DBMS_OUTPUT.PUT_LINE('INCOME TAX OF THE EMPLOYEE IS');
DBMS_OUTPUT.PUT_LINE(IT);
END;
________________________________________________________________________
_
9.
--WRITE A FUNCTION TO FIND THE FACTORIAL OF A NUMBER
F:=F*I;
END LOOP;
RETURN F;
END;
DECLARE
RESULT NUMBER;
N NUMBER:=&N;
BEGIN
RESULT:= FACT(N);
DBMS_OUTPUT.PUT_LINE('FACTORIAL OF'|| N || 'IS'||RESULT);
END;
OR
10.
WRITE A BLOCK TO CALCULATE GROSS SALARY WHEN NUMBER IS PASSED.
GROSS=BASIC+HRA(30%)+DA(50%)-PF(10%)
DECLARE
GROSS1 NUMBER;
ENO NUMBER:=&EMPNO;
SALARY NUMBER;
BEGIN
SELECT SAL INTO SALARY FROM EMP WHERE EMPNO=ENO;
DBMS_OUTPUT.PUT_LINE('BEFORE FUNCTION CALL SALARY IS'||' '||SALARY);
GROSS1:=GROSS(ENO);
DBMS_OUTPUT.PUT_LINE('AFTER FUNCTION CALL SALARY IS'||' '||GROSS1);
END;
OR
SQL> SELECT GROSS(7934) FROM DUAL;
GROSS(7934)
-----------
2210
36
________________________________________________________________________
_
11.
WRITE A FUNCTION TO RETURN THE AVERAGE SALARY OF THE
EMPLOYEES.IF THE
NUMBER OF EMPLOYEES ARE MORE THAN 2 THEN ONLY CALCULATE THE
AVERAGE.
DECLARE
N NUMBER:=&DEPTNO;
ASAL NUMBER;
BEGIN
ASAL:=AVSAL(N);
DBMS_OUTPUT.PUT_lINE('AVERAGE SALARY OF DEPTNO'||' '||N||' IS
'||ASAL);
END;
________________________________________________________________________
_
12.
--write a pl/sql block to delete a record. If delete operation
--ISsuccessful return 1 else return 0.
declare
n number;
begin
37
n:=fun3(&empno);
dbms_output.put_Line(n);
if n=0 then
dbms_output.put_line('deletion unsuccessfull');
elsif n=1 then
dbms_output.put_line('deletion successfull');
end if;
end;
________________________________________________________________________
_
13.
write a pl/sql block to pass the employee number and the increment
amount . FUNCTION SHOULD RETURN THE NET SALARY
declare
netsal number;
begin
netsal:=raise_sal(&empno,&salary);
dbms_output.put_line('Net salary is'||' '||netsal);
end;
________________________________________________________________________
_
14.
write a pl/sql block to illustrate default as in parameter.
________________________________________________________________________
_
1 ARITHMETIC FUNCTIONS
~~~~~~~~~~~~~~~~~~~~
ABs
ceil(>n)
floor(<n)
mod
power
sign
sqrt
trunc
round
(SIN,COS,TAN,SINH,LOG,LN)
2 CHARACTER FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~
INITCAP
LOWER
UPPER
LPAD
RPAD
SOUNDEX
LTRIM
RTRIM
SUBSTR
LENGTH
INSTR
CONCAT
3 GENERAL FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~
NVL
DECODE
TRANSLATE
REPLACE
GREATEST
LEAST
4 DATE FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~
SYSDATE
ADD_MONTHS
MONTHS_BETWEEN
LAST_DAY
NEXT_DAY
5 CONVERSION FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TO_DATE
TO_CHAR
TO_NUMBER
GROUP FUNCTIONS
~~~~~~~~~~~~~~~~
40
~~~~~~~~~~~~~~~~
COUNT
SUM
MAX
MIN
AVG
SYMBOL DEFINITION
~~~~~~ ~~~~~~~~~~
D NUMBER OF DAY IN WEEK(1-7 STARTS WITH SUNDAY)
DD DAY IN NUMBERS
DDD NUMBER OF DAYS SINCE JAN1
DAY DAY FULLY SPELT OUT
DY FIRST 3 CHARACTERS OF THE DAY
MM MONTH IN NUMBERS
MONTH MONTH FULLY SPELT OUT
MON MONTH IN 3 LETTERS
WW NUMBER OF WEEKS OVER SINCE JAN 1
W NUMBER OF WEEKS OVER IN THAT MONTH
Y 1 DIGIT YEAR
YY 2 DIGIT YEAR
YYY 3 DIGIT YEAR
YYYY 4 DIGIT YEAR
YEAR YEAR FULLY SPELT OUT
HH HOUR
MI MINUTES
SS SECONDS
SSSS MILLISECONDS
HH24 24 HOUR FORMAT
HH12 12 HOUR FORMAT
FMDDTH 1ST,2ND,3RD ----(ORDINAL FORMAT)
RM ROMAN NUMERAL MONTH
FMDDSPTH SPELLED FORMAT (FIRST, SECOND, THIRD)
CORRECT QUERY
~~~~~~~~~~~~~~
SELECT ROWNUM,ROWID,ENAME FROM EMP WHERE ROWNUM<=&N
MINUS
SELECT ROWNUM,ROWID,ENAME FROM EMP WHERE ROWNUM<=&N
(TRIALS)
~~~~~~~~~
select rownum,rowid,empno,ename,job FROM EMP A WHERE ROWID IN
(
(SELECT ROWID FROM EMP WHERE ROWNUM <=
(SELECT COUNT(*)-&N FROM EMP)) minus
(select rowid from emp where rownum<=
(select count(*) -&n from emp))
);
(SECOND TRIAL)
~~~~~~~~~~~~~~
select rownum,rowid,empno,ename,job FROM EMP A WHERE ROWID IN
(
(SELECT ROWID FROM EMP WHERE ROWNUM <
(SELECT COUNT(*)-&N FROM EMP)) minus
(select rowid from emp where rownum<=
(select count(*) -&n from emp))
);
(THIRD TRIAL)
~~~~~~~~~~~~~~~
DELETE FROM EMP A WHERE ROWID IN
42
(
( SELECT ROWID FROM EMP WHERE ROWNUM<=&N)
MINUS
(SELECT ROWID FROM EMP WHERE ROWNUM<=&N)
);
select a.ename,a.sal,b.ename,b.sal
from emp a,emp b
where a.ename='FORD' AND B.SAL>A.SAL;
SELECT ENAME,SAL,JOB
FROM EMP
WHERE SAL>(SELECT SAL FROM EMP WHERE ENAME='ADAMS');
3. Display the details of all employees who are joined before 'FORD'
SELECT A.ENAME,A.SAL,A.HIREDATE,B.ENAME,B.SAL,B.HIREDATE
FROM EMP A,EMP B
WHERE A.ENAME='FORD' AND B.HIREDATE<A.HIREDATE;
4. Display the details of all the employees who are joined After SMITH
2 But Before Miller.
5. Display the Details of all employees along with their managers Name
43
AND
Employee Number.
select a.ename,a.sal,a.job,a.deptno
from emp a,emp b
where a.deptno=b.deptno and b.ename='FORD';
7. Display the details of all the employees who are Getting the Less
commision than 'FORD.'. Consider NULL commision as '0'
8. Display the details of all the employees for whom the Ford is
the Manager.
9. Display the Details of all the employees who are joined in the
same month of FORD
SELECT A.ENAME,A.HIREDATE,B.ENAME,B.HIREDATE
FROM EMP A,EMP B
WHERE B.ENAME='FORD' AND TO_CHAR(A.HIREDATE,'MONTH')=
TO_CHAR(B.HIREDATE,'MONTH');
44
10. Display the details of all the employees who are in the
SALES Department
SELECT A.ENAME,A.DEPTNO,A.JOB,B.DEPTNO,B.DNAME
FROM EMP A,DEPT B WHERE B.DNAME='SALES' AND
A.DEPTNO=B.DEPTNO;
11. Display the details of all the employees who are not ANALYSTS
and Getting Salary Greeater than 'CLARK';
SELECT A.ENAME,A.JOB,A.SAL,B.ENAME,B.SAL,B.JOB
FROM EMP A,EMP B
WHERE A.JOB!='ANALYST' AND A.SAL >B.SAL AND
B.ENAME='CLARK';
12. Display All the employee Names along with their Manager.
13. Display the Names of all the employees who are all getting the
salary Greater than the salary of 'SMITH'
SELECT A.ENAME,A.JOB,A.SAL,B.ENAME,B.SAL
FROM EMP A,EMP B
WHERE B.ENAME='SMITH' AND A.SAL > B.SAL;
14. Display the Names of all the employees who are all getting the
salary Greater than the salary of 'SMITH' along with the
difference in salary
SELECT A.ENAME,A.JOB,A.SAL,B.ENAME,B.SAL,A.SAL-B.SAL
FROM EMP A,EMP B
WHERE B.ENAME='SMITH' AND A.SAL>B.SAL ;
15. Display the details of all the employees whose sal is greater
than the average salary of the employees in their
respective deparments.
45
17. LIST FOLLOWING DETAILS FOR EMPLOYEES WHO EARN 36000/- A YEAR
OR ARE CLERKS
SELECT A.ENAME,A.JOB,A.SAL*12,B.DNAME
FROM EMP A,DEPT B
WHERE A.DEPTNO=B.DEPTNO AND A.SAL*12>30000
OR A.DEPTNO=B.DEPTNO AND A.JOB='CLERK';
18.
select ename,sal,
decode(sal,least(sal,1000),'<1000',
greatest(least(sal,2000)),'>1000 and <2000',
greatest(least(sal,3000)),'>2000 and <3000') from emp;
/*SILLY*/
OR
SELECT SAL,DECODE(FLOOR(SAL/1000),0,'<1000',1,'> 1000 AND < 2000',
3,'>2000 AND < 3000','> 3000') FROM EMP;
OR
46
(SANJAYA)
SELECT SAL,DECODE(GREATEST(SAL,1000),1000,'< 1000',
LEAST(SAL,2000), ' > 1000 AND < 2000',
LEAST(SAL,3000), '> 2000 AND < 3000',
LEAST(SAL,4000),' = 3000',' > 3000 ') FROM EMP;
19. display the details of all the employees whose salary is equal to
range as specified in grade 5.
20. CREATE A SEQUENCE AND GENERATE SOME NUMBERS .INSERT THESE NUMBERS
INTO THE TABLE BY CONCATENATING IT WITH E.
create sequence s1
start with 1
increment by 1
maxvalue 10
21.
_______________________________________________________________________
END OF REPORT
_______________________________________________________________________
select a.sal,a.ename,a.deptno
from emp a
group by a.deptno having a.sal>all(select avg(sal) from emp b
from emp b
group by deptno);
-------------------------------------------------------------------
declare
47
a number:=&a;
b number:=&b;
c number;
begin
c:=a*b;
b:=a+b;
a:=mod(c,b);
dbms_output.put_line(' '||a);
dbms_output.put_line(' '||b);
dbms_output.put_line(' '||c);
end;
declare
a number:=&a;
n number;
begin
for i in 1..10 loop
n:=a*i;
dbms_output.put_line(a||'*'||i||'='||n);
end loop;
end;
declare
a number:=&a;
b number;
c number:=2;
begin
for i in 1..10 loop
if a:=1 then
b:=c+2;
else
b:=2+i;
dbms_output.put_line(b);
end if;
end loop;
end;
declare
begin
for i in 1..3 loop
dbms_output.put_line('?');
dbms_output.new_line;
dbms_output.put(?);
end loop;
end;
48
declare
a number(10);
begin
a:=1;
dbms_output.put_line(a);
a:=12;
dbms_output.put_line(a);
a:=123;
dbms_output.put_line(a);
a:=1234;
dbms_output.put_line(a);
a:=12345;
dbms_output.put_line(a);
a:=123456;
dbms_output.put_line(a);
a:=1234567;
dbms_output.put_line(a);
a:=12345678;
dbms_output.put_line(a);
a:=123456789;
dbms_output.put_line(a);
a:=1234567890;
dbms_output.put_line(a);
end;
declare
e1 number;
e2 number;
e3 number;
val number;
begin
val:=&e3;
select deptno into e2 from emp where empno=val;
if e2=10 then update emp set comm=1000 where empno=val;
elsif e2 = 20 then update emp set comm=2000 where empno=val;
elsif e3=30 then update emp set comm=3000 where empno=val;
end if;
end;
declare
type type1 is record(eno number(4) not null:=7788,ena emp.ename%type);
erec type1;
begin
select empno,ename into erec from emp where empno=7788;
dbms_output.put_line(erec.eno||erec.ena);
end;
declare
salary emp.sal%type;
mgr_num emp.mgr%type;
emno emp.empno%type;
enam emp.ename%type;
begin
select ename,empno,sal, mgr into enam,emno,salary,
mgr_num from emp where empno=7788;
while salary<4000
49
loop
update emp set sal=1000 where empno=mgr_num;
end loop;
insert into emp1 values(null,salary,enam);
commit;
end;
declare
no number;
name varchar2(20);
salary number(7,2);
begin
select ename,sal into name,salary from emp where empno=&no;
insert into emp1 values(name,salary);
end;
declare
str varchar2(20);
rev varchar2(20);
begin
str:='&input';
if length(str)=0 then
dbms_output.put_line('null');
else
for i in 1..length(str)
loop
rev:=substr(str,i,1);
end loop;
end if;
dbms_output.put_line('rev string' ||rev);
end;
declare
a number;
b number:=1;
begin
a:=&no;
for i in 1..a
loop
b:=b*i;
end loop;
dbms_output.put_line(b);
end;
a. DEPT :
50
7: DECREASE THE SIZE FOR THE COLUMN EMPNO WITH THE FOLLOWING
INFORMATION :-
COLUMN NAME DATATYPE(SIZE)
--------------------------------------------------------
EMPNO NUMBER(2)
11: WHAT IS THE DATA TYPE OF THE COLUMN HIREDATE AND HOW MANY BYTES
IT OCCUPIES
12: CREATE A TABLE EMP AND DEPT USING THE FOLLOWING INFORMATIOM
a. DEPT :
EMP :
EMP:
(i)7369,SMITH,CLERK,7902,17-DEC-80,800,NULL,20
(ii)7499,ALLEN,SALESMAN,7698,20-FEB-81,1600,300,30
(iii)7521,WARD,SALESMAN,7698,22-FEB-81,1250,500,30
(iv)7566,JONES,MANAGER,7839,02-APR-81,2975,NULL,20
(v)7654,MARTIN,SALESMAN,7698,28-SEP-81,1250,1400,30
(vi)7698,BLAKE,MANAGER,7839,01-MAY-81,2850,NULL,30
(vii)7782,CLARK,MANAGER,7839,09-JUN-81,2450,NULL,10
(viii)7788,SCOTT,ANALYST,7566,19-NOV-96,3000,NULL,20
(ix)7839,KING,PRESIDENT,NULL,17-NOV-81,5000,NULL,10
(x)7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
(xi)7876,ADAMS,CLERK,7788,23-DEC-96,1100,NULL,20
(xii)7900,JAMES,CLERK,7698,03-DEC-81,950,NULL,30
(xiii)7902,FORD,ANALYST,7566,03-DEC-81,3000,NULL,20
(xiv)7934,MILLER,CLERK,7782,23-JAN-82,1300,NULL,10
(xv)7943,JOHN,CLERK,7943,10-DEC-83,2000,NULL,50
DEPT:
(i)10,ACCOUNTING,NEW YORK
(ii)20,RESEARCH,DALLAS
(iii)30,SALES,CHICAGO
(iv)40,OPERATIONS,BOSTON
(v)50,COMPUTER,AMERICA
20: INSERT ONLY THE RECORDS OF EMPLOYEE NUMBER,NAME,SALARY
INTO EMP TABLE
21: INSERT TWO ROWS INTO EMP TABLE USING PARAMETER SUBSTITUTION
28: COPY ALL THE RECORDS OF THREE COLUMNS EMPNO,ENAME,JOB FROM EMP
TABLE AND INSERT THE RECORDS INTO A TEMP TABLE WITH COLUMN
NAMES
SAME AS EMPNO,ENAME,JOB
30: LIST THE INFORMATION OF ALL THE EMPLOYEES PRESENT IN THE USER
NAMED SCOTT
31: LIST THE INFORMATION OF ALL THE DEPARTMENTS FROM YOUR BATCHMATES
DEPT TABLE
36: LIST OUT ALL THE TABLES WHICH START WITH 'S'
SOL: ROLLBACK;
SOL: ROLLBACK;
( OR )
54: UPDATE A RECORD OF EMP TABLE AND SAVE THE CHANGES PERMANENTLY
IN THE DATABASE
58: LIST THE USER'S ID AND WHICH DATABASE OBJECT YOU HAVE GRANTED
60: LIST ALL THE EMPLOYEES SALARY WITH COMMISSION PROVIDE A NEW
HEADING AS TOTAL SALARY
61: GIVE THE SALARY WITH A REDUCTION OF 10% FROM THEIR SALARY TO
PERSONS WHOSE JOB IS CLERK AND AND BELONGING TO DEPARTMENT
NUMBER
30
63: LIST THE NAME AND SALARY OF THE EMPLOYEES WHOSE SALARY IS
LESS THAN 3000
64: LIST THE NAME AND SALARY OF THE EMPLOYEES WHOSE SALARY IS
GREATER THAN 3000
67: LIST THE EMPLOYEE NAME AND SALARY WHOSE SALARY IS BETWEEN
2000 AND 3000
68: LIST ALL THE EMPLOYEES WHO HAVE JOINED BEFORE 30TH SEPTEMBER
1981
72: LIST EMPLOYEES NAME AND JOB WHOSE SALARY IS BETWEEN 2000 AND
3000
SOL: SELECT ENAME,JOB FROM EMP WHERE SAL BETWEEN 2000 AND 3000;
73: LIST THE EMPLOYEE NAMES AND THEIR DESIGNATION WHO HAVE JOINED
BEFORE 30TH JUNE '81 AND AFTER DECEMBER '81
SOL: SELECT ENAME FROM EMP WHERE HIREDATE NOT BETWEEN '30-JUN-81' AND
'31-DEC-81';
74: LIST THE EMPLOYEE NAMES WHO ARE NOT ELIGIBLE FOR COMMISSION
75: LIST THE EMPLOYEE NAMES WHO ARE ELIGIBLE FOR COMMISSION
76: LIST ALL THE EMPLOYEE NAMES BETWEEN THE LETTER 'M' AND 'O'
SOL: SELECT * FROM EMP WHERE ENAME BETWEEN 'M' AND 'O';
77: LIST THE NAME OF THE EMPLOYEE AND JOB OF THE EMPLOYEE , WHO DOES
NOT BELONGS TO ANY BODY
SOL: SELECT ENAME FROM EMP WHERE ENAME LIKE '_ _ _ _ _';
81: LIST THE EMPLOYEE NAMES HAVING 'I' AS THE SECOND CHARACTER
84: LIST THE EMPLOYEE NAME,SALARY, WHOSE SALARY IS BETWEEN 2000 AND
3000
86: LIST THE EMPLOYEE NAMES WHOSE JOB IS CLERK AND DEPTARTMENT IS 30
87: DISPLAY ALL THE EMPLOYEE NAMES IN THE FORMAT AS "NAME IS ALLEN"
SOL: SELECT EXP(7) "e TO THE 7TH POWER " FROM DUAL;
97: CHECK THE O/P WHEN A VALUE M>N IN A MOD FUNCTION AND NOTE DOWN
THE REASON FOR THAT KIND OF O/P
99: DISPLAY THE VALUE FOR 15.193 ROUNDED TO FIRST DECIMAL PLACE
FORWARD
100: DISPLAY THE VALUE FOR 15.193 ROUNDED TO FIRST DECIMAL PLACE
BACKWARDS
112: DISPLAY THE LEFT PADDED CHAR'S TO THE STRING BRAINWARE WHEN
THE RETURN VALUE IS 15 AND PADDED CHARACTER IS '*'
113: DISPLAY THE RIGHT PADDED CHAR'S TO THE STRING BRAINWARE WHEN
THE RETURN VALUE IS 15 AND PADDED CHARACTER IS '*'
116: REPLACE THE CHARACTER 'J' WITH 'BL' FROM THE WORD
JACK AND JUE
SOL: SELECT
TRANSLATE('0123KASL','0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'9999999999TTTTTTTTTTTTTTTTTTTTTTTTTT') from dual;
125: DISPLAY THE DATE OF THE NEXT TUESDAY AFTER MARCH 15,1992.
127: DISPLAY THE DIFFERENCE DATE BETWEEN CURRENT DATE AND LAST DAY
OF THE SYSDATE
135: DISPLAY THE CURRENT DATE IN THE FORMAT DDTH MONTH YYYY
141: DISPLAY ALL THE ENAMES WHERE COMMISSION IS NULL AND PLACE
THE EXPRESSION " NOT POSSIBLE " IN DEPTARTMENT 30 WHERE
COMMISSION
IS NULL ELSE DISPALY THEIR COMMISSION
157: CALCULATE THE VARIANCE OF ALL THE SALARIES FROM EMP TABLE
PROBLEMS ON JOINS:-
162: LIST THE TOTAL EMPLOYEES SALARIES WHO DOES NOT BELONG
TO ANY DEPARTMENT
163: LIST THE MANAGER'S NAME AND EMPLOYEES NAME WHO BELONGS TO
THEIR MANAGER'S
168: DISPLAY INFORMATION ABOUT EMPLOYEES WHO EARN MORE THAN ANY
EMPLOYEES IN DEPARTMENT 30
170: LIST THE EMPLOYEES WITH EITHER THE SAME JOB AS JONES
OR SALARY GREATER THAN OR SAME AS FORD
(OR)
175: LIST THE DEPARTMENT IN WHICH THEIR ARE MORE THAN ONE
SALESMAN
66
SELECT SEX,COUNT(*)
FROM EMP
GROUP BY SEX
HAVING SEX ='M';
UNION
SELECT SEX,COUNT(*)
FROM EMP
GROUP BY SEX
HAVING SEX='F';
67
183: DISPLAY THE YEAR AND NUMBER OF EMPLOYEES FOR THE YEAR
IN WHICH MORE THAN ONE EMPLOYEE WAS HIRED
SOL: SELECT ENAME,SAL FROM EMP MINUS SELECT ENAME,SAL FROM EMP
WHERE ROWNUM<(SELECT MAX(ROWNUM)-4 FROM EMP );
193: LIST THE TREE STRUCTURED QUERY WITH EMPLOYEE NAME STARTING
WITH KING
195: SELECT ALL THE ENAMES WITH THE OCCURANCES OF THE LETTER 'S'
MINUS
SELECT JOB FROM EMP
WHERE DEPTNO=10
MINUS
SELECT JOB FROM EMP
WHERE DEPTNO=30;
199: LIST THE EMPLOYEES IF AND ONLY IF MORE THAN 10 EMPLOYEES ARE
PRESENT IN DEPARTMENT NUMBER 10
200: LIST THE EMPLOYEE NAMES WHOSE SALARY IS GREATER THAN THE
LOWEST SALARY OF AN EMPLOYEE BELONGING TO DEPARTMENT 20
201: LIST THE EMPLOYEE NAMES WHOSE SALARY IS GREATER THAN THE
HIGHEST SALARY OF AN EMPLOYEE BELONGING TO DEPARTMENT NUMBER 20
202: CREATE A VIEW WITH COLUMNS EMPNO AND A 10% HIKE SALARY COLUMN
AND TRY TO INSERT A ROW IN THIS VIEW AND VERIFY IT
216: CREATE A CLUSTER BASED ON THE DEPT AND EMP TABLE FOR
THE FIELD DEPTNO
217: INSERT INTO TABLES DEPT1 AND EMP1 EACH THREE ROWS AND
CHECK THE ROWID OF THE TWO CLUSTER TABLES;
EMPLOYEES REPORT
COLUMN:
NAMES EMPLOYEE EMPLOYEE DESIGNATION MANAGER DATE OF CURRENT
NUMBER NAME NUMBER JOINING SALARY
COMMISION DEPARTMENT
NUMBER
Package Specification
#****************#
PACKAGES
#****************#
#****************##****************##****************##****************#
_________________________________________________________________-
PROCEDURE P1 IS
ENO NUMBER;
NAME VARCHAR2(10);
DEPNO NUMBER;
SALARY EMP.SAL%TYPE;
BEGIN
ENO:=EMP_PACK.NEXTEMP;
PRINT('NEW EMPLOYEE NUMBER IS'||ENO);
SELECT ENAME,SAL,DEPTNO INTO NAME,SALARY,DEPNO FROM EMP WHERE
EMPNO=ENO;
PRINT(ENO||' '||NAME||' '||DEPNO||' '||SALARY);
EXCEPTION
WHEN NO_DATA_FOUND THEN
PRINT('NO DATA FOUND');
END P1;
END EMP_PACK;
DECLARE
NEXTEMP NUMBER;
F2 EMP.JOB%TYPE;
BEGIN
NEXTEMP:=EMP_PACK.NEXTEMP;
PRINT('NEXTEMP IS '||NEXTEMP);
EMP_PACK.P1;
F2:=EMP_PACK.F2(&EMPNO);
PRINT('PRINTING JOB FROM F2 '||F2);
EMP_PACK.P2(&DEPTNO);
END;
#****************##****************##****************##****************#
_______________________________________________________________________
END GEEPACK;
declare
eno number:=&empno;
dno number:=&deptno;
mg number:=&mgr;
name varchar2(10):='&name';
res number;
begin
print('new_emp is called and values will be inserted');
geepack.new_emp(eno,dno,mg,name);
geepack.update_emp(&updateno);
res:=geepack.CHECK_eMP(&checkno);
print(res);
end;
#****************##****************##****************##****************#
DECLARE
F1RES NUMBER;
BEGIN
F1RES:=PACK.F1;
PRINT('THE RESULT OF FUNCTION IS '||F1RES);
PACK.P1(F1RES);
PACK.P2(&DEPTNO);
END;
);
For the column ordid of the ordtab table i have inserted the below
specified values
99
98
100
150
199
200
250
290
90
1000
250
350
450
200
PARTITION_NAME
------------------------------
P1
P2
P3
Table altered.
PARTITION_NAME
------------------------------
P2
P3
SYS_P61
SYS_P62
Table altered.
PARTITION_NAME
------------------------------
P5
P3
SYS_P61
SYS_P62
P6
ADD PARTITION
79
~~~~~~~~~~~~~
CREATE TABLE GG
( ACNO NUMBER(3),
actype varchar2(3),
acbal number(4))
partition by range(acbal)
(partition p1 values less than(5000),
partition p2 values less than(6000));
Table created.
Table altered.
DROP PARTITION
~~~~~~~~~~~~~~~~~
ALTER TABLE GG DROP PARTITION P1;
ALTER TABLE <TABLENAME> DROP PARTITION <PARTITION NAME>
RENAME PARTITION
~~~~~~~~~~~~~~~~~~
ALTER TABLE EMP RENAME PARTITION P5 TO P6;
************************************************************************
~~~~~~~~~~~~~~~~~~~~
SIMPLE PL/SQL BLOCKS
~~~~~~~~~~~~~~~~~~~~
declare
a number;
b number;
begin
A:=25;
B:=50;
dbms_output.put_line('THE CONTENT OF A IS'||A);
dbms_output.put_line('THE CONTENT OF B IS'||B);
end;
declare
a number:=&A;
b numbeR:=&B;
begin
dbms_output.put_line('THE CONTENT OF A IS '||A);
dbms_output.put_line('THE CONTENT OF B IS '||B);
end;
declare
a number:=&A;
b numbeR:=&B;
PROD NUMBER;
begin
PROD:=A*B;
dbms_output.put_line('THE CONTENT OF A IS '||A);
dbms_output.put_line('THE CONTENT OF B IS '||B);
dbms_output.put_line('THE PRODUCT IS '||PROD);
81
end;
declare
a number:=10;
b number:=20;
res number;
begin
res:=a+b;
dbms_output.put_line(a|| ' '||b);
dbms_output.put_line('The sum is '||res);
end;
declare
a number:=&a;
b number:=&b;
res number;
begin
res:=a+b;
dbms_output.put_line(a|| ' '||b);
dbms_output.put_line('The sum is '||res);
end;
declare
a number:=&a;
b number:=&b;
begin
if (a>b) then
dbms_output.put_line('Biggest number is'||' '||a);
else
dbms_output.put_line('Biggest number is'||' '||b);
end if;
end;
declare
a number:=&a;
begin
if mod(a,5)=0 then
dbms_output.put_line('It is divisible by 5');
else
dbms_output.put_line('It is not divisible by 5');
end if;
end;
declare
n char(10):='&n';
begin
SELECT UPPER(N) INTO N FROM DUAL;
if n='F' then
dbms_output.put_line('FIRST CLASS');
ELSIF N='S' THEN
dbms_output.put_line('SECOND CLASS');
82
declare
n number:=1;
begin
loop
dbms_output.put_line(n);
n:=n+1;
exit when n=11;
end loop;
end;
declare
a number:=1;
begin
while (a<=10) loop
dbms_output.put_line(a);
a:=a+1;
end loop;
end;
declare
begin
for a in 1..10 loop
dbms_output.put_line(a);
end loop;
end;
declare
begin
for a in reverse 1..10 loop
dbms_output.put_line(a);
end loop;
end;
declare
n number:=&n;
i number:=1;
begin
while i<=n loop
dbms_output.put_line(i);
i:=i+2;
end loop;
end;
declare
n number:=&n;
i number:=1;
83
begin
while i<=n loop
dbms_output.put(i);
i:=i+2;
end loop;
dbms_output.new_line;
end;
DECLARE
BEGIN
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(I);
IF I=5 THEN
EXIT;
END IF;
END LOOP;
END;
declare
n number:=&n;
begin
dbms_output.put_line('even numbers are');
for i in 1..n loop
if mod(i,2)=0 then
dbms_output.put_line(i);
end if;
end loop;
end;
declare
n number:=&n;
begin
for i in 1..n loop
dbms_output.put(i);
end loop;
dbms_output.new_line;
end;
declare
n number:=&n;
sum1 number:=0;
begin
dbms_output.put_line('the numbers are');
for i in 1..n loop
dbms_output.put_line(i);
sum1:=sum1+i;
end loop;
dbms_output.put_line('SUM IS '||SUM1);
END;
84
DECLARE
N NUMBER:=&N;
RES NUMBER;
BEGIN
FOR I IN 1..10 LOOP
RES:=N*I;
DBMS_OUTPUT.PUT_LINE(N || ' * '|| I || ' = '|| RES );
END LOOP;
END;
GENERAL PROBLEMS
2. Write a PL/SQL Program to list all the EVEN & ODD Number from 1 to
100 as Follows
Even Numbers Odd Numbers
------------------- ----------------
-
0 1
2 3
4 5
85
. .
. .
3. Write a PL/SQL Program to Find the sum of numbers between 1 and 100
that are divisible by 5 but not
divisible by 7
4. Write a PL/SQL Script to find all the PRIME NUMBERS from 1 to 100
10. Write a PL/SQL Program to Find out the Given Input data is LEAP
YEAR OR NOT.
12. Write a PL/SQL Program to find out the RAMAN NUMBER between
1000 and 9999
ex : 1233 , 12*12+33*33 = 1233
13. Write a PL/SQL Program to display the vowels from the input
string
15. Write a PL/SQL Program to find out a given sub string from the
given Main string.(Without using
INSTR FUNCTION).
************************************************************************
*****
CURSOR PROBLEMS
ID
NAME
MARK1
MARK2
MARK3
MARK4
1000
AYYAPPAN
100
99
90
95
1002
BASKER
30
20
25
70
1001
N.B.SING
90
60
50
35
John,smith,miller
job : CLERK
------- --------
-------------
************************************************************************
*****
EXCEPTION
************************************************************************
*****
SUBPROGRAM
88
1. Write a PL/SQL Program to SWAP the two input number without using
the temporary variable, by
using the PROCEDURE.
************************************************************************
*****
DATABASE TRIGGERS
************************************************************************
*****
PACKAGES
BLOCKS CREATED ON
1. GOTO
2. EXIT
3. EXIT WHEN
4. GENERATE EVEN/ODD SERIES
5. GENERATE MULTIPLICATION TABLES
6. REVERSE OF NUMBER
7. REVERSE OF STRING
8. PRIME OR NOT
9. SQUARE
10. M TO POWER N
11. ODD AND EVEN SERIES TOGETHER
12. ACCEPT SYMBOL FROM USER AND DEPENDING ON THAT
ADD,SUBTRACT,MULTIPLY
OR DIVIDE 2 NUMBERS.
13. GENERATE NUMBERS IN FIBONACCI SERIES.
14. FACTORIAL OF NUMBER
1.---------GOTO STATEMENT
~~~~~~~~~~~~~~
DECLARE
BEGIN
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_lINE(I);
IF I=5 THEN
GOTO AA;
END IF;
END LOOP;
<<AA>>
DBMS_OUTPUT.PUT_lINE('OUTSIDE THE LOOP');
END;
OUTPUT IS
1
2
3
4
90
5
HELLO
DECLARE
A NUMBER:=&A;
B NUMBER:=&B;
BEGIN
a:=a+b+100;
if a>1000 then
goto zz;
else
dbms_output.put_line('out put not greater than 1000');
end if;
<<zz>>
dbms_output.put_line(a);
END ;
2.-----------------EXIT STATEMENT
~~~~~~~~~~~~~~
DECLARE
BEGIN
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_lINE(I);
IF I=5 THEN
EXIT;
END IF;
END LOOP;
END;
OUTPUT IS
1
2
3
4
5
OUTPUT IS
1
2
3
4
5
91
4.-----------------------WHILE LOOP
~~~~~~~~~~
GENERATE ODD/EVEN NUMBERS
DECLARE
N NUMBER:=&N;
I NUMBER:=1;
BEGIN
WHILE(I<N) LOOP
DBMS_OUTPUT.PUT_lINE(I);
I:=I+2;
END LOOP;
END;
DECLARE
N NUMBER:=&N;
BEGIN
FOR I IN 1..N LOOP
IF MOD(I,2)=0 THEN
DBMS_OUTPUT.PUT_lINE(I);
END IF;
END LOOP;
END;
5.--------------------MULTIPLICATION TABLES
~~~~~~~~~~~~~~~~~~~~~
DECLARE
N NUMBER:=&N;
RES NUMBER;
BEGIN
DBMS_OUTPUT.PUT_lINE('THE MULTIPLICATION TABLE IS');
FOR I IN 1..10 LOOP
RES:=N*I;
DBMS_OUTPUT.PUT_LINE(N ||' * '||I ||' = '||RES);
END LOOP;
END;
~~~~~~~~~~~~~~~~~~~~~
6.----------------REVERSE OF A NUMBER
~~~~~~~~~~~~~~~~~~~
DECLARE
N NUMBER:=&N;
R NUMBER:=0;
L NUMBER;
BEGIN
L:=LENGTH(N);
FOR I IN 1..L LOOP
R:=R*10+MOD(N,10);
N:=FLOOR(N/10);
END LOOP;
92
--DBMS_OUTPUT.PUT_LINE(N);
DBMS_OUTPUT.PUT_LINE(R);
END;
DECLARE
N NUMBER:=&N;
R NUMBER;
L NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('THE NUMBER IS '||N);
L:=LENGTH(N);
FOR I IN 1..L LOOP
R:=R||MOD(N,10);
N:=FLOOR(N/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE REVERSE OF THE NUMBER IS '||R);
END;
7. ------------------REVERSE OF A STRING
DECLARE
N VARCHAR2(12):='&N';
REV VARCHAR2(12);
LEN NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('STRING IS '||N);
LEN:=LENGTH(N);
FOR I IN REVERSE 1..LEN LOOP
REV:=REV||SUBSTR(N,I,1) ;
END LOOP;
DBMS_OUTPUT.PUT_LINE('REVERSED STRING IS '||rEV);
END;
8---------------------PRIME OR NOT
~~~~~~~~~~~~
DECLARE
N NUMBER:=&N;
FLAG NUMBER:=1;
BEGIN
FOR I IN 2..N-1 LOOP
IF MOD(N,I)=0 THEN
FLAG:=0;
EXIT;
END IF;
END LOOP;
IF FLAG=0 THEN
DBMS_OUTPUT.PUT_LINE('NUMBER IS NOT PRIME');
ELSE
DBMS_OUTPUT.PUT_lINE('NUMBER IS PRIME');
END IF;
93
END;
9.------------------DRAW A SQUARE
DECLARE
BEGIN
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT('?');
DBMS_OUTPUT.PUT(' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('? ?');
END LOOP;
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT('?');
DBMS_OUTPUT.PUT(' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END;
DECLARE
M NUMBER:=&M;
N NUMBER:=&N;
RES NUMBER;
BEGIN
RES:=POWER(M,N);
DBMS_OUTPUT.PUT_lINE('M TO POWER OF N IS '||RES);
END;
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('ODD-----------EVEN');
DBMS_OUTPUT.PUT_lINE('__________________');
FOR I IN 1..20
LOOP
IF MOD(I,2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(' '||I);
ELSE
DBMS_OUTPUT.PUT(I);
END IF;
END LOOP;
END;
94
12.---------------------------
ACCEPT SYMBOL FROM USER AND DEPENDING ON THAT ADD,SUBTRACT,MULTIPLY
OR DIVIDE 2 NUMBERS.
DECLARE
NUM1 NUMBER:=&NUMBER1;
SYB CHAR:='&SYMBOL';
NUM2 NUMBER:=&NUMBER2;
RESULT NUMBER;
BEGIN
IF SYB='+' THEN
RESULT:=NUM1+NUM2;
ELSE
IF SYB='-' THEN
RESULT:=NUM1-NUM2;
ELSE
IF SYB='*' THEN
RESULT:=NUM1*NUM2;
ELSE
IF SYB='/' THEN
RESULT:=NUM1/NUM2;
END IF;
END IF;
END IF;
END IF;
DBMS_OUTPUT.PUT_LINE(RESULT);
END;
95
PL/SQL Tables
Objects of type TABLE are called "PL/SQL tables," which are modelled
on (but not the same as) database tables. PL/SQL tables use a primary
key to give you array-like access to rows. The size of a PL/SQL table
is unconstrained. That is, the number of rows in a PL/SQL table can
increase dynamically.
96
PL/SQL tables can have one column and a primary key, neither of which
can be named. The column can belong to any scalar type, but the
primary
PL/SQL tables must be declared in two steps. First, you define a TABLE
type, then declare PL/SQL tables of that type. You can declare TABLE
types in the declarative part of any block, subprogram, or package
using the syntax
INDEX BY BINARY_INTEGER;
DECLARE
TYPE EnameTabTyp IS TABLE OF CHAR(10)
INDEX BY BINARY_INTEGER;
You could have used %TYPE to provide the column datatype, as follows:
DECLARE
TYPE EnameTabTyp IS TABLE OF emp.ename%TYPE
INDEX BY BINARY_INTEGER;
Once you define type "EnameTabTyp," you can declare PL/SQL tables of
that type, as follows:
ename_tab EnameTabTyp;
PACKAGE emp_actions IS
TYPE EnameTabTyp IS TABLE OF emp.ename%TYPE
INDEX BY BINARY_INTEGER;
TYPE SalTabTyp IS TABLE OF emp.sal%TYPE
INDEX BY BINARY_INTEGER;
ename_tab EnameTabTyp;
sal_tab SalTabTyp;
...
PROCEDURE hire_batch
97
(ename_tab EnameTabTyp,
sal_tab SalTabTyp,
...);
PROCEDURE log_names
(ename_tab EnameTabTyp,
num BINARY_INTEGER);
...
END emp_actions;
plsql_table_name(primary_key_value)
ename_tab(3) ...
You can assign the value of a PL/SQL expression to a specific row using
the following syntax:
plsql_table_name(primary_key_value) := plsql_expression;
In the next example, you assign the sum of variables "salary" and
"increase" to the fifth row in PL/SQL table "sal_tab":
DECLARE
TYPE JobTabTyp IS TABLE OF CHAR(14)
INDEX BY BINARY_INTEGER;
job_tab JobTabTyp;
BEGIN
job_tab(1) := 'CLERK';
IF job_tab(2) = 'CLERK' THEN -- raises NO_DATA_FOUND
...
END IF;
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- here because job_tab(2) does not exist
...
END;
98
Inserting/Fetching Rows
You must use a loop to INSERT values from a PL/SQL table into a
database
column. Likewise, you must use a loop to FETCH values from a database
column into a PL/SQL table. For example, given the declarations
DECLARE
TYPE EmpnoTabTyp IS TABLE OF NUMBER(4)
INDEX BY BINARY_INTEGER;
TYPE EnameTabTyp IS TABLE OF CHAR(10)
INDEX BY BINARY_INTEGER;
...
empno_tab EmpnoTabTyp;
ename_tab EnameTabTyp;
...
you might use the following procedure to INSERT values from the PL/SQL
tables into the "emp" database table:
PROCEDURE insert_emp_data
(rows BINARY_INTEGER,
empno_tab EmpnoTabTyp,
ename_tab EnameTabTyp,
...) IS
BEGIN
FOR i IN 1..rows LOOP
INSERT INTO emp (empno, ename, ...)
VALUES (empno_tab(i), ename_tab(i), ...);
END LOOP;
END;
Conversely, you might use the next procedure to FETCH all rows from the
PROCEDURE fetch_emp_data
(rows OUT BINARY_INTEGER,
empno_tab OUT EmpnoTabTyp,
ename_tab OUT EnameTabTyp,
...) IS
BEGIN
rows := 0;
FOR emprec IN (SELECT * FROM emp) LOOP
rows := rows + 1;
empno_tab(rows) := emprec.empno;
ename_tab(rows) := emprec.ename;
...
END LOOP;
END;
However, you cannot reference PL/SQL tables in the INTO clause. For
example, the following SELECT statement is illegal:
99
PROCEDURE fetch_emp_data
(rows OUT BINARY_INTEGER,
empno_tab OUT EmpnoTabTyp,
ename_tab OUT EnameTabTyp,
...) IS
BEGIN
SELECT empno, ename
INTO empno_tab, ename_tab -- illegal
FROM emp;
...
END;
Deleting Rows
You cannot delete individual rows from a PL/SQL table because the
DELETE statement cannot specify PL/SQL tables. However, you can use
a simple workaround to delete entire PL/SQL tables. When you want to
delete a PL/SQL table, simply assign a null to it, as shown in the
following example:
DECLARE
TYPE NumTabTyp IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;
sal_tab NumTabTyp;
...
BEGIN
/* Load salary table. */
FOR i IN 1..50 LOOP
sal_tab(i) := i;
END LOOP;
...
/* Delete salary table. */
sal_tab := NULL; -- releases all PL/SQL table resources
...
END;
declare
num number(5);
name varchar(10);
sal number(7,2);
begin
select empno,ename,sal into num,name,sal from emp where empno=7902;
dbms_output.put_Line(num||' '||name||' '||sal);
100
end;
declare
num number(5);
name varchar(10);
sal number(7,2);
begin
select empno,ename,sal into num,name,sal from emp where empno=&n;
insert into areas values(100,4000);
dbms_output.put_Line(num||' '||name||' '||sal);
end;
DECLARE
NAME VARCHAR2(10):='&NAME';
DESIG VARCHAR2(12);
SALARY NUMBER(7,2);
NETSAL NUMBER(7,2);
BEGIN
SELECT JOB,SAL INTO DESIG,SALARY FROM EMP WHERE ENAME=NAME;
DBMS_OUTPUT.PUT_LINE(NAME||' '||DESIG||' '||SALARY);
IF DESIG='CLERK' THEN
UPDATE EMP SET SAL=SALARY*0.1 +SALARY WHERE ENAME=NAME;
NETSAL:=SALARY*0.1+SALARY;
DBMS_OUTPUT.PUT_LINE('THE NET SALARY IS '||NETSAL);
ELSE
UPDATE EMP SET SAL=SALARY*0.2 +SALARY WHERE ENAME=NAME;
NETSAL:=SALARY*0.1+SALARY;
DBMS_OUTPUT.PUT_LINE('THE NET SALARY IS '||NETSAL);
END IF;
END;
3. update statement in pl/sql
block
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare
num number(5):=&EMPNO;
name varchar(10);
salary number(7,2);
desig varchar2(10);
begin
select empno,ename,sal,job into num,name,salary,desig
from emp where empno=NUM;
dbms_output.put_Line(num||' '||name||' '||salARY||' '||DESIG);
if desig='CLERK' THEN
UPDATE EMP SET SAL=SAL+200 WHERE EMPNO=NUM;
ELSIF DESIG='MANAGER' THEN
UPDATE EMP SET SAL=SAL+300 WHERE EMPNO=NUM;
END IF;
COMMIT;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare
num number(5);
name varchar(10);
salary number(7,2);
desig varchar2(10);
begin
select empno,ename,sal,job into num,name,salary,desig
from emp where empno=&n;
if desig='CLERK' THEN
DBMS_OUTPUT.PUT_LINE('RECORD DELETED');
DELETE FROM EMP WHERE EMPNO=NUM;
ELSE
DBMS_OUTPUT.PUT_LINE('RECORD NOT DELETED');
END IF;
dbms_output.put_Line(num||' '||name||' '||salARY||' '||DESIG);
end;
DECLARE
ENO NUMBER(4);
MGR NUMBER(4);
DEPNO NUMBER(3);
BEGIN
INSERT INTO EMP(EMPNO,MGR,DEPTNO)VALUES(&ENO,&MGR,&DEPNO);
DBMS_OUTPUT.PUT_LINE(ENO||' '||MGR||' '||DEPNO);
COMMIT;
END;
declare
num number(5);
name varchar(10);
salary number(7,2);
desig varchar2(10);
begin
select empno,ename,sal,job into num,name,salary,desig
from emp where empno=&n;
if desig='CLERK' THEN
DBMS_OUTPUT.PUT_LINE('RECORD INSERTED');
INSERT INTO TEMP VALUES(NUM,NAME,SALARY,DESIG);
END IF;
dbms_output.put_Line(num||' '||name||' '||salARY||' '||DESIG);
COMMIT;
end;
102
DECLARE
N NUMBER;
NAME EMP.ENAME%TYPE;
DESIG EMP.JOB%TYPE;
BEGIN
SELECT COUNT(*) INTO N FROM EMP WHERE JOB='MANAGER';
FOR I IN 1..N LOOP
SELECT ENAME,JOB INTO NAME,DESIG FROM EMP WHERE JOB='MANAGER';
DBMS_OUTPUT.PUT_LINE(NAME||' '||DESIG);
END LOOP;
END;
DECLARE
NAME VARCHAR2(10);
DESIG VARCHAR2(10);
SALARY NUMBER(7,2);
ENO NUMBER(4):=&EMPNO;
BEGIN
SELECT ENAME,JOB,SAL INTO NAME,DESIG,SALARY FROM EMP WHERE EMPNO=ENO;
DBMS_OUTPUT.PUT_LINE(ENO||' '||NAME||' '||SALARY||' '||DESIG);
IF DESIG='CLERK' THEN
DELETE FROM EMP WHERE EMPNO=ENO;
INSERT INTO TEMP VALUES(NAME,DESIG,SALARY);
DBMS_OUTPUT.PUT_LINE('DELETED FROM EMP AND INSERTED TO TEMP');
COMMIT;
ELSIF DESIG='MANAGER' THEN
UPDATE EMP SET SAL=SALARY+200 WHERE EMPNO=ENO;
DBMS_OUTPUT.PUT_LINE('INCREMENTED SALARY IS '||TO_CHAR(SALARY+200));
END IF;
END;
DECLARE
MDEPNO NUMBER(3);
BEGIN
FOR I IN 1..4 LOOP
SELECT MAX(DEPTNO) INTO MDEPNO FROM DEPT5;
103
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
9. 2 tables are created. In one table the quantiy and the product name
is stored and the other table is dummy table. If we give product name
the details will be displayed.If the number of products is below 5 then
insert into temp table out of stock and sysdate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~
inventory table (inv)
~~~~~~~~~~~~~~~
quantity prodname
9 Racket
8 Book
5 Pencil
1 Marker
temp
~~~~
prodname comment date1
declare
qty NUMBER(5);
pname VARCHAR2(10);
begin
select quantity,prodname into qty,pname from inv where
prodname='&productname';
if qty>5 then
DBMS_OUTPUT.PUT_LINE('THANK U FOR THE PURCHASES MADE VISIT AGAIN');
update inv set quantity=quantity-1 where prodname=pname;
commit;
else
DBMS_OUTPUT.PUT_LINE('STOCK LEVEL IS BELOW ORDER LEVEL');
insert into temp values(pname,'out of stock',sysdate);
commit;
end if;
end;
declare
a constant number:=5;
begin
dbms_output.put_Line(a);
dbms_output.put_Line(a+2);
end;
declare
a number default 3;
begin
dbms_output.put_Line(a);
end;
(hERE A CAN BE USED AS ASSIGNMENT TARGET).
12. Write a pl/sql block to fetch the details of all the employees
whose
number is 7902. Now if his sal is less than 4000 then fetch salary
mgr and name of the employees whose empno should be equal to
MGR.
13. WAP TO CHECK THE BANK DETAILS. IN BANK TABLE ACCNO AND BALANCE
IS STORED. USER HAS TO ENTER THE ACCOUNT NUMBER AND WITHDRAW
AMOUNT.
IF WITHDRAWL AMOUNT < BALANCE THEN UPDATE BALANCE OF BANK TABLE
AND SUBTRACT THE WITHDRAWL AMOUNT FROM BALANCE ELSE INSERT A
MESSAGE LIKE INSUFFICIENT FUNDS TO TEMP TABLE
ACCNO BAL
-------- ---------
10 5000
20 6000
30 20
declare
awith number(3):=&awith;
ac_no number(3):=&accno;
abal number(6);
105
begin
select bal into abal from bank where accno=ac_no;
if abal>awith then
update bank set bal=bal-awith where accno=ac_no;
commit;
else
insert into temp values(ac_no,abal,'insuffunds');
commit;
end if;
end;
15.ILLUSTRATE %ROWTYPE
***********************
declare
a emp%ROWtype;
begin
select * into a from emp where empno=&N;
dbms_output.put_line(A.ENAME||' '||A.SAL||' '||A.COMM);
end;
16
write a pl/sql block to calculate the sum and average salaries of
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
all the employees of a particular department.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DECLARE
NO NUMBER:=&DEPTNO;
SUM1 NUMBER(7,2);
AVG1 NUMBER(7,2);
BEGIN
SELECT SUM(SAL),AVG(SAL) INTO SUM1,AVG1
FROM EMP
WHERE DEPTNO=NO;
DBMS_OUTPUT.PUT_line('Sum of salary is'||SUM1);
DBMS_OUTPUT.PUT_Line('Average salary is'||AVG1);
END;
106
NESTED TABLE
CREATE OR REPLACE TYPE EMPLOYEE AS OBJECT
_ CREATE TYPE EMPLOYEE AS OBJECT
(NO NUMBER(4),
NAME VARCHAR2(20),
JOB VARCHAR2(10),
SAL NUMBER(7,2));
-------------------------------------------
-------------------------------------------
---------------------------------------------
-------------------------------------------------------------
-----------------------------------------------------
SELECT TYPE_CODE,ATTRIBUTES FROM USER_TYPES WHERE TYPE_NAME='DEPTNEST';
------------------------------------------------------------------------
-------------------------
VARRAY:
-------------
CREATE TYPE PHONE AS OBJECT
(NO NUMBER(6));
----------
CREATE TYPE PHONETYPE AS VARRAY(5) OF PHONE;
----------
CREATE TABLE CUST
(NAME VARCHAR2(20),
PHONENO PHONETYPE);
-----------
INSERT INTO CUST VALUES ('XXXX',
PHONETYPE(PHONE(232322),PHONE(345345));
-----------
------------------------------------------------------------------------
----------------------
---------------------------------------------------------------
108
#**************#
PROCEDURES
#**************#
#**************##**************##**************##**************##*******
*******#
#**************##**************##**************##**************##*******
*******#
EXEC LINE
OR
DECLARE
CURSOR C1 IS SELECT * FROM EMP WHERE JOB='CLERK';
CURSOR C2 IS SELECT * FROM EMP WHERE JOB='MANAGER';
BEGIN
LINE;
LINE;
FOR I IN C1 LOOP
DBMS_OUTPUT.PUT_LINE(I.ENAME||' '||I.JOB);
END LOOP;
LINE;
LINE;
FOR I IN C2 LOOP
DBMS_OUTPUT.PUT_LINE(I.ENAME||' '||I.JOB);
END LOOP;
LINE;
LINE;
END;
________________________________________________________________________
__
109
declare
A NUMBER:=25;
begin
PRINT(A);
print('BRAINWARE');
end;
________________________________________________________________________
EXEC DISP(7499)
DECLARE
BEGIN
DISP(&N);
END;
OR
DECLARE
NAME1 VARCHAR2(30);
DESIG1 VARCHAR2(10);
N NUMBER:=&EMPNO;
BEGIN
DISP1(N,NAME1,DESIG1);
DBMS_OUTPUT.PUT_LINE(NAME1||' '||DESIG1||' '||N);
110
END;
________________________________________________________________________
__
declare
n number:=&empno;
incre number:=&increment;
begin
dbms_output.put_line('n = '|| n||' '||' increment='||incre);
hike_sal(n,incre);
dbms_output.put_line('n = '|| n||' '||' increment='||incre);
if n=1 then
dbms_output.put_line('salary incremented');
else
dbms_output.put_line('salary is not incremented');
end if;
end;
______________________________________________________________________
DECLARE
A NUMBER:=10;
J VARCHAR2(10);
ENO NUMBER;
PROCEDURE DISPJOB(N NUMBER,J OUT VARCHAR2) IS
BEGIN
SELECT JOB INTO J FROM EMP WHERE EMPNO=N;
END;
BEGIN
ENO:=&EMPNO;
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NUMBER IS'||ENO);
DISPJOB(ENO,J);
DBMS_OUTPUT.PUT_lINE('EMPNO='||ENO||' JOB='||J);
111
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('MESSAGE');
PRINT('SORRRY NO EMPLOYEE EXISTS WITH THAT NUMBER');
END;
_________________________________
______________________________________
CURSORS
-------
DECLARE
CURSOR C1 IS SELECT ENAME,COMM FROM EMP WHERE EMPNO=7900;
NAME EMP.ENAME%TYPE;
COMMISSION EMP.COMM%TYPE;
BEGIN
OPEN C1;
FETCH C1 INTO NAME,COMMISSION;
DBMS_OUTPUT.PUT_LINE('THE VALUES ARE'||' '||NAME||' '||COMMISSION);
CLOSE C1;
END;
DECLARE
CURSOR C1 IS SELECT EMPNO,ENAME ,DEPTNO FROM EMP;
EMPNUM EMP.EMPNO%TYPE;
EMPNAME EMP.ENAME%TYPE;
DEPTNUM EMP.DEPTNO%TYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO EMPNUM,EMPNAME,DEPTNUM;
if c1%notfound then
exit;
else
dbms_output.put_line(EMPNUM||' '||EMPNAME||' '||DEPTNUM);
end if;
END LOOP;
end;
DECLARE
CURSOR C1 IS SELECT EMPNO,ENAME ,SAL FROM EMP WHERE DEPTNO=10;
EMPNUM EMP.EMPNO%TYPE;
NAME EMP.ENAME%TYPE;
SALARY EMP.SAL%TYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO EMPNUM,NAME,SALARY;
IF C1%NOTFOUND THEN EXIT;
END IF;
INSERT INTO TEMP VALUES(EMPNUM,NAME,SALARY);
END LOOP;
--CLOSE C1;
112
END;
DECLARE
CURSOR C1 IS SELECT EMPNO,SAL,COMM FROM EMP;
EMPCODE EMP.EMPNO%TYPE;
SALARY EMP.SAL%TYPE;
COM EMP.COMM%TYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO EMPCODE,SALARY,COM;
EXIT WHEN C1%NOTFOUND;
INSERT INTO TEMP VALUES(EMPCODE,SALARY,COM);
END LOOP;
CLOSE C1;
END;
////////(ISOPEN WITH CLOSE)
DECLARE
CURSOR C1 IS SELECT EMPNO,SAL,COMM FROM EMP;
EMPCODE EMP.EMPNO%TYPE;
SALARY EMP.SAL%TYPE;
COM EMP.COMM%TYPE;
BEGIN
LOOP
IF C1%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('CURSOR IS OPEN');
EXIT;
ELSE
OPEN C1;
FETCH C1 INTO EMPCODE,SALARY,COM;
EXIT WHEN C1%NOTFOUND;
INSERT INTO TEMP VALUES(EMPCODE,SALARY,COM);
END IF;
113
END LOOP;
CLOSE C1;
END;
DECLARE
CURSOR C1 IS SELECT EMPNO,ENAME,COMM FROM EMP;
BEGIN
--OPEN C1;
FOR I IN C1
LOOP
DBMS_OUTPUT.PUT_LINE(I.EMPNO||' '||I.ENAME||' '||I.COMM);
INSERT INTO TEMP VALUES(I.EMPNO,I.ENAME,I.COMM);
END LOOP;
--CLOSE C1;
END;
%ROWTYPE
--------
DECLARE
CURSOR C1 IS SELECT * FROM EMP;
TEMPROW EMP%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO TEMPROW;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(TEMPROW.EMPNO||' '||TEMPROW.SAL);
END LOOP;
CLOSE C1;
DECLARE
CURSOR C1 IS SELECT EMPNO,ENAME,SAL FROM EMP;
TEMPROW C1%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO TEMPROW;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(TEMPROW.EMPNO||' '||TEMPROW.SAL);
END LOOP;
CLOSE C1;
END;
END;
IMPLICIT CURSOR
----------------
BEGIN
UPDATE EMP SET HIREDATE='01-SEP-98' WHERE DEPTNO=20;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT||'ROW UPDATED');
END;
ROWCOUNT
--------
114
EXCEPTION;
----------
(ZERO_DIVIDE)
------------
DECLARE
A NUMBER:=&A;
B NUMBER:=&B;
C NUMBER;
BEGIN
C:=A/B;
DBMS_OUTPUT.PUT_LINE(C);
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('THE DENOMINATOR IS ZERO');
B:=A;
C:=A/B;
DBMS_OUTPUT.PUT_LINE(C);
END;
VALUE error;
---------------
DECLARE
EMPNUM NUMBER(3);
NAME VARCHAR2(10);
NO NUMBER:=&NO;
BEGIN
SELECT EMPNO,ENAME INTO EMPNUM,NAME FROM EMP WHERE EMPNO=NO;
DBMS_OUTPUT.PUT_LINE(EMPNUM||' '||NAME);
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('WIDTH IS NOT SUFFICIENT');
DECLARE
ENUM EMP.EMPNO%TYPE;
115
BEGIN
SELECT EMPNO,ENAME INTO ENUM,NAME FROM EMP WHERE EMPNO=NO ;
DBMS_OUTPUT.PUT_LINE(ENUM||' '||NAME);
END;
END;
INVALID NUMBER;
---------------
DECLARE
NAME VARCHAR2(10):='&NAME';
NUM NUMBER(3);
BEGIN
SELECT TO_NUMBER(NAME) INTO NUM FROM DUAL;
DBMS_OUTPUT.PUT_LINE(NUM);
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('INVALID NUMBER IS RAISED');
END;
NO_DATA_FOUND
-------------
DECLARE
NO EMP.EMPNO%TYPE;
NAME EMP.ENAME%TYPE;
SALARY EMP.SAL%TYPE;
DESIG EMP.JOB%TYPE;
BEGIN
SELECT EMPNO,ENAME,SAL,JOB INTO NO,NAME,SALARY,DESIG
FROM EMP WHERE EMPNO=&EMPNO;
IF SQL%FOUND THEN
116
CURSOR_ALREADY_OPEN
--------------------
DECLARE
CURSOR C1 IS SELECT * FROM EMP ;
A C1%ROWTYPE;
BEGIN
open c1;
LOOP
OPEN C1;
FETCH C1 INTO A;
EXIT WHEN C1%NOtFOUND;
DBMS_OUTPUT.PUT_LINE(a.sal||' '||a.ename);
END LOOP;
CLOSE C1;
exception
when cursor_already_open then
dbms_output.put_line('cursor already openED');
END;
WHEN OTHERS
117
------------
DESC TEMP
A VARCHAR2(10)
B VARCHAR2(25)
C VARCHAR2(25)
DECLARE
ENO NUMBER(4);
NAME VARCHAR2(10);
DEPNO NUMBER(3);
ACODE NUMBER(4);
BMSG VARCHAR2(40);
EMPNO NUMBER(4);
BEGIN
SELECT EMPNO,ENAME,DEPTNO INTO ENO,NAME,DEPNO FROM EMP
WHERE empno=&empno;
DBMS_OUTPUT.PUT_LINE(ENO||' '||NAME||' '||DEPNO);
EXCEPTION
WHEN OTHERS THEN
ACODE:=SQLCODE;
BMSG:=SQLERRM;
DBMS_OUTPUT.PUT_LINE('ACODE:'||' '||ACODE||' '||BMSG);
--INSERT INTO temp VALUES(ACODE,BMSG ,'asds');
END;
DUP_VAL_ON_INDEX
-----------------
create table temp(a number(3) primary key,
b varchar2(10),c varchar2(10));
declare
dno dept.deptno%type;
dname dept.dname%type;
loc dept.loc%type;
begin
insert into DEPT values(&deptno,'&dname','&loc');
exception
when dup_val_on_index then
dbms_output.put_line('DUP_VALUES_ON_INDEX');
end;
USER_DEFINED_EXCEPTION
----------------------
DECLARE
SALARY EMP.SAL%TYPE;
COM EMP.COMM %TYPE;
MIS_COMM EXCEPTION;
NETSAL NUMBER(7);
BEGIN
SELECT SAL,COMM INTO SALARY,COM FROM EMP WHERE EMPNO=&N;
DBMS_OUTPUT.PUT_LINE(SALARY||' '||COM);
IF COM IS NULL THEN
118
RAISE MIS_COMM;
ELSE
NETSAL:=SALARY+COM;
DBMS_OUTPUT.PUT_LINE(SALARY||' '||COM||' '||NETSAL);
END IF;
EXCEPTION
WHEN MIS_COMM THEN
DBMS_OUTPUT.PUT_lINE('ORA421'||' '||'MISS_COMM EXCEPTION');
END;
PARAMETER(ERROR)
----------
DECLARE
I EMP%ROWTYPE;
CURSOR C1(TEMPENO NUMBER) IS
SELECT * FROM EMP WHERE EMPNO=TEMPENO;
BEGIN
OPEN C1(7499);
FETCH C1 INTO I ;
INSERT INTO EMP(EMPNO,ENAME,DEPTNO) VALUES(I.EMPNO,I.ENAME,I.DEPTNO);
--EXIT WHEN C1%NOT FOUND;
--END LOOP;
END;
END LOOP;
CLOSE T2;
END;
(ERROR)
CREATE OR REPLACE TRIGGER T1 BEFORE INSERT ON EMP
DECLARE
A VARCHAR2(10);
BEGIN
SELECT TO_CHAR(SYSDATE,'DAY') INTO A FROM DUAL;
IF A ='TUESDAY' THEN
RAISE_APPLICATION_ERROR(-20909,'INSERTION NOT POSSIBLE');
END IF;
END;
(FIND T1 OR T2 )(OK)
CREATE OR REPLACE TRIGGER T2 BEFORE INSERT ON EMP FOR EACH ROW
DECLARE
A NUMBER;
BEGIN
SELECT COUNT(*) INTO A FROM EMP WHERE DEPTNO=:NEW.DEPTNO;
IF A>=1 THEN
RAISE_APPLICATION_ERROR(-20101,'PRIMARY KEY VIOLATION');
END IF;
END;
BEGIN
SELECT COUNT(DEPTNO) INTO A FROM DEPT
WHERE DEPTNO=:NEW.DEPTNO;
IF A>=1 THEN
RAISE_APPLICATION_ERROR(-20001,'MASTER EXIST');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('-20001 NO MASTER RECORD');
END;
(MUTATING ERROR)
CREATE OR REPLACE TRIGGER T3 AFTER UPDATE ON EMP FOR EACH ROW
DECLARE
A NUMBER(1);
BEGIN
SELECT COUNT(*) INTO A FROM EMP WHERE DEPTNO=:old.DEPTNO;
dbms_output.put_line('new.depttno'||' '||:old.deptno);
IF A>1 THEN
RAISE_APPLICATION_ERROR(-20001,'UPDATED');
END IF;
END ;
NESTED TABLES:
--------------
SELECT X.NAME FROM THE (SELECT EMPL FROM DEPTNEST WHERE DNO=10) X
WHERE X.NAME='YYYY';
SUBPROGRAMS
1. PROCEDURE Syntax:
EXECUTABLE STATEMENTS
EXCEPTION HANDLERS
END;
Eg.:
EXEC P1(7499,1000);
1.1 IN PARAMETER
EXEC P2(10);
DECLARE
INNER NUMBER;
A NUMBER;
BEGIN
P3 (7499,A);
122
DECLARE
A NUMBER;
B NUMBER;
BEGIN
SAMPLE(10,B);
DBMS_OUTPUT.PUT_LINE('B VALUE IS'||' '||B);
END;
FUNCTIONS
---------
<CREATE OR REPLACE FUNCTION NAME> {(ARGUMENT) - IN ,OUT ,IN OUT}
RETURN DATATYPE IS >
DECLARATION
BEGIN
EXECUTABLE STATEMENTS
EXCEPTION
EXCEPTION HANDLERS
END;
DECLARE
B NUMBER;
BEGIN
B:=F1;
DBMS_OUTPUT.PUT_LINE(B);
END;
TO EXECUTE IT:
DECLARE
ID NUMBER;
AMOUNT NUMBER;
REMARK BOOLEAN;
BEGIN
ID:=&A;
AMOUNT:=&B;
REMARK:=FUN(ID,AMOUNT);
IF REMARK=TRUE THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;
END;
IN PARAMETER
-------------
CREATE OR REPLACE FUNCTION F1(NAME IN VARCHAR2) RETURN NUMBER IS
A EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO A FROM EMP WHERE ENAME=NAME;
RETURN A;
END;
DECLARE
B NUMBER;
BEGIN
B:=F1('&A');
DBMS_OUTPUT.PUT_LINE(B);
END;
IN AND OUT
----------
CREATE OR REPLACE FUNCTION F1(NAME IN VARCHAR2,B OUT VARCHAR2) RETURN
NUMBER IS
A EMP.SAL%TYPE;
BEGIN
SELECT SAL,JOB INTO A,B FROM EMP WHERE ENAME=NAME;
RETURN A;
END;
DECLARE
124
A NUMBER;
JOB VARCHAR2(20);
BEGIN
A:=F1('&c',JOB);
DBMS_OUTPUT.PUT_LINE(A||' '||JOB);
END;
IN OUT
------
CREATE OR REPLACE function F1(A IN number,c OUT NUMBER) RETURN NUMBER
IS
B EMP.COMM%TYPE;
BEGIN
SELECT SAL,COMM INTO sal1,comm1 FROM EMP WHERE EMPNO=A;
RETURN sal1;
END;
/* ERROR */
DECLARE
M2 NUMBER(5);
M3 NUMBER;
M1 NUMBER;
SALARY NUMBER;
BEGIN
M1:=F1(&M2,M3);
DBMS_OUTPUT.PUT_LINE(M2||' '||SALARY);
END;
PACKAGE SPECIATION
-------------------
CREATE OR REPLACE PACKAGE PACK1 IS
PROCEDURE P1;
FUNCTION F1 RETURN NUMBER;
A NUMBER;
END PACK1;
DECLARE
N NUMBER;
BEGIN
N:=PACK1.F1;
DBMS_OUTPUT.PUT_LINE(N);
PACK1.P1;
125
END;
/*
CREATE OR REPLACE PACKAGE PACK2 IS
PROCEDURE DDEP;
FUNCTION BALANCE RETURN NUMBER;
NAME VARCHAR2(15);
END PACK2;
DECLARE
TYPE T1 IS TABLE OF EMP.sal %TYPE
INDEX BY BINARY_INTEGER;
M BINARY_INTEGER :=1;
T2 T1;
BEGIN
FOR I IN 1..20
LOOP
T2(M) :=I*10;
DBMS_OUTPUT.PUT_LINE(T2(M)||' '||m);
M:=M+1;
END LOOP;
END;
DECLARE
TYPE T1 IS TABLE OF EMP.ENAME%TYPE
INDEX BY BINARY_INTEGER;
M BINARY_INTEGER:=0;
T2 T1;
BEGIN
FOR I IN (SELECT ENAME FROM EMP )
LOOP
T2(M):=I.ENAME;
--INSERT INTO DEPT1(DNAME) VALUES(T2(M));
dbms_output.put_line(t2(m)||' '||m);
126
M:=M+1;
END LOOP;
END;
DECLARE
TYPE T1 IS TABLE OF EMP.ENAME%TYPE
INDEX BY BINARY_INTEGER;
M BINARY_INTEGER :=1;
T2 T1;
T3 T1;
BEGIN
FOR I IN(SELECT ENAME FROM EMP)
LOOP
T2(M) :=I.ENAME;
T3(M):=T2;
DELETE FROM EMP WHERE ENAME=T2(M);
DBMS_OUTPUT.PUT_LINE(T3(M));
M:=M+1;
END LOOP;
END;
DECLARE
TYPE T1 IS TABLE OF DEPT%ROWTYPE
INDEX BY BINARY_INTEGER;
T2 T1;
M BINARY_INTEGER :=1;
BEGIN
FOR I IN (SELECT * FROM DEPT)
LOOP
T2(M).DEPTNO :=I.DEPTNO;
T2(M).DNAME :=I.DNAME;
T2(M).LOC :=I.LOC;
DBMS_OUTPUT.PUT_LINE(T2(M).DEPTNO||' '||T2(M).DNAME||' '||T2(M).LOC);
M :=M+1;
END LOOP;
END;
--RECORD(ONE ROW)
DECLARE
TYPE T1 IS RECORD
(A NUMBER(3),B VARCHAR2(30),C VARCHAR2(30));
T2 T1;
BEGIN
SELECT * INTO T2 FROM DEPT WHERE DEPTNO=10;
DBMS_OUTPUT.PUT_LINE(T2.A||T2.B||T2.C);
END;
BEGIN
SELECT * FROM EMP;
END;
-----------------------------------------
BEGIN
UPDATE EMP SET SAL=SAL+2000;
127
END;
-----------------------------------------
DECLARE
I NUMBER :=7788;
J NUMBER :=10000;
BEGIN
UPDATE EMP SET SAL=J WHERE EMPNO=I;
END;
-----------------------------------------
DECLARE
I NUMBER:=&I;
J NUMBER:=&J;
BEGIN
UPDATE EMP SET SAL=J WHERE EMPNO=I;
------------------------------------------
DECLARE
I NUMBER;
J NUMBER;
BEGIN
UPDATE EMP SET SAL=&J WHERE EMPNO=&I;
END;
------------------------------------------
FOR LOOP
--------
-------------------------------------------
declare
begin
FOR I IN 1..5
LOOP
INSERT INTO TEMP VALUES (I);
END LOOP;
end;
-------------------------------------------
WHILE LOOP
----------
WHILE <CONDITION>
LOOP
-
128
-
-
END LOOP;
-------------------------------------------
DECLARE
A NUMBER(2);
BEGIN
A:=&A;
FOR COUNT IN 1..A
LOOP
INSERT INTO TEMP VALUES (COUNT);
END LOOP;
END;
-------------------------------------------
DECLARE
I NUMBER:=10;
BEGIN
WHILE I <=15
LOOP
INSERT INTO TEMP VALUES (I);
I:=I+1;
END LOOP;
END;
-------------------------------------------
DECLARE
I NUMBER:=1;
BEGIN
WHILE I<=10
LOOP
INSERT INTO TEMP VALUES(I,'XXXX');
IF I>5 THEN
EXIT;
END IF;
I:=I+1;
END LOOP;
INSERT INTO TEMP VALUES(I,'YYYY');
END;
-------------------------------------------
GO TO STATEMENT
---------------
DECLARE
I NUMBER:=1;
BEGIN
WHILE I<=10
LOOP
INSERT INTO TEMP VALUES(I,'XXXX');
IF I=5 THEN
GOTO PROC;
END IF;
129
I:=I+1;
END LOOP;
<<PROC>>
INSERT INTO TEMP VALUES(I,'PROC');
END;
------------------------------------------------------------
DECLARE
TEMP NUMBER: = &TEMPNO;
TEMPSAL NUMBER;
BEGIN
SELECT SAL INTO TEMPSAL FROM EMP WHERE EMPNO=TEMP;
DBMS_OUTPUT.PUT_LINE(TEMPSAL);
END;
-------------------------------------------------------------------
DECLARE
TEMPEMPNO NUMBER:=&TEMPEMPNO;
TEMPHIREDATE DATE;
TEMPNAME VARCHAR2(20);
BEGIN
SELECT HIREDATE,ENAME INTO TEMPHIREDATE,TEMPNAME FROM EMP
WHERE EMPNO=TEMPEMPNO;
IF TEMPHIREDATE >='01-JAN-80' AND TEMPHIREDATE <='30-JAN-85'
THEN
UPDATE EMP SET COMM=COMM+COMM*0.1 WHERE EMPNO=TEMPEMPNO;
END IF;
END;
-------------------------------------------------------------------
DECLARE
TEMPEMPNO NUMBER:=&TEMPEMPNO;
TEMPDEPTNO NUMBER;
BEGIN
130
-------------------------------------------------------------------
DECLARE
A NUMBER;
B NUMBER;
C NUMBER;
BEGIN
A:=&A;
B:=&B;
C:=A+B;
DBMS_OUTPUT.PUT_LINE(C);
END;
------------------------------------------------------------------------
-
%TYPE
--------
DECLARE
SALARY EMP.SAL%TYPE;
DOJ EMP.HIREDATE%TYPE;
BEGIN
SELECT SAL,HIREDATE INTO SALARY,DOJ FROM EMP WHERE EMPNO=7900;
DBMS_OUTPUT.PUT_LINE(SALARY||' '||DOJ);
END;
------------------------------------------------------------------------