Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:05:29+00:00 2026-06-15T02:05:29+00:00

I am trying to write a PLSQL function that implements a constraint that an

  • 0

I am trying to write a PLSQL function that implements a constraint that an employee cannot be both a driver and a mechanic at the same time, in other words, the same E# from TRKEMPLOYEE cannot be in TRKDRIVER and TRKMECHANIC at the same time. If the contents of the DB violate that constraint, list the E# and NAME of all employees that are both mechanics and drivers. The abovementioned tables are something like as follows:

TRKEMPLOYEE(E#              NUMBER(12)      NOT NULL
    NAME            VARCHAR(50)     NOT NULL,
    DOB             DATE                    ,
    ADDRESS         VARCHAR(300)    NOT NULL,
    HIREDATE        DATE            NOT NULL,
CONSTRAINT TRKEMPLOYEE_PKEY PRIMARY KEY(E#))

TRKDRIVER(E#              NUMBER(12)      NOT NULL
L#              NUMBER(8)       NOT NULL,
STATUS          VARCHAR(10)     NOT NULL,
CONSTRAINT TRKDRIVER_PKEY PRIMARY KEY(E#),
CONSTRAINT TRKDRIVER_FKEY FOREIGN KEY(E#) REFERENCES TRKEMPLOYEE(E#))

TRKMECHANIC(E#              NUMBER(12)      NOT NULL
L#              NUMBER(8)       NOT NULL,
STATUS          VARCHAR(10)     NOT NULL,
EXPERIENCE      VARCHAR(10)     NOT NULL,
CONSTRAINT TRKMECHANIC_PKEY PRIMARY KEY(E#),
CONSTRAINT TRKMECHANIC_FKEY FOREIGN KEY(E#) REFERENCES TRKEMPLOYEE(E#))

I have attempted to write a function but keep getting a compile error in line 1 column 7. Can someone tell me why my code doesn’t work? My code is as follows

CREATE OR REPLACE FUNCTION Verify()
IS DECLARE
  E# TRKEMPLOYEE.E#%TYPE;
  CURSOR C1 IS SELECT E# FROM TRKEMPLOYEE;
BEGIN
  OPEN C1;
  LOOP
   FETCH C1 INTO EMPNUM;
   IF(EMPNUM IN(SELECT E# FROM TRKMECHANIC )AND EMPNUM IN(SELECT E# FROM TRKDRIVER))
     SELECT E#, NAME FROM TRKEMPLOYEE WHERE E#=EMPNUM;
   ELSE
     dbms_output.put_line(“OK”);
   ENDIF
   EXIT WHEN C1%NOTFOUND;
  END LOOP;
  CLOSE C1;
END;
/

Any help would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:05:31+00:00Added an answer on June 15, 2026 at 2:05 am

    You are creating a function but missing the RETURN declaration. If you don’t want to return a result, the use create or replace procedure.

    Additionally, if you don’t have any parameters, remove the brackets () and the DECLARE keyword is not correct either.


    The the way you check the tables is not really efficient. You can get the count of all employees violating your business constraint with a single query:

    select emp.e#, 
           emp.name,
           count(drv.e#) + count(mec.e#) as cnt
    from trkemployee emp
       left join trkdriver drv on drv.e# = emp.e#
       left join trkmechanic mec on mec.e# = emp.e#
    group by emp.e#, emp.name
    having count(drv.e#) + count(mec.e#) > 1;
    

    Additionally the EXIT WHEN C1%NOTFOUND; should be right after the FETCH statement. Otherwise you are staying in the loop even though the cursor didn’t fetch anything.

    If you take my simplified statement, you can loop once through all employees and print OK, nor Not OK depending on the count:

    CREATE OR REPLACE procedure Verify
    IS 
      empnum number(12);
      cnt    integer;
      empname varchar(50);
    
      CURSOR C1 IS 
          select emp.e#,
                 emp.name,
                 count(drv.e#) + count(mec.e#) as cnt  
          from trkemployee emp
             left join trkdriver drv on drv.e# = emp.e#
             left join trkmechanic mec on mec.e# = emp.e#
          group by emp.e#, emp.name;
    BEGIN
      OPEN C1;
      LOOP
        FETCH C1 INTO empnum, empname, cnt;
        EXIT WHEN C1%NOTFOUND;
    
        if cnt > 1 then 
           dbms_output.put_line(to_char(empnum)||' NOK');
        else
           dbms_output.put_line(to_char(empnum)||' OK');
        end if;
    
      END LOOP;
    CLOSE C1;
    END;
    /
    

    Solving the problem without a stored procedure

    I was thinking about a way how this business rule can be enforced with only constraints in the database:

    create table trkemployee
    (
        E#              NUMBER(12)      NOT NULL,
        NAME            VARCHAR(50)     NOT NULL,
        DOB             DATE                    ,
        ADDRESS         VARCHAR(300)    NOT NULL,
        HIREDATE        DATE            NOT NULL,
        emp_type        char(1)         NOT NULL,
        constraint trkemployee_pkey primary key(e#, emp_type),
        constraint chk_emp_type check (emp_type in ('d','m'))
    );
    
    create table TRKDRIVER
    (
      e#              number(12)      not null,
      emp_type        char(1)         default 'd' not null,
      l#              number(8)       not null,
      status          varchar(10)     not null,
      constraint trkdriver_pkey primary key(e#),
      constraint trkdriver_fkey foreign key(e#,emp_type) references trkemployee(e#, emp_type),
      constraint check_drv_type check (emp_type = 'd')
    );
    
    create table trkmechanic
    (
      e#              number(12)      not null,
      emp_type        char(1)         default 'm' not null,
      l#              number(8)       not null,
      status          varchar(10)     not null,
      experience      varchar(10)     not null,
      constraint trkmechanic_pkey primary key(e#),
      constraint trkmechanic_fkey foreign key(e#,emp_type) references trkemployee(e#, emp_type),
      constraint check_mec_type check (emp_type = 'm')
    );
    

    The idea is to include the employee type in its foreign key, and to make sure that the dependent tables cannot use the wrong type. When inserting a new trkemployee the type must be specified. The check constraint on the detail tables will reject any row with the wrong type.

    To “move” an employee from one type to the other (if this is possible at all), the old detail row must be deleted first. Only then the employee’s type can be changed to the other type. And finally the new detail could be inserted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
Trying to write a simple jQuery function that will multiple the index of an
I'm trying to write a function in PL/PgSQL that have to work with a
I am trying to write a plsql query that allows me to query a
I am trying write a PHP function that returns a random string of a
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a function to see how often an object exists and give
Trying to write a code at the moment that basically tests to see if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.