Lab Exercise 10-11-12_PLSQL

download Lab Exercise 10-11-12_PLSQL

of 24

Transcript of Lab Exercise 10-11-12_PLSQL

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    1/24

    LAB EXERCISE 10

    PL/SQL (BASICS)

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    2/24

    1. Write a PL/SQL block to find the area of the circle for radius=4. Also the radiusand the new area should be inserted into a table called Circle.

    Sol.:

    1.circle_area

    declare

    v_radius number(5,2):=1;

    v_area number(8,2):=1;c_pi constant number(3,2):=3.14;

    begin

    v_radius:= &V_radius;

    V_area := c_pi*power(v_radius,2);

    insert into circle values(v_radius, V_area);

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    3/24

    2. Write a PL/SQL block to find the area of the circle for different radius untilarea is less than 200. Also the radius and the new area should be insertedinto a table called Different_Area. Solve this question using all the threeloops separately.

    Simple Loop

    If-then Loop

    While Loop

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    4/24

    (a) different_area

    declare

    v_radius number(5,2):=1;

    v_area number(8,2):=1;

    c_pi constant number(3,2):=3.14;

    begin

    v_radius:= 4;Loop

    V_area := c_pi*power(v_radius,2);

    insert into different_circle values(v_radius, V_area);

    v_radius:=v_radius+1;exit when v_radius=10;

    end loop;

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    5/24

    (b) diff_area

    declare

    v_radius number(5,2):=1;v_area number(8,2):=1;

    c_pi constant number(3,2):=3.14;

    begin

    v_radius:= 4;

    while (v_radius

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    6/24

    (c) diff_area1

    declare

    v_radius number(5,2):=1;v_area number(8,2):=1;

    c_pi constant number(3,2):=3.14;

    begin

    v_radius:= 4;

    while (v_radius

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    7/24

    LAB EXERCISE 11

    PL/SQL(CURSORS)

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    8/24

    1. Write a PL/SQL block to display the emp_name, designation anddept_no from Employee table where dept_no=10.

    Solution:

    declare

    em employee%rowtype;

    begin

    select * into em from employee where dept_no=10;

    dbms_output.put_line('Employee name:'||em.emp_name ||'

    Designation:'||em.designation ||' Dept_no:' || em.dept_no);

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    9/24

    2. Write a PL/SQL block to update the value of City in Employee tableusing IMPLICIT CURSOR attributes:

    %FOUND

    %NOTFOUND

    %ROWCOUNT

    And EXPLICIT CURSOR attributes:

    %NOTFOUND

    %ROWCOUNT

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    10/24

    Solution:

    /*-----------------Implict cursor---------------*/

    declare

    row number(4);

    begin

    update employee set dept_loc='&city' where emp_no=&emp_no;

    if sql%found then

    dbms_output.put_line('Sussesfully updated');

    end if;if sql%notfound then

    dbms_output.put_line('Employee not found');

    end if;

    row:=sql%rowcount;if sql%rowcount>0 then

    dbms_output.put_line(row ||' :Employee updated');

    end if;

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    11/24

    /*-------------------Explicit cursor--------------------*/

    declare

    cursor cr_emp is select emp_no from employee where emp_no=&emp_no;

    v_emp_no employee.emp_name%type;

    row number(5);

    begin

    open cr_emp;

    fetch cr_emp into v_emp_no;

    if (cr_emp%notfound) then

    dbms_output.put_line('Employee not found');

    end if;

    row:=cr_emp%rowcount;

    if(cr_emp%rowcount>0)then

    update employee set dept_loc='&loc' where emp_no=v_emp_no;

    dbms_output.put_line(row || ' :row updated');

    end if;

    close cr_emp;

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    12/24

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    13/24

    declare

    cursor c_dep(v_dep_no number)isselect dept_no,dept_loc fromemployee wheredept_no=v_dep_no;

    v_dep_no employee.dept_no%type;

    v_dep_loc employee.dept_loc%type;

    begin

    v_dep_no:=&dep_no;

    open c_dep(v_dep_no);loop

    fetch c_dep intoV_dep_no,v_dep_loc;

    if (c_dep%notfound andc_dep%rowcount>0) then

    dbms_output.put_line('Lastemployee');

    exit;

    else

    if(c_dep%notfound)then

    dbms_output.put_line('noemployee');

    end if;

    exit when c_dep%notfound;dbms_output.put_line(v_dep_no ||'

    '||v_dep_loc);

    end if;

    end loop;

    close c_dep;end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    14/24

    4. Write a PL/SQL code block to display the following report in the formatgiven below. Given Table is Emp (Empno, name, deptno, salary,date_of_join):

    Years of service No. of persons

    =1 and =5 and =10 ----

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    15/24

    declare

    cursor c_rep is selectemp_no,date_of_join fromemployee;

    doj date;e_no number(5);

    n1 number(5):=0;

    n2 number(5):=0;

    n3 number(5):=0;n4 number(5):=0;

    begin

    open c_rep;

    loop

    fetch c_rep into e_no,doj;exit when c_rep%notfound;

    if((sysdate-doj)/365=1 and (sysdate-doj)/365=5 and (sysdate-doj)/365=10 )then

    n4:=n4+1;

    end if;

    end loop;

    dbms_output.put_line('Years of services Noof person');

    dbms_output.put_line('=1 and =5 and =10 ' ||n4);

    close c_rep;

    end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    16/24

    5. Write a PL/SQL code block to display the following reportdepartment wise in the format given below based on thegiven table:

    Emp(Empno, name, deptno, salary, date_of_join)

    For Department Number: ----

    Salary No. of persons

    =1000 and =5000 and < 10000 ----

    >=10000 ----

    Total ----

    Given Table

    Item_master(icode, description, balstock)

    Item_tran(icode, tran_type, qty)

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    17/24

    create table item_master(icodevarchar2(15)primary key check(icodelike 'I%'),

    description varchar2(10),

    balstock number(5)not null);

    create table item_tran(icodevarchar2(15)primary key check(icodelike 'I%'),

    tran_type varchar2(6 )not null

    check(tran_type in('I','D','U')),qty number(5)not null);

    declare

    cursor c_item is selecticode,tran_type,qty from item_tran;

    icode varchar2(15);

    type1 varchar2(5);

    qty1 number(5);

    begin

    open c_item;

    loop

    fetch c_item into icode,type1,qty1;

    exit when c_item%notfound;if(type1='I')then

    insert into item_mastervalues(icode,'&description',qty1);

    end if;

    end loop;

    close c_item;end;

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    18/24

    LAB EXERCISE 12

    PL/SQL (PROCEDURES)

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    19/24

    1. Write a PL/SQL procedure in which you have toaccept salary from the user. Based on thefollowing range of salary the employee will

    receive his/her D.A:

    Salary D.A

    >1000 and < 3000 5%>3000 and 5000 and < 10000 15%

    The D.A which the employees will receive basedon their salary should be notified to themthrough proper messages.

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    20/24

    create or replace procedure da(sal innumber)as

    vsal number(5);

    beginvsal:=sal;

    if(vsal>1000 and vsal3000 and vsal5000 and vsal

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    21/24

    2. Write a PL/SQL procedure to

    calculate total salary of the

    employees from the Employeetable. Display total salary,Dept_name and Designation if

    Emp_no is given.

    l d

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    22/24

    create or replace procedurets(empno1 in number)as

    sal1 number(5);

    dep emp.deptno%type;

    des emp.job%type;

    begin

    select sal into sal1 from emp whereempno=empno1;

    if(sal1>1000 and sal13000 and sal15000 and sal1

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    23/24

    Create the Branch_Summary table

    with the following specification:

    Column Name Data type and Size

    Branch_City Varchar2(15)

    Reserv_Count Number(3)

    Reserv_Amt Number(8)

    Cancel_Count

    Number(3)

    Cancel_Amt Number(8)

    Create a procedure which receives branch code as a

    parameter and calculates the total collection made due toreservation and lost due to cancellation. It should alsocount the total number of reservations and cancellations.

  • 8/12/2019 Lab Exercise 10-11-12_PLSQL

    24/24

    create or replace procedureupd_sal(id in varchar2,min_sal innumber,max_sal in number)as

    u1 exception;

    begin

    if(min_sal>max_sal)then

    raise u1;

    end if;

    update jobs setmin_salary=min_sal,max_salary=

    max_sal where job_id=id;exception

    when no_data_found then

    dbms_output.put_line('id notpersent');

    when u1 thendbms_output.put_line('minimum

    salary is grater then maximumsalary');

    end;

    declare

    id number(4);

    min_s jobs.min_salary%type;

    max_s number(8);

    begin

    id:=&jobs_id;

    min_s:=&minimum_salary;

    max_s:=&maximum_salary;

    upd_sal(id,min_s,max_s);

    end;