Showing posts with label pl/sql. Show all posts
Showing posts with label pl/sql. Show all posts

Sunday, 19 February 2012

pl/sql best examples session 2

To see previous session 1



case and %type example in pl/sql

declare
 stu marks.name%type;
 mat marks.math%type;
 eng marks.english%type;
 tam marks.tamil%type;
 tot marks.tamil%type;
 per number(5,2);

begin
 select  math,english,tamil into mat,eng,tam from marks where name = 'bala';
 tot:=mat+eng+tam;
 per:=((tot/300)*100);
 case
  when per = 100 then dbms_output.put_line('Ur grade is S');
  when (per>90 and per<100) then dbms_output.put_line('Ur grade is A');
  when (per>80 and per<90) then dbms_output.put_line('Ur grade is b');
  when (per>50 and per<80) then dbms_output.put_line('Ur grade is c');
  when (per<50) then dbms_output.put_line('Ur grade is u');
 End case;
end;
/

Iteration like loop,while and for example in pl/sql
LOOP example in pl/sql

declare
            var number(10):=5;
            i number:=0;
begin
            loop
                        dbms_output.put_line(i);
                        exit when (i>5);
                        i:=i+1;
            end loop;
end;
/


while loop example in pl/sql

declare
            var number(10):=5;
            i number:=0;
begin
            while (i<5) loop
                        dbms_output.put_line(i);
                        i:=i+1;
            end loop;
end;
/                      

for loop ascending example in pl/sql


declare
            var number(10):=5;
            i number:=0;
begin
            for i in 1..10 loop
                        dbms_output.put_line(i);
            end loop;
end;                
/



for loop for finding odd and even between 1 to 10

 declare
  var number(10):=5;
  i number:=0;
 begin
  for i in 1..10 loop
   if(mod(i,2)=0) then
     NULL;
   else
     dbms_output.put_line(i||'is odd number');
   end if;
  end loop;
end;
/          

In reverse
declare
  var number(10):=5;
  i number:=0;
 begin
  for i in reverse 1..10 loop
   if(mod(i,2)=0) then
     NULL;
   else
     dbms_output.put_line(i||'is odd number');
   end if;
  end loop;
end;
/
Factorial
declare
  var number(10):=5;
  i number:=0;
temp number:=1;
 begin
  for i in reverse 1..5 loop
            temp:=temp*i;
        dbms_output.put_line(temp);
   end loop;
end;
/



%row type example in pl/sql
Declare
            Employeerow emp %rowtype;
            Deptrow dept%rowtype;
Begin
            Select   * into employeerow from emp where empno=102;
            Select   * into deptrow from dept where deptno=employeerow.deptno;
            dbms_output.put_line(‘Employee name :’|| Employeerow.ename||‘working in:’||deptrow.deptname);
end;
/

pl/sql best examples

Best links for mp3 and movie downloads

To learn UNIX basic commands click here
To learn Shell Script click here

Below is example code of pl/sql concepts
Addition of two numbers:

declare
 a number:=10;
 b number:=20;
 c number;
 begin
 c:=a+b;
 dbms_output.put_line(c);
 end;
 /

getting value from table and storing it in a variable and displaying it

declare
v_name varchar2(10);
begin
select ename into v_name from emp where empno=102;
 dbms_output.put_line('The emp name with emp no:102 is'|| v_name);
end;
/



Pay roll program in pl/sql
 declare
 eno number(5):=1245;
 ename varchar2(50):='jayachandran';
 ebasic number(7,2):=3000;
epf  number(7,2);
ehra number(7,2);
eda number(7,2);
ethome number(9,2);
begin
 epf:=0.02;
 epf:=epf*ebasic;
 ehra:=.12*ebasic;
 eda:=0.05*ebasic;
 ethome:=eda+ehra+ebasic-epf;
 dbms_output.put_line('Employee number ='||eno);
 dbms_output.put_line('Employee Name ='||ename);
 dbms_output.put_line('Employee take home ='||ethome);
 end;
/




 %type example in pl/sql

 declare
 v_name emp.ENAME%type;
begin
select ename into v_name from emp where empno=102;
 dbms_output.put_line('The emp name with emp no:102 is'|| v_name);
end;
/


if - else - if example in pl/sql
create table marks(name varchar2(10),math number(3),english number(3),tamil number(3));
declare
            stu marks.name%type;
            mat marks.math%type;
            eng marks.english%type;
            tam marks.tamil%type;
            tot marks.tamil%type;
            per number(5,2);
begin
            select  math,english,tamil into mat,eng,tam from marks where name = 'bala';
            tot:=mat+eng+tam;
            per:=((tot/300)*100);
            if(per = 100) then
                        dbms_output.put_line('you have scored s grade');
            else
                        if(per < 50) then
                                    dbms_output.put_line('you failed in exam');
            else
                        dbms_output.put_line('you passed exam');
            end if;
            end if;
end;
/


Go to session - 2