0% found this document useful (0 votes)
4 views6 pages

PL/SQL Code Examples and Errors

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

PL/SQL Code Examples and Errors

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

SQL> DECLARE

2 a integer :=10;
3 b integer := 20;
4 c integer;
5 f real;
6 BEGin
7 c:=a+b;
8 dbms_output.put_line('Value of c:'||c);
9 f:70.0/3.0;
10 dbms_output.put_line('Value of f:'||f);
11 END;
12 /
f:70.0/3.0;
*
ERROR at line 9:
ORA-06550: line 9, column 2:
PLS-00103: Encountered the symbol "" when expecting one of the following:
:= . ( @ % ;
The symbol ":=" was substituted for "" to continue.

SQL> set serveroutput on;


SQL> DECLARE
2 a integer :=10;
3 b integer := 20;
4 c integer;
5 f real;
6 BEGin
7 c:=a+b;
8 dbms_output.put_line('Value of c:'||c);
9 f:=70.0/3.0;
10 dbms_output.put_line('Value of f:'||f);
11 END;
12 /
Value of c:30
Value of f:23.33333333333333333333333333333333333333

PL/SQL procedure successfully completed.

SQL> declare
2 x number(11,2):=10;
3 begin
4 dbms_output.put_line(x);
5 end;
6 /
10

PL/SQL procedure successfully completed.

SQL> create or replace function myfn return varchar2 is


2 v_dname varchar2(20);
3 begin
4 select dname
5 into v_name
6 from dept
7 where deptno=&p_deptno;
8 return v_name;
9 end;
10 /
Enter value for p_deptno: 20
old 7: where deptno=&p_deptno;
new 7: where deptno=20;

Warning: Function created with compilation errors.

SQL> declare
2 grade CHAR(1);
3 BEGin
4 gradeP:='B'
5 CASE grade
6 WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('EXCELLENT');
7 WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('VERY GOOD');
8 WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('GOOD');
9 WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('FAIR');
10 WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('POOR');
11 ELSE THEN DBMS_OUTPUT.PUT_LINE('no such grade');
12 END CASE;
13 END;
14 /
CASE grade
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "CASE" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
The symbol "*" was substituted for "CASE" to continue.
ORA-06550: line 6, column 48:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 7, column 48:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 8, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 9, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 10, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
SQL> declare
2 grade CHAR(1);
3 BEGin
4 gradeP:='B'
5 CASE grade
6 WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('EXCELLENT');
7 WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('VERY GOOD');
8 WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('GOOD');
9 WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('FAIR');
10 WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('POOR');
11 ELSE DBMS_OUTPUT.PUT_LINE('no such grade');
12 END CASE;
13 END;
14 /
CASE grade
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "CASE" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
The symbol "*" was substituted for "CASE" to continue.
ORA-06550: line 6, column 48:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 7, column 48:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 8, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 9, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 10, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
like2 like4 likec between || multiset member submultiset
The symbol ";" was ignored.
ORA-06550: line 11, column 43:
PLS-00103: Encountered the symbol ";" when expecting one of the follow

SQL> declare
2 grade CHAR(1);
3 BEGin
4 grade:='B';
5 CASE grade
6 WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('EXCELLENT');
7 WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('VERY GOOD');
8 WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('GOOD');
9 WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('FAIR');
10 WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('POOR');
11 ELSE DBMS_OUTPUT.PUT_LINE('no such grade');
12 END CASE;
13 END;
14 /
VERY GOOD

PL/SQL procedure successfully completed.

SQL> declare
2 p number:=0;
3 begin
4 for k in 1..500 loop --calculate pi for 500 terms
5 p:=p+(((-1)**(k+1))/((2*k)-1));
6 end loop;
7 p:=4*p;
8 dbmsoutput.put_line('pi is approximately : '||p);--print result
9 end;
10 /
dbmsoutput.put_line('pi is approximately : '||p);--print result
*
ERROR at line 8:
ORA-06550: line 8, column 1:
PLS-00201: identifier 'DBMSOUTPUT.PUT_LINE' must be declared
ORA-06550: line 8, column 1:
PL/SQL: Statement ignored

SQL> declare
2 p number:=0;
3 begin
4 for k in 1..500 loop --calculate pi for 500 terms
5 p:=p+(((-1)**(k+1))/((2*k)-1));
6 end loop;
7 p:=4*p;
8 dbms_output.put_line('pi is approximately : '||p);--print result
9 end;
10 /
pi is approximately : 3.13959265558978323858464061338053947907

PL/SQL procedure successfully completed.

SQL> declare
2 cursor empcursor is
3 select * from emp;
4 v_empdata empcursor%rowtype;
5 begin
6 open empcursor;
7 loop
8 fetch empcurso into v_empdata;
9 exit when empcursor%notfound;
10 dbms_output.put_line('Recor number: '||empcursor%rowcount||''||
v_empdata.ename);
11 end loop;
12 close empcursor;
13 end;
14 /
fetch empcurso into v_empdata;
*
ERROR at line 8:
ORA-06550: line 8, column 7:
PLS-00201: identifier 'EMPCURSO' must be declared
ORA-06550: line 8, column 1:
PL/SQL: SQL Statement ignored

SQL> declare
2 cursor empcursor is
3 select * from emp;
4 v_empdata empcursor%rowtype;
5 begin
6 open empcursor;
7 loop
8 fetch empcursor into v_empdata;
9 exit when empcursor%notfound;
10 dbms_output.put_line('Recor number: '||empcursor%rowcount||''||
v_empdata.ename);
11 end loop;
12 close empcursor;
13 end;
14 /

PL/SQL procedure successfully completed.

SQL> create or replace procedure mybonus as


2 cursor deptcursor is select deptno from dept;
3 begin
4 for r in deptcursor
5 loop
6 update dept set sal=sal*0.95 where deptno=[Link];
7 dbms_output.put_line('the bonus information is: '||[Link]);
8 end loop;
9 end mybonus;
10 /

Warning: Procedure created with compilation errors.

SQL> create or replace procedure oddnumber(num1 number,num2 number) as


2 mynum number(4);
3 begin
4 mynum:=num1;
5 while mynum<num2
6 loop
7 if mod(mynum,2)!=0 then
8 dbms_output.put_line('The odd number '||mynum);
9 end if;
10 mynum:=mynum+1;
11 end loop;
12 end;
13 /
Procedure created.

SQL> create or replace function factorial(num number) return number


2 is
3 fact number(4):=1;
4 begin
5 for myindex in reverse 1..num
6 loop
7 fact:=fact*myindex;
8 end loop;
9 return fact;
10 end;
11 /

Function created.

SQL> declare
2 v_factorial number(4):=0;
3 begin
4 v_factorial:=factorial(5);
5 dbms_output.put_line('The Factorial value : '||v_factorial);
6 end;
7 /
The Factorial value : 120

PL/SQL procedure successfully completed.

SQL> create or replace function combination(num1 number,num2 number) return number


2 as
3 combi:=factorial(num1)+factorial(num2);
4 return combi;
5 end;
6 /

Warning: Function created with compilation errors.

SQL> create or replace function empexp(v_empno number) return number as


2 v_hiredate [Link]%type;
3 v_exp number(6,2):=1;
4 begin
5 select hiredate into v_hiredate from emp where empno=v_empno;
6 v_exp:=months_between(sysdate,v_hiredate)/12;
7
8 return v_exp;
9 end;
10 /

Function created.

SQL> spool off;

You might also like