DBMS LABWEEKS

Embed Size (px)

Citation preview

  • 8/14/2019 DBMS LABWEEKS

    1/32

    *Print Armstrong numbers using While Loop in PL/SQL Block

    declare

    n number(12):=&num1;

    s number(12):=0;

    d number(12):=n;

    r number(12);begin

    while(d>=1)

    loop

    r:=mod(d,10);

    s:=s+(r*r*r);

    d:=floor(d/10);

    end loop;

    if(s=n) then

    dbms_output.put_line(n||' is a Armstrong');

    else

    dbms_output.put_line(n||' is not a Armstrong');

    end if;

    end;

    /

    *Print character in PL/SQL Block

    declare

    a number(3):=#

    b char(1);

    begin

    b:=chr(a);dbms_output.put_line('char is "'||b||'"');

    end;

    /

    *Print employees commission in PL/SQL Block

    declare

    eno number(4);

    ename varchar(20);

    sal number(12,2);

    comm number(4);

    beginselect empno,ename,sal,comm into eno,ename,sal,comm from epm where

    empno=7782;

    sal:=sal+nvl(comm,0);

    dbms_output.put_line(eno||' '||ename||' '||sal||' '||comm);

    end;

    /

    *Print salary level in PL/SQL Block

  • 8/14/2019 DBMS LABWEEKS

    2/32

    begin

    select ename,sal "salary",

    case

    when sal3000 and sal

  • 8/14/2019 DBMS LABWEEKS

    3/32

    loop

    c:=a+b;

    dbms_output.put_line(c||' ');

    a:=b;

    b:=c;

    end loop;end;

    /

    *Print factorial of a given number in PL/SQL Block

    declare

    n number(3):=#

    f number(4):=1;

    begin

    for i in 1..n

    loop

    f:=f*i;

    end loop;

    if(n

  • 8/14/2019 DBMS LABWEEKS

    4/32

    c:='THREE';

    when 4 then

    c:='FOUR';

    when 5 then

    c:='FIVE';

    when 6 thenc:='SIX';

    when 7 then

    c:='SEVEN';

    when 8 then

    c:='EIGHT';

    when 9 then

    c:='NINE';

    end case;

    dbms_output.put_line(c||' ');

    exit when (n

  • 8/14/2019 DBMS LABWEEKS

    5/32

    c:=one(mod(r,10))||'TEEN';

    end case;

    when 2 then

    c:=' TWENTY';

    when 3 then

    c:=' THIRTY';when 4 then

    c:=' FOURTY';

    when 5 then

    c:=' FIFTY';

    when 6 then

    c:=' SIXTY';

    when 7 then

    c:=' SEVENTY';

    when 8 then

    c:=' EIGHTY';

    when 9 then

    c:=' NINTY';

    end case;

    if(mod(n,10)>0 and t>1) then

    c:=c||' '||one(mod(n,10));

    end if;

    return c;

    end;

    /

    *Print words for a given number in PL/SQL Block

    create or replace function th(n in number) return varcharis

    t number(4);

    c varchar(50);

    begin

    t:=floor(n/1000);

    if(t>0) then

    c:=tens(t)||' THOUSAND';

    end if;

    if(mod(n,1000)>0) then

    c:=c||' '||hun(mod(n,1000));end if;

    return c;

    end;

    /

    *Print words for a given number in PL/SQL Block

    declare

    n number(10):=#

  • 8/14/2019 DBMS LABWEEKS

    6/32

    n1 number(10):=n;

    c number(3):=0;

    begin

    c:=length(n);

    case c

    when 1 thendbms_output.put_line(one(n));

    when 2 then

    dbms_output.put_line(tens(n));

    when 3 then

    dbms_output.put_line(hun(n));

    when 4 then

    dbms_output.put_line(th(n));

    when 5 then

    dbms_output.put_line(th(n));

    when 6 then

    dbms_output.put_line(lakh(n));

    when 7 then

    dbms_output.put_line(lakh(n));

    else

    dbms_output.put_line('no too large');

    end case;

    end;

    /

    *Print reverse string in PL/SQL Block

    declares1 varchar(13):='&string';

    s2 varchar(13);

    begin

    function nis (number s1)

    number s2:=revstr(s1);

    if(s1=s2) then

    dbms_output.put_line('palindrome');

    else

    dbms_output.put_line('not a palindrome');

    end if;end function;

    f(5);

    end;

    /

    *Print Reverse number in PL/SQL Block

    declare

    n varchar(5):='&n';

  • 8/14/2019 DBMS LABWEEKS

    7/32

    l number(2);

    r varchar(5);

    begin

    l:=length(n);

    for i in reverse 1..l

    loopr:=r||substr(n,i,1);

    end loop;

    dbms_output.put_line(n);

    dbms_output.put_line(r);

    end;

    *Print sum of numbers in PL/SQL Block

    declare

    a number(5);

    b number(5);

    c number(5);

    begin

    a:=&num1;

    b:=&num2;

    c:=a+b;

    dbms_output.put_line(a||'+'||b||'='||c);

    end;

    /

    Print Armstrong numbers using for loop in PL/SQL Block

    declare

    n number(12);s number(12):=0;

    d number(12);

    r number(12);

    begin

    for i in 1..1000

    loop

    d:=i;

    s:=0;

    while(d>=1)

    loopr:=mod(d,10);

    s:=s+(r*r*r);

    d:=floor(d/10);

    end loop;

    if(s=i) then

    dbms_output.put_line(i||' is a Armstrong');

    --else

  • 8/14/2019 DBMS LABWEEKS

    8/32

    --dbms_output.put_line(n||' is not a Armstrong');

    end if;

    end loop;

    end;

    /

    *Create a PL/SQL Block to print series of numbers using functionscreate or replace function fun(n number(3)) return number

    is

    begin

    for i in 1..n

    loop

    for j in 1..i

    loop

    dbms_output.put_line(i||' ');

    end loop;

    --dbms_output.put_line(' ');

    end loop;

    end ;

    declare

    n number(3):=#

    begin

    fun(n);

    end;

    /

    *Create a PL/SQL Block to check palindrome of a string using substr function

    declaren varchar(5):='&n';

    l number(2);

    r varchar(5);

    begin

    l:=length(n);

    for i in 1..l loop

    for j in 1..i loop

    dbms_output.put(substr(n,j,1)||' ');

    end loop;

    dbms_output.put_line(' ');end loop;

    end;

    *Create a PL/SQL Block to check palindrome of a string using revstr function

    declare

    s1 varchar(13):='&string';

    s2 varchar(13);

  • 8/14/2019 DBMS LABWEEKS

    9/32

    begin

    s2:=revstr(s1);

    if(s1=s2) then

    dbms_output.put_line('palindrome');

    else

    dbms_output.put_line('not a palindrome');end if;

    end;

    /

    *Create a PL/SQL Block to print wishing messages according to the timings and use

    commit.

    declare

    a number(3):=to_number(to_char(sysdate,'hh24'),'99');

    begin

    if(a>0 and a=12 and a

  • 8/14/2019 DBMS LABWEEKS

    10/32

    begin

    select ename,sal,comm into a,i.sal,i.comm from pm where empno=7782;

    dbms_output.put_line('ename= '||a||' sal='||i.sal);

    end;

    /*Create a PL/SQL Block to update commission using Row Type.

    declare

    i epm%type;

    begin

    update epm set comm=decode(comm,null,3000,0,2000);

    end;

    /

    *Create a PL/SQL Block to print Palindrome of a given number.

    declare

    n number(4):=#

    n1 number(4):=n;

    s number(6):=0;

    begin

    loop

    s:=s+mod(n1,10);

    n1:=floor(n1/10);

    exit when(n1

  • 8/14/2019 DBMS LABWEEKS

    11/32

    dbms_output.put_line(j);

    end if;

    end loop;

    end;

    /

    *Create a PL/SQL block to display factorial of a given number using procedure.create or replace procedure fibno(n in number)

    is

    f number(4);

    begin

    for i in 1..n

    loop

    f:=f*i;

    end loop;

    if(n0) then

  • 8/14/2019 DBMS LABWEEKS

    12/32

    c:=one(t)||' HUNDRED';

    end if;

    if(mod(n,100)>0) then

    c:=c||' '||tens(mod(n,100));

    end if;

    return c;end;

    /

    *Create a PL/SQL block to display numbers using functions.

    create or replace function lakh(n in number) return varchar

    is

    t number(4);

    c varchar(100);

    begin

    t:=floor(n/100000);

    if(t>0) then

    c:=tens(t)||' LAKHS';

    end if;

    if(mod(n,1000)>0) then

    c:=c||' '||th(mod(n,100000));

    end if;

    return c;

    end;

    /

    *Create a PL/SQL block to display numbers using functions.

    create or replace function one(n in number) return varchar

    isc varchar(10);

    begin

    case n

    when 0 then

    c:='ZERO';

    when 1 then

    c:='ONE';

    when 2 then

    c:='TWO';

    when 3 thenc:='THREE';

    when 4 then

    c:='FOUR';

    when 5 then

    c:='FIVE';

    when 6 then

    c:='SIX';

  • 8/14/2019 DBMS LABWEEKS

    13/32

    when 7 then

    c:='SEVEN';

    when 8 then

    c:='EIGHT';

    when 9 then

    c:='NINE';end case;

    return c;

    end;

    /

    *Create a PL/SQL block to display step numbers using procedures.

    create or replace procedure stepnum(n in number)

    is

    begin

    for i in 1..n

    loop

    for j in 1..i

    loop

    dbms_output.put(j||' ');

    end loop;

    dbms_output.put_line(' ');

    end loop;

    end stepnum;

    /

    *Create a PL/SQL block to display reverse of given number using functions.

    create or replace function rev(n in number) return number

    isnum number(5);

    begin

    select to_number(reverse(to_char(n,'99999'))) into num from dual;

    return(num);

    end;

    /

    *Create a PL/SQL block to display reverse of string using functions.

    create or replace function revstr(n in varchar) return varchar

    is

    r varchar(10);l number(3);

    begin

    l:=length(n);

    for i in reverse 1..l

    loop

    r:=r||substr(n,i,1);

    end loop;

  • 8/14/2019 DBMS LABWEEKS

    14/32

    return r;

    end;

    /

    *Create a PL/SQL block to display series of numbers.

    begin

    dbms_output.put_line(series(5));end;

    /

    Create a PL/SQL block to display greatest of two numbers.

    declare

    a number(2):=10;

    b number(2):=20;

    begin

    if(a>b) then

    dbms_output.put_line(a);

    dbms_output.new_line();

    dbms_output.put_line(a);

    else

    dbms_output.put_line(b);

    dbms_output.new_line();

    dbms_output.put_line(b);

    end if;

    end;

    /

    *Create a PL/SQL block to display greatest of three numbers.

    declare

    a number(2):=10;b number(2):=20;

    c number(2):=30;

    begin

    if(a>b)and(a>c) then

    dbms_output.put_line(a);

    dbms_output.new_line();

    dbms_output.put_line(a);

    elsif(b>c) then

    dbms_output.put_line(b);

    dbms_output.new_line();dbms_output.put_line(b);

    else

    dbms_output.put_line(c);

    dbms_output.new_line();

    dbms_output.put_line(c);

    end if;

    end;

  • 8/14/2019 DBMS LABWEEKS

    15/32

    /

    19) Create a PL/SQL block which accepts a string and displays the count of vowels ,

    words and length of the string.

    declare

    s varchar(30);

    nv int:=0;nw int:=1;

    len int;

    k varchar(1);

    begin

    s:=&s;

    len:=length(s);

    for i in 1..len

    loop

    k:=substr(s,i,1);

    case k

    when ' ' then

    nw:=nw+1;

    when 'a' then

    nv:=nv+1;

    when 'e' then

    nv:=nv+1;

    when 'i' then

    nv:=nv+1;

    when 'o' then

    nv:=nv+1;

    when 'u' thennv:=nv+1;

    when 'A' then

    nv:=nv+1;

    when 'E' then

    nv:=nv+1;

    when 'I' then

    nv:=nv+1;

    when 'O' then

    nv:=nv+1;

    when 'U' thennv:=nv+1;

    else

    nv:=nv;

    end case;

    end loop;

    dbms_output.put_line('count of vowels:'||nv);

    dbms_output.put_line('count of words:'||nw);

  • 8/14/2019 DBMS LABWEEKS

    16/32

    dbms_output.put_line('length of string:'||len);

    end;

    *Create a PL/SQL block which prints step number

    declare

    n number(2);

    beginn:=&n;

    for i in 0..n+1 loop

    for j in 0..i loop

    dbms_output.put(' ');

    end loop;

    for k in 0..(n-i) loop

    dbms_output.put(' $');

    end loop;

    dbms_output.new_line;

    end loop;

    end;

    /

    Output:

    $ $ $ $ $ $

    $ $ $ $ $

    $ $ $ $

    $ $ $

    $ $

    $*Create a PL/SQL block which displays particular record using exception.

    declare

    v_empno number;

    begin

    select empno into v_empno from emp1 where emp_name='Daniel';

    exception

    when no_data_found then

    insert into emp1 (empno,emp_name,sal) values(223,'Daniel',17000);

    end;

    *Create a PL/SQL block which displays particular record using case.

    declare

    v_grade char(1) := upper('&p_grade');

    v_appraisal varchar2(20);

    begin

    v_appraisal := CASE v_grade

    when 'A' then 'Excellent'

  • 8/14/2019 DBMS LABWEEKS

    17/32

    when 'B' then 'Very Good'

    when 'C' then 'Good'

    else 'No such grade'

    end;

    dbms_output.put_line('Grade: '|| v_grade);

    dbms_output.put_line('Appraisal: ' || v_appraisal);end;

    /

    *Create a PL/SQL block which displays value_error exception.

    DECLARE

    a VARCHAR2(1);

    b VARCHAR2(2) := 'AB';

    BEGIN

    a := b;

    EXCEPTION

    WHEN value_error THEN

    dbms_output.put_line('You can''t put ['||b||'] in a one character string.');

    END;

    /

    *Create a PL/SQL block which displays value_error exception.

    declare

    a number;

    b varchar2(2);

    begin

    b:=&b;

    a := b;

    dbms_output.put_line(' '||a);EXCEPTION

    WHEN value_error THEN

    dbms_output.put_line('You can''t put ['||b||'] in a one character string.');

    WHEN others THEN

    dbms_output.put_line('Caught in outer block ['||SQLERRM||'].');

    END;

    /

    declare

    a number;

    b varchar2(2);begin

    select sal into a from emp1 where emp_name='karthik';

    EXCEPTION

    WHEN value_error THEN

    dbms_output.put_line('You can''t put ['||b||'] in a one character string.');

    WHEN others THEN

    dbms_output.put_line('Caught in outer block ['||SQLERRM||'].');

  • 8/14/2019 DBMS LABWEEKS

    18/32

    END;

    /

    declare

    n number;

    p varchar2(20);

    k number;begin

    n:=&n;

    k:=mod(n,2);

    p:=case k

    when 0 then 'Even Number'

    when 1 then 'Odd Number'

    end;

    dbms_output.put_line(' '||p);

    end;

    /

    *print step symbols in plsql block.

    declare

    n number;

    k number;

    p number;

    m number;

    s number;

    e number;

    d number;

    begin

    for i in 1..n loopfor j in 1..i loop

    if(mod(i,2)=0)

    if(mod(j,2)=0)

    s:=mod((i+j),2);

    m:=case s

    when 0 then '*';

    when 1 then '#';

    end;

    end if;

    elseif(mod(j,2)=0)

    e:=mod((i*j),2);

    d:=case e

    when 0 then '%'

    when 1 then '@'

    end if;

    end if;

  • 8/14/2019 DBMS LABWEEKS

    19/32

    end loop;

    dbms_output.new _line;

    end loop;

    end;

    *print step number in pl/sql block.declare

    n number;

    begin

    n:=&n;

    for i in 1..n loop

    for j in 1..i loop

    dbms_output.put(i||' ');

    end loop;

    dbms_output.put_line(' ');

    end loop;

    end;

    /

    *print step number in plsql block.

    declare

    n number(2);

    i number(2);

    begin

    n:=&n;

    for i in 0..n loop

    for j in 0..i loop

    if mod(i,2)=0 thenif mod(j,2)=0 then

    dbms_output.put(' 1');

    else

    dbms_output.put(' 0');

    end if;

    else

    if mod(j,2)=0 then

    dbms_output.put(' 0');

    else

    dbms_output.put(' 1');end if;

    end if;

    end loop;

    dbms_output.new_line;

    end loop;

    end;

  • 8/14/2019 DBMS LABWEEKS

    20/32

    declare

    stu student1%rowtype;

    begin

    select nvl(max(rollno),0) + 1 into stu.rollno from student1;

    stu.name:= &name;

    stu.sub1:= &marks1;stu.sub2:= &marks2;

    Stu.total:=stu.sub1+stu.sub2;

    Stu.avg:= stu.total/2;

    insert into student1 values stu;

    end;

    /

    declare

    Vempno emp.emp_no%TYPE;

    Vsal emp.salary%TYPE;

    begin

    Vempno := &empno;

    select sal into vsal from emp where emp_no = Vempno;

    dbms_output_put_line(SALARY = || Vsal);

    exception

    when no_data_found

    then

    dbms_output.put_line(Employee not found);

    end;

    /

    declare

    name varchar2(15);begin

    select sname into name from student where marks=96;

    dbms_output.put_line('Name is : '||name);

    exception

    when too_many_rows then

    --dbms_output.put_line('Too many students exists with given marks');

    dbms_output.put_line(SQLCODE||SQLERRM);

    end;

    declare

    v_empno emp1.empno%type;v_sal emp1.sal%type;

    begin

    v_empno:=&empno;

    select sal into v_sal from emp1 where empno=v_empno;

    dbms_output.put_line('Salary: '||v_sal);

    exception

    /*when no_data_found

  • 8/14/2019 DBMS LABWEEKS

    21/32

    then

    dbms_output.put_line('Employee not found..');*/

    when others

    then

    dbms_output.put_line(SQLCODE||SQLERRM);

    end;/

    declare

    n number;

    k number;

    m number;

    p number;

    begin

    n:=&n;

    for i in 1..n loop

    for j in 1..n loop

    k:=mod(i,2);

    m:=mod(j,2);

    p:=k/m;

    dbms_output.put_line('p : '||p);

    end loop;

    end loop;

    exception

    when zero_divide then

    dbms_output.put_line(SQLCODE||SQLERRM);

    end;

    declare

    s1_marks number(3):=null;

    s2_marks number(3):=90;

    s3_marks number(3):=90;

    s4_marks number(3):=98;

    k number(3);

    begin

    if nullif(s2_marks,s3_marks) is Null then

    dbms_output.put_line('s1 and s2 have same marks');

    end if;if nullif(s2_marks,s4_marks) is not Null then

    dbms_output.put_line('s1 and s2 do not have same marks');

    end if;

    dbms_output.put_line(coalesce(s1_marks,s2_marks,s3_marks,s4_marks));

    end;

    DECLARE

  • 8/14/2019 DBMS LABWEEKS

    22/32

    TYPE list IS TABLE OF VARCHAR2(5);

    ord list:= list('','','Three','','Five');

    harry_potter VARCHAR2(10) := 'Gryffindor';

    ron_weasley VARCHAR2(10) := 'Gryffindor';

    cedric_diggory VARCHAR2(10) := 'Hufflepuff';

    BEGINdbms_output.put_line(COALESCE(ord(1),ord(2),ord(3),ord(4),ord(5)));

    IF NULLIF(harry_potter,ron_weasley) IS NULL THEN

    dbms_output.put_line('Same house!');

    END IF;

    IF NULLIF(harry_potter,cedric_diggory) IS NOT NULL THEN

    dbms_output.put_line('Different houses!');

    END IF;

    END;

    /

    create or replace function

    f1(pemp_no in emp2.emp_no%type,

    pemp_name out emp2.emp_name%type)

    return emp2.emp_name%type

    as

    begin

    select emp_name into pemp_name

    from emp2

    where emp_no=pemp_no;

    return pemp_name;

    end f1;

    /

    declare

    pemp_no emp2.emp_no%type;

    pemp_name emp2.emp_name%type;

    pjob emp2.job%type;

    pdept emp2.dept%type;

    pdept_no emp2.dept_no%type;

    begin

    pemp_no:=&pemp_no;

    p2(pemp_no,pemp_name,pjob,pdept,pdept_no);dbms_output.put_line(' '||pemp_name||pjob||pdept||pdept_no);

    end;

    /

    create or replace procedure

    p1(pemp_no in emp2.emp_no%type,

    pemp_name out emp2.emp_name%type)

  • 8/14/2019 DBMS LABWEEKS

    23/32

    as

    begin

    select emp_name into pemp_name

    from emp2

    where emp_no=pemp_no;

    end p1;/

    create or replace procedure

    p2(pemp_no in emp2.emp_no%type,

    pemp_name out emp2.emp_name%type,

    pjob out emp2.job%type,

    pdept out emp2.dept%type,

    pdept_no out emp2.dept_no%type)

    as

    begin

    select emp_name,job,dept,dept_no into pemp_name,pjob,pdept,pdept_no

    from emp2

    where emp_no=pemp_no;

    end p2;

    /

    --The following program declares and raises an exception:

    DECLARE

    e EXCEPTION;

    BEGIN

    RAISE e;dbms_output.put_line('Can''t get here.');

    EXCEPTION

    WHEN OTHERS THEN

    IF SQLCODE = 1 THEN

    dbms_output.put_line('This is a ['||SQLERRM||'].');

    END IF;

    END;

    /

    declarestu1 stu%rowtype;

    negative exception;

    begin

    stu1.sname:='&name';

    stu1.sub1:=&sub1;

    stu1.sub2:=&sub2;

    if stu1.sub1

  • 8/14/2019 DBMS LABWEEKS

    24/32

    then

    raise negative;

    end if;

    stu1.total:=stu1.sub1+stu1.sub2;

    stu1.avg:=stu1.total/2;

    insert into stu values stu1;exception

    when negative then

    dbms_output.put_line('Negative(-ve) Marks');

    end;

    declare

    invalid_department exception;

    begin

    update emp2 set dept_no=&dept_no where dept='&dept';

    if sql%notfound

    then

    raise invalid_department;

    end if;

    exception

    when invalid_department

    then

    dbms_output.put_line('No Such Department');

    end;

    BEGINRAISE_APPLICATION_ERROR(-20001,'A not too original message.');

    EXCEPTION

    WHEN others THEN

    dbms_output.put_line(SQLERRM);

    END;

    /

    DECLARE

    v_country_id locations.country_id%TYPE := 'IND';

    v_location_id locations.location_id%TYPE;v_city locations.city%TYPE := 'Hyderabad';

    v_c NUMBER := 1;

    BEGIN

    SELECT MAX(location_id) INTO v_location_id FROM locations

    WHERE country_id = v_country_id and city=v_city;

    WHILE v_c

  • 8/14/2019 DBMS LABWEEKS

    25/32

    VALUES(v_country_id,(v_location_id + v_c), v_city);

    v_c := v_c + 1;

    END LOOP;

    END;

    /

    DECLARE

    c NUMBER := 1;

    BEGIN

    WHILE (c = 1 THEN

    c := c + 1;

    END IF;

    END LOOP;

    END;

    /

    create function revstr(st varchar2)

    return varchar2

    is

    ch varchar2(10);

    begin

    ch:=NULL;

    for i in 1..length(str)

    loopch:=ch||substr(st,i,1);

    end loop;

    return ch;

    end;

    function created

    select revstr('mohini')from dual;

    inihom

    declares varchar2(10):='madam';

    ch varcahr2(10):=NULL;

    begin

    for i in reverse 1..length(s)

    loop

    ch:=ch||substr(s,i,1);

    end loop;

  • 8/14/2019 DBMS LABWEEKS

    26/32

    if s=ch then

    dbms_out.put_line('Palindrome');

    else

    dbms_out.put_line('Not a Palindrome');

    end if;

    end;

    declare

    i number(5):=1;

    j number(5);

    n number(5):=5;

    begin

    dbms_output.put_line('Output is:');

    while (i

  • 8/14/2019 DBMS LABWEEKS

    27/32

    rem:=mod(rev,10);

    if rem=3 or rem=4 or rem=5 or rem=6 or rem=7 or rem=8 or rem=9 then

    hiop('teen',rem);

    elsif rem=0 then

    dbms_output.put('ten');

    elsif rem=1 thendbms_output.put('eleven');

    elsif rem=2 then

    dbms_output.put('tweleve');

    end if;

    end if;

    else

    if n=3 then

    hiop('hundred and',rem);

    elsif n=1 then

    hiop('',rem);

    else

    hiop('thousand',rem);

    end if;

    end if;

    n:=n-1;

    rev:=trunc(rev/10);

    end loop;

    dbms_output.new_line();

    end;

    declare

    n number(10):=132;

    n1 number(10):=0;

    j number(10);

    n2 number(10);

    begin

    while (n0)

    loop

    j:=mod(n,10);n1:=(n1*10)+j;

    n:=n/10;

    end loop;

    n2:=n1;

    while (n20)

    loop

    j:=mod(n2,10);

  • 8/14/2019 DBMS LABWEEKS

    28/32

    n2:=n2/10;

    case j

    when 1 then

    dbms_output.put_line('ONE');

    when 0 then

    dbms_output.put_line('ZERO');when 2 then

    dbms_output.put_line('TWO');

    when 3 then

    dbms_output.put_line('THREE');

    when 4 then

    dbms_output.put_line('FOUR');

    when 5 then

    dbms_output.put_line('FIVE');

    when 6 then

    dbms_output.put_line('SIX');

    when 7 then

    dbms_output.put_line('SEVEN');

    when 8 then

    dbms_output.put_line('EIGHT');

    when 9 then

    dbms_output.put_line('NINE');

    else

    dbms_output.put_line(' ');

    end case;

    end loop;

    end;

    declare

    a number(4):=&a;

    begin

    if(mod(a,4):=0)then

    dbms_output.put_line(a||'is a leapyear');

    else

    dbms_output.put_line(a||'is not a leapyear');

    endif;

    end

    o/p

    enter value for a:2008

    2008 is a leapyear

    declare

    name varcahr2(10):='&name';

  • 8/14/2019 DBMS LABWEEKS

    29/32

    rollno number(10):='&rollno';

    m1 number(5):='&m1';

    m2 number(5):='&m2';

    m3 number(5):='&m3';

    m4 number(5):='&m4';

    total number(5);avg number(5,2);

    begin

    total:=m1+m2+m3+m4;

    dbms_output.put_line(total);

    avg:=total/4;

    dbms_output.put_line(avg);

    if avg>75 then

    dbms_output.put_line('grade is distinction');

    else if avg60 then

    dbms_output.put_line('grade is A');

    else if avg50 then

    dbms_output.put_line('grade is B');

    else if avg40 then

    dbms_output.put_line('grade is C');

    else

    dbms_output.put('Fail');

    end if;

    end;

    declare

    s varchar2(20):='Hello madam';i number(5):=length(s);

    e varchar2(20):=NULL;

    begin

    while (i>=1)

    loop

    e:=e||substr(s,i,1);

    i:=i-1;

    end loop;

    dbms_output.put_line('Reverse of string is '||e);

    end;

    declare

    i number(5):=1;

    j number(5);

    s varchar2(20):='GRIET';

    begin

    dbms_output.put_line('Output is:');

  • 8/14/2019 DBMS LABWEEKS

    30/32

    while (ic then

    if a>d then

    dbms_output.put_line('big number is'||a);

    else

    dbms_output.put_line('big number is'||b);

    else if

    elseif c>d then

    dbms_output.put_line('big number is'||c);

    else

    dbms_output.put_line('big number is'||d);

    end if

    end if

    end

    if b>c then

    if b>d then

    dbms_output.put_line('big number is'||b);else

    dbms_output.put_line('big numnber is'||d);

    end if;

    end if;

    end if;

    end;

  • 8/14/2019 DBMS LABWEEKS

    31/32

    declare

    s varchar2(50):='hello world,where r u';

    i number(5):=0;

    j number(5):=0;

    c number(5):=0;

    m varchar2(20);n varchar2(20);

    d number(5):=0;

    begin

    while (i

  • 8/14/2019 DBMS LABWEEKS

    32/32

    dbms_output.put(' 0');

    end if;

    else

    if mod(j,2)=0 then

    dbms_output.put(' 0');

    elsedbms_output.put(' 1');

    end if;

    end if;

    end loop;

    dbms_output.new_line;

    end loop;

    end;

    ----------------------------------------------------------------------------------------------------------------------

    --------------------