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

  • SEARCH
  • Home
  • 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 8886021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:27:22+00:00 2026-06-14T21:27:22+00:00

Below i have created 2 triggers that apply a discount, one uses a function

  • 0

Below i have created 2 triggers that apply a discount, one uses a function while the other does not. Is there any other way to make this efficient/better?

 CREATE OR REPLACE TRIGGER APPLY_DISCOUNT
 BEFORE INSERT OR UPDATE OF INV_NO,C_NO ON INVOICE
 FOR EACH ROW

 DECLARE

 CURSOR C_APPTMNT
 IS
 SELECT C_NO,COUNT(C_NO) 
 FROM APPOINTMENT GROUP BY C_NO;

 V_C_NO APPOINTMENT.C_NO%TYPE;
 VISIT NUMBER(2);

 BEGIN

 VISIT:=CNT_VISIT(:NEW.C_NO);

 IF VISIT BETWEEN 2 AND 4 
 AND :NEW.C_NO = V_C_NO THEN
 :NEW.BILL := :NEW.BILL * 0.9;

 ELSIF VISIT BETWEEN 5 AND 8 
 AND :NEW.C_NO = V_C_NO THEN
 :NEW.BILL := :NEW.BILL * 0.8;

 ELSIF VISIT >=9 AND :NEW.C_NO = V_C_NO THEN:NEW.BILL := :NEW.BILL * 0.7;

 ELSE DBMS_OUTPUT.PUT_LINE('no discount added');

 END IF;

 CLOSE C_APPTMNT;
 END;
 /

////////////////////////////////////////////////////////////////

 CREATE OR REPLACE FUNCTION ADD_DISCOUNT(
 I_C_NO INVOICE.C_NO%TYPE, I_BILL INVOICE.BILL%TYPE)
 RETURN NUMBER
 IS
 V_BILL invoice.bill%type;

 CURSOR C_APPTMNT
 IS
SELECT C_NO,COUNT(C_NO)
 FROM APPOINTMENT GROUP BY C_NO;

V_C_NO INVOICE.C_NO%TYPE;
VISIT NUMBER;

BEGIN

OPEN C_APPTMNT;
FETCH C_APPTMNT INTO V_C_NO,VISIT;

 IF VISIT >=3
AND I_C_NO = V_C_NO THEN
V_BILL := I_BILL * 0.9;

ELSIF VISIT >=6
AND I_C_NO = V_C_NO THEN
V_BILL := I_BILL * 0.8;

 ELSIF VISIT >=9 AND I_C_NO = V_C_NO THEN V_BILL := I_BILL * 0.7;
 ELSE V_BILL:= I_BILL;
END IF;

CLOSE C_APPTMNT;

RETURN V_BILL;

END;
/


CREATE OR REPLACE TRIGGER DIS_BILL
BEFORE INSERT OR UPDATE OF INV_NO,C_NO ON INVOICE
FOR EACH ROW
DECLARE
BEGIN
:NEW.BILL:=ADD_DISCOUNT(:NEW.C_NO,:NEW.BILL);
END;
/
  • 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-14T21:27:23+00:00Added an answer on June 14, 2026 at 9:27 pm

    The second one is wrong. If a value is >= 9 it is also >=6 and >=3. Therefore, those elses will never be reached.

    In the first one, you write output, but only if no discount was added. It feels like you just put that line there because it won’t compile without it, but you can also add a line containing null; to make an empty statement block compiling.

    There are more tricks to make this trigger faster. For one, you don’t have to query all records, since you know the group. And you can do the calculation in the query, although that won’t make it much faster.

    Your trigger could look like this:

    CREATE OR REPLACE TRIGGER APPLY_DISCOUNT
      BEFORE INSERT OR UPDATE OF INV_NO, C_NO ON INVOICE
      FOR EACH ROW
    
    BEGIN
    
      SELECT 
        CASE 
          WHEN COUNT(C_NO) >= 9 THEN 0.7
          WHEN COUNT(C_NO) >= 6 THEN 0.8
          WHEN COUNT(C_NO) >= 3 THEN 0.9
        ELSE 1
        END * :NEW.BILL
      INTO
        :NEW.BILL
      FROM 
       APPOINTMENT 
      WHERE
        C_NO = :NEW.C_NO;
    
    END;
    /
    

    I think SELECT INTO :NEW.BILL should work, but if not, you could select it into a variable and then assign it to :NEW.BILL.

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

Sidebar

Related Questions

I have created one table using the below command: create table Table1( Id int
I have created the below function to bind to all the classes of type
I have created below bat file to run my RMI server @echo Off set
//I have created below snippet to let the sensor to be detected. -(void)addProximitySensorControl {
I have created button 2 below: <input id=Button1 type=button value=Stop onclick=alert('hello world');/> <input id=Button2
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have created a spring security Filter as below. <!-- Enables Spring Security -->
I have created a dialog box in my MFC app as below: BackfaceControlPoints *controlpoints
I have created table and inserted values as below. create table mytable (id INT,
I have created a file with printStream object as shown below. PrintStream outPutOffice =

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.