Results 1 to 7 of 7

Thread: delete deptno record in dept table

  1. #1
    Contributing Member
    Join Date
    Jul 2007
    Answers
    42

    delete deptno record in dept table

    hi sir,
    the following procedure is doing delete deptno record in dept table

    but reference to deptno in emp table so i can not delete data

    i want delete unless do match deptno in emp then
    match data would came error



    Create or replace procedure dept_del(pname varchar2)
    is
    dname_exc exception;
    pragma exception_int(dname_exc,-2292);
    cursor dept_cur is select deptno from dept where
    dname=pname;
    dept_rec dept%rowtype;
    ss number;
    emp_rec emp%rowtype;
    begin
    for dept_rec in dept_cur loop
    ss:=dept_rec.deptno;
    for emp_rec in(select deptno from emp) loop
    if emp_rec.deptno<>ss then
    delete from dept where dname=pname;
    else
    dbms_output.put_line(ss);
    end if;
    end loop;
    end loop;
    exception
    when dname_exc then
    dbms_output.put_line('integrity constraint');
    end;


  2. #2
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: delete deptno record in dept table

    Quote Originally Posted by priyasp_msit View Post
    hi sir,
    the following procedure is doing delete deptno record in dept table

    but reference to deptno in emp table so i can not delete data

    i want delete unless do match deptno in emp then
    match data would came error



    Create or replace procedure dept_del(pname varchar2)
    is
    dname_exc exception;
    pragma exception_int(dname_exc,-2292);
    cursor dept_cur is select deptno from dept where
    dname=pname;
    dept_rec dept%rowtype;
    ss number;
    emp_rec emp%rowtype;
    begin
    for dept_rec in dept_cur loop
    ss:=dept_rec.deptno;
    for emp_rec in(select deptno from emp) loop
    if emp_rec.deptno<>ss then
    delete from dept where dname=pname;
    else
    dbms_output.put_line(ss);
    end if;
    end loop;
    end loop;
    exception
    when dname_exc then
    dbms_output.put_line('integrity constraint');
    end;
    Here are some suggestions?

    >> pragma exception_int(dname_exc,-2292);
    it is pragma exception_init not int


    >>if emp_rec.deptno<>ss then
    delete from dept where dname=pname;


    Here you are checking condition. If no record is found in emp then only you are deleting that record in dept table. Then there will not be any integrity constraint problem.


    >>for emp_rec in(select deptno from emp) loop
    Here better to use select distinct deptno from emp


    >>pragma exception_int(dname_exc,-2292)
    Do you know what is the use of prgama exception_init?
    As per my expectation it will never raise this exception.


    What is your doubt? Still I am clear about that.


  3. #3
    Contributing Member
    Join Date
    Jul 2007
    Answers
    42

    Re: delete deptno record in dept table

    Create or replace procedure dept_del(pname varchar2)
    is
    dname_exc exception;
    pragma exception_init(dname_exc,-2292);
    cursor dept_cur is select deptno from dept where
    dname=pname;
    dept_rec dept%rowtype;
    ss number;
    emp_rec emp%rowtype;
    begin
    for dept_rec in dept_cur loop
    ss:=dept_rec.deptno;
    for emp_rec in(select deptno from emp) loop
    if emp_rec.deptno<>ss then
    delete from dept where dname=pname;
    else
    dbms_output.put_line(ss);
    end if;
    end loop;
    end loop;
    exception
    when dname_exc then
    dbms_output.put_line('integrity constraint');
    end;


    this program is run successfully.

    my expected result have not came.


    some data only foreign key reference(deptno in dept) deptno in emp table

    so i want to delete unmatched data
    at deptno in dept table.
    but this above program checked full table then match only consider so result came integrity constraint

    but i want match data result come integrity constraint
    not matched data will dlelete


  4. #4
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: delete deptno record in dept table

    >>but i want match data result come integrity constraint
    not matched data will dlelete


    For testing purpose first I have given OPERATIONS AS PARAMATER
    It deleted one record for deptno = 40

    Next I have given SALES as parameter.
    It raised exception and displaying message
    "integrity constraint"

    It is working as per your requirement. Then what is your problem?


  5. #5
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: delete deptno record in dept table

    The following modifications avoid full table scan of emp table.

    Create or replace procedure dept_del(pname varchar2)
    is
    dname_exc exception;
    pragma exception_init(dname_exc,-2292);
    cursor dept_cur is select deptno from dept where
    dname=pname;
    v_count number;
    dept_rec dept%rowtype;
    ss number;
    emp_rec emp%rowtype;
    begin
    for dept_rec in dept_cur loop
    ss:=dept_rec.deptno;
    select count(*) into v_count
    from emp
    where deptno = dept_rec.deptno;
    if v_count = 0 then
    delete from dept where dname=pname;
    else
    dbms_output.put_line(ss);
    end if;

    end loop;
    exception
    when dname_exc then
    dbms_output.put_line('integrity constraint');
    end;


  6. #6
    Contributing Member
    Join Date
    Jul 2007
    Answers
    42

    Re: delete deptno record in dept table

    i want following
    For testing purpose first I have given OPERATIONS AS PARAMATER
    It deleted one record for deptno = 40

    Next I have given SALES as parameter.
    It raised exception and displaying message
    "integrity constraint

    but my program and ur program is coming same result

    scaning full table suppose if one matched data came,result came "integrity constraint" ,i can't delete other un matched datas


  7. #7
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: delete deptno record in dept table

    Once an exception is raised it terminates the normal processing and control transfers to exception handling part. After executing exception handling part control goes to host environment. So you can not delete remaining records once exception is raised.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact