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 4037222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:19:14+00:00 2026-05-20T12:19:14+00:00

I have to create a trigger (not procedure) to update subtotal, shipping_charge, tax and

  • 0

I have to create a trigger (not procedure) to update subtotal, shipping_charge, tax and total_amount. Basically, all these attributes come from orders table, and each of them involve some calculations. subtotal = Unit_Price * QTY where Unit_Price comes from Product table, and QTY comes from Orderline table. The problem is when the orders table was first created, there were no exp_ship and exp_receive dates so I had to alter the orders table to add these new columns. I am not allowed to change the old data. So, I created a copy of the Orders table for records, and have been working on the original orders table. Now, every time a new order is made, I have to write a trigger that would calculate the subtotal, shipping_charge, tax and total_amt, and update these fields in the orders table.

I tried the following code, but there are so many errors. I am sorry but I’m trying this for the first time. I am still in the learning process. The way I have to do it is by creating a view and using an Instead of trigger on the view. That is, get the information from the view and update the order table.

DROP VIEW ORDERS_V;

CREATE VIEW ORDERS_V AS

SELECT * FROM ORDERS;

CREATE OR REPLACE TRIGGER update_orders

INSTEAD OF INSERT ON ORDERS_V

FOR EACH ROW

DECLARE 

SHIPPING_COST NUMBER(6,3);

BEGIN

SELECT UNIT_PRICE INTO UNIT_PRICE FROM PRODUCT;

SELECT QTY INTO QTY FROM ORDERLINE;

SELECT SHIPPING_METHOD INTO SHIPPING_METHOD FROM ORDERS;

SELECT SUBTOTAL INTO SUBTOTAL FROM ORDERS;

UPDATE ORDERS 

    SET SUBTOTAL = UNIT_PRICE * QTY;

    IF SHIPPING_METHOD = 'GROUND' THEN

        shipping_cost := .05;

    ELSIF SHIPPING_METHOD = '1 DAY' THEN

        shipping_cost :=   .15;

    ELSIF SHIPPING_METHOD = '2 DAY' THEN

        shipping_cost :=   .10; 
    ELSE 

    shipping_cost := 0;        

    END IF;

UPDATE ORDERS

    SET shipping_charge = SUBTOTAL * shipping_cost;

END;

/
  • 1 1 Answer
  • 2 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-05-20T12:19:15+00:00Added an answer on May 20, 2026 at 12:19 pm

    At the moment your trigger is working with whole tables. This is obviously not what you want. So you need to add WHERE clauses to those statements, in order to restrict the activity to the required rows. In triggers we use the :NEW. notation to get the values of columns in the triggering table. Something like this:

        SELECT UNIT_PRICE INTO :new.UNIT_PRICE FROM PRODUCT
        where product_id = :new.product_id;
    
        SELECT QTY INTO :new.QTY FROM ORDERLINE
        where order_id = :new.order_id;
    

    Another syntax correction: we can include several columns in an UPDATE. This is better than issuing multiple statements.

    UPDATE ORDERS 
    SET :new.SUBTOTAL = ( :new.UNIT_PRICE * :new.QTY )
        , :new.shipping_charge = ( :new.SUBTOTAL * shipping_cost ) -- ??? see below
    where order_id = :new.order_id
    ;
    

    I did intend to re-write your whole trigger. However your logic remains confusing (you will recall I suggested you clarify things in my answer to your previous question). Things you need to straighten out:

    1. Your trigger fires on INSTEAD OF INSERT but your code UPDATES existing records in the underlying table. Huh?

    2. You are selecting from ORDERLINE. The expected data model would be an ORDER to have one or more LINES. If this is so, your code ought to handle multiple rows for ORDERLINE (and I guess PRODUCT).

    3. You select SUBTOTAL from the underlying table although subsequently you appear to overwrite the value. Then you use SUBTOTAL in a calculation: which value of SUBTOTAL do you think you’re using?

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

Sidebar

Related Questions

I have a table trigger like below: CREATE OR REPLACE TRIGGER PAT_BUR_DOB_TRG BEFORE UPDATE
I have this trigger procedure CASE_A as UPDATE table1 SET field1 = (SELECT bus
I have one question regarding trigger. The scenario is like this Create Procedure begin
I have a pretty simple trigger: CREATE OR REPLACE FUNCTION f_log_datei() RETURNS TRIGGER AS
I have a trigger that looks something like this: create or replace TRIGGER cluster_check
is is possible in postgres to have a trigger on CREATE TABLE that will
I have a deferred AFTER UPDATE trigger on a table, set to fire when
I have a strange problem here, I want to create an emtpy trigger: SET
I have tow tables concept_access and concept_access_log. I want to create a trigger that
I have the same trigger on two tables. The first one: CREATE TRIGGER updateURL1

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.