Simple Structure of PLSQL Block

 

 

Simple Structure of PLSQL Block

 

First set server output on to display output

SQL> set serveroutput on;

1.     

SQL> begin

  2  dbms_output.put_line('Hello Friends this is oracle class');

  3  end;

  4  /

Hello Friends this is oracle class

 

PL/SQL procedure successfully completed.

2.     

SQL> declare

  2  message varchar(30);

  3  Begin

  4  message:='Hello Friends how are you';

  5  dbms_output.Put_line(message);

  6  end;

  7  /

Hello Friends how are you

 

PL/SQL procedure successfully completed.

3.     

SQL> declare

  2  message1 varchar(30);

  3  message2 varchar(30);

 4  Begin

5  message1:='Hello Friends';

 6  message2:='How are you';

7  dbms_output.Put_line(message1 || message2);

 8  end;

9  /

Hello FriendsHow are you

 

PL/SQL procedure successfully completed.

 

4.     

SQL> declare

  2  message1 varchar(30);

  3  message2 varchar(30);

  4  Begin

  5  message1:='Hello Friends';

  6  message2:='How are you';

  7  dbms_output.Put_line(message1 || ' ' || message2);

  8  end;

  9  /

Hello Friends How are you

 

PL/SQL procedure successfully completed.

 

5.     

SQL> declare

  2  number1 number(4);

  3  number2 number(4);

  4  result number(4);

  5  begin

  6  number1:=10;

  7  number2:=20;

  8  result:=number1+number2;

  9  dbms_output.put_line('The addition of number1 and number2 is:' || ' '|| result);

 10  end;

 11  /

The addition of number1 and number2 is: 30

 

PL/SQL procedure successfully completed.

 

6.     

SQL> declare

  2  n1 number(4);

  3  n2 number(4);

  4  result number(4);

  5  begin

  6  n1:=20;

  7  n2:=30;

  8  result:=n1+n2;

  9  dbms_output.put_line('The addition of ' || n1 || ' and ' || n2 || ' is ' ||result);

 10  end;

 11  /

The addition of 20 and 30 is 50

PL/SQL procedure successfully completed.

 

7.     

SQL> DECLARE

  2     n1 NUMBER := 10;

  3     n2 NUMBER := 20;

  4     result NUMBER;

  5  BEGIN

  6     result := n1 + n2;

  7     DBMS_OUTPUT.PUT_LINE('The addition of ' || n1 || ' and ' || n2 || ' is ' || result);

  8  END;

  9  /

The addition of 10 and 20 is 30

 

PL/SQL procedure successfully completed.

 

8.     

SQL> declare

  2  fname varchar(20);

  3  mname varchar(20);

  4  lname varchar(20);

  5  fullname varchar(50);

  6  begin

  7  fname:='Manisha';

  8  mname:='MMM';

  9  lname:='More';

 10  fullname:=fname ||' '|| mname || ' ' ||lname;

 11  dbms_output.Put_line('My name is:' || ' ' || fullname);

 12  end;

 13  /

 

My name is: Manisha MMM More

 

PL/SQL procedure successfully completed.

 

9.     

SQL> declare

  2  fname varchar(20) :='Manisha';

  3  lname varchar(20) :='More';

  4  result varchar(30);

  5  begin

  6  result:=fname || ' ' || lname;

  7  dbms_output.put_line('My name is:' || ' ' ||result );

  8  end;

  9  /

My name is: Manisha More

 

PL/SQL procedure successfully completed.


PLSQL Block Arithmetic Operations using User Input

declare

  n1 number;

  n2 number;

    result number;

    begin

    result:=&n1+&n2;

   dbms_output.put_line('The addition is:' || result);

   end;

   /


declare

  n1 number;

  n2 number;

    result number;

    begin

    result:=&n1-&n2;

   dbms_output.put_line('The subtraction is:' || result);

   end;

   /

Output:

SQL> @C:\Users\Admin\Desktop\Oracle\userinput.sql

Enter value for n1: 20

Enter value for n2: 20

old   6:     result:=&n1+&n2;

new   6:     result:=20+20;

The addition is:40


PL/SQL procedure successfully completed.


Enter value for n1: 30

Enter value for n2: 30

old   6:     result:=&n1-&n2;

new   6:     result:=30-30;

The subtraction is:0


टिप्पणी पोस्ट करा

0 टिप्पण्या