0% found this document useful (0 votes)
66 views

PLSQL

The document contains examples of using PL/SQL to: 1) Print output using DBMS_OUTPUT.PUT_LINE and concatenating strings with the || operator. 2) Declare variables of different data types and perform operations like addition on numbers. 3) Handle exceptions using exception handling. 4) Use constants and demonstrate that constants cannot be reassigned. 5) Use loops like WHILE and FOR to iterate through values and print output.

Uploaded by

Tummala Navya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

PLSQL

The document contains examples of using PL/SQL to: 1) Print output using DBMS_OUTPUT.PUT_LINE and concatenating strings with the || operator. 2) Declare variables of different data types and perform operations like addition on numbers. 3) Handle exceptions using exception handling. 4) Use constants and demonstrate that constants cannot be reassigned. 5) Use loops like WHILE and FOR to iterate through values and print output.

Uploaded by

Tummala Navya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PLSQL – 16 AUG 2021:

SQL> SET SERVEROUTPUT ON;

SQL> BEGIN
2 DBMS_OUTPUT.put_line('HELLO CG');
3 END;
4 /
HELLO CG
PL/SQL procedure successfully completed.

SQL> /
HELLO CG
PL/SQL procedure successfully completed.

SQL> EDIT;
Wrote file afiedt.buf

SQL> declare
2 l_name varchar2(25) := 'NAVYA';
3 BEGIN
4 DBMS_OUTPUT.put_line('HELLO' || l_name);
5 end;
6 /
HELLONAVYA
PL/SQL procedure successfully completed.

SQL> declare
2 l_name varchar2(25) := 'NAVYA';
3 BEGIN
4 DBMS_OUTPUT.put_line('HELLO ' || l_name);
5 end;
6 /
HELLO NAVYA
PL/SQL procedure successfully completed.

SQL> declare
2 l_number number(5) := '5';
3 l_number number(5) := '20';
4 BEGIN
5 DBMS_OUTPUT.put_line('addition');
6 end;
7 /
addition
PL/SQL procedure successfully completed.

Addition of 2 numbers:
SQL> declare
2 x number(5);
3 y number(5);
4 z number(5);
5 begin
6 x:=6;
7 y:=10;
8 z:=x+y;
9 DBMS_OUTPUT.put_line('addition' || z);
10 end;
11 /
addition16
PL/SQL procedure successfully completed.

SQL> declare
2 n1 number :=15;
3 n2 number :=25;
4 begin
5 dbms_output.put_line(n1+n2);
6
7 exception
8 when zero_divide then
9 dbms_output.put_line('sorry');
10 end;
11 /
40
PL/SQL procedure successfully completed.

SQL> declare
2 n1 number :=15;
3 n2 number :=0;
4 begin
5 dbms_output.put_line(n1/n2);
6 exception
7 when zero_divide then
8 dbms_output.put_line('sorry cant display');
9 end;
10 /
sorry cant display
PL/SQL procedure successfully completed.

SQL> edit;
Wrote file afiedt.buf

SQL> declare
2 n1 number not null := 10;
3 begin
4 dbms_output.put_line('hiii');
5 end;
6 /
hiii
PL/SQL procedure successfully completed.

SQL> declare
2 n1 number not null := 10;
3 begin
4 n1 :=0;
5 dbms_output.put_line('hello');
6 end;
7 /
hello
PL/SQL procedure successfully completed.

SQL> declare
2 var1 varchar2(15):= 'Hello Hi';
3 begin
4 var1:= '';
5 dbms_output.put_line(var1);
6 end;
7 /
PL/SQL procedure successfully completed.

CONSTANT:
WITH:
SQL> declare
2 counter CONSTANT number := 27;
3 begin
4 counter := counter + 1;
5 dbms_output.put_line(counter);
6 end;
7 /
counter := counter + 1;
*
ERROR at line 4:
ORA-06550: line 4, column 1:
PLS-00363: expression 'COUNTER' cannot be used as an assignment target
ORA-06550: line 4, column 1:
PL/SQL: Statement ignored

WITHOUT:
SQL> declare
2 counter number := 27;
3 begin
4 counter := counter + 1;
5 dbms_output.put_line(counter);
6 end;
7 /
28

PL/SQL procedure successfully completed.

Using If conditions:
SQL> DECLARE
2 i number := 1;
3 begin
4 LOOP
5 dbms_output.put_line(i);
6 i := i + 1;
7 EXIT when i=10;
8 END LOOP;
9 END;
10 /
1
2
3
4
5
6
7
8
9
PL/SQL procedure successfully completed.

SQL> declare
2 i number := 1;
3 begin
4 dbms_output.put_line('inside begin');
5 while i>=10
6
7 loop
8 dbms_output.put_line('loop');
9 dbms_output.put_line('i');
10 i := i+1;
11 end loop;
12 end;
13 /
inside begin
PL/SQL procedure successfully completed.

To print even numbers:


SQL> begin
2 for i in 1..20
3 loop
4
5 continue when mod(i, 2)=1;
6 dbms_output.put_line(i);
7 end loop;
8 end;
9 /

Op:
2
4
6
8
10
12
14
16
18
20
PL/SQL procedure successfully completed.

You might also like