How to fix trigger or procedure in Oracle express to automate values in a column of a table












0















How can I to automate the column sale.sale_total which would be the sum of the values column sales_line.line_total. Can you make a trigger or procedure for these two questions?
can you help me?



Something like: sale.sale_total = (sale.sale_total + .sales_line.line_total).



CREATE TABLE product (
product_id NUMBER(4) NOT NULL,
category_id NUMBER(4) NOT NULL,
p_desc VARCHAR2(40),
cpu VARCHAR2(14),
ram VARCHAR2(14),
capacity VARCHAR2(14),
screen_size VARCHAR2(14),
battery VARCHAR2(14),
unit_price NUMBER(7, 2),
colour VARCHAR2(14),
qty_stock NUMBER(4)
);
ALTER TABLE product ADD CONSTRAINT product_pk PRIMARY KEY ( product_id );

CREATE TABLE sale (
sale_id NUMBER(4) NOT NULL,
sale_date DATE,
customer_id NUMBER(4) NOT NULL,
employee_id NUMBER(4) NOT NULL,
sale_total NUMBER(7, 2)
);

ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY ( sale_id );

CREATE TABLE sales_line (
sale_id NUMBER(4) NOT NULL,
product_id NUMBER(4) NOT NULL,
line_qty NUMBER(4),
line_total NUMBER(7, 2)
);

ALTER TABLE sales_line ADD CONSTRAINT index_3 PRIMARY KEY ( sale_id,
product_id );

ALTER TABLE product
ADD CONSTRAINT product_p_category_fk FOREIGN KEY ( category_id )
REFERENCES p_category ( category_id );

ALTER TABLE sale
ADD CONSTRAINT sale_customer_fk FOREIGN KEY ( customer_id )
REFERENCES customer ( customer_id );

ALTER TABLE sale
ADD CONSTRAINT sale_employee_id_fk FOREIGN KEY ( employee_id )
REFERENCES employee ( employee_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_product_fk FOREIGN KEY ( product_id )
REFERENCES product ( product_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_sale_fk FOREIGN KEY ( sale_id )
REFERENCES sale ( sale_id );

**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


I tried do like @Gordon Linoff (code below), but the value in the table sale.line_total continue null. I tried with BEFORE UPDATE, the same:



CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total +
(CASE WHEN sale_id = :NEW.sale_id THEN :NEW.line_total ELSE 0 END) -
(CASE WHEN sale_id = :OLD.sale_id THEN :OLD.line_total ELSE 0 END)
)
WHERE sale_id IN (:OLD.sale_id, :NEW.sale_id);


I just want update the sale.sale_total column with the sum of the values of the sales_line.line_total, after I insert data in the sales_line table.



**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


Thank you










share|improve this question

























  • I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

    – DViga
    Jan 1 at 21:27











  • Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

    – sticky bit
    Jan 1 at 21:36













  • Thank you for your advice @sticky bit I will make view.

    – DViga
    Jan 2 at 0:53
















0















How can I to automate the column sale.sale_total which would be the sum of the values column sales_line.line_total. Can you make a trigger or procedure for these two questions?
can you help me?



Something like: sale.sale_total = (sale.sale_total + .sales_line.line_total).



CREATE TABLE product (
product_id NUMBER(4) NOT NULL,
category_id NUMBER(4) NOT NULL,
p_desc VARCHAR2(40),
cpu VARCHAR2(14),
ram VARCHAR2(14),
capacity VARCHAR2(14),
screen_size VARCHAR2(14),
battery VARCHAR2(14),
unit_price NUMBER(7, 2),
colour VARCHAR2(14),
qty_stock NUMBER(4)
);
ALTER TABLE product ADD CONSTRAINT product_pk PRIMARY KEY ( product_id );

CREATE TABLE sale (
sale_id NUMBER(4) NOT NULL,
sale_date DATE,
customer_id NUMBER(4) NOT NULL,
employee_id NUMBER(4) NOT NULL,
sale_total NUMBER(7, 2)
);

ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY ( sale_id );

CREATE TABLE sales_line (
sale_id NUMBER(4) NOT NULL,
product_id NUMBER(4) NOT NULL,
line_qty NUMBER(4),
line_total NUMBER(7, 2)
);

ALTER TABLE sales_line ADD CONSTRAINT index_3 PRIMARY KEY ( sale_id,
product_id );

ALTER TABLE product
ADD CONSTRAINT product_p_category_fk FOREIGN KEY ( category_id )
REFERENCES p_category ( category_id );

ALTER TABLE sale
ADD CONSTRAINT sale_customer_fk FOREIGN KEY ( customer_id )
REFERENCES customer ( customer_id );

ALTER TABLE sale
ADD CONSTRAINT sale_employee_id_fk FOREIGN KEY ( employee_id )
REFERENCES employee ( employee_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_product_fk FOREIGN KEY ( product_id )
REFERENCES product ( product_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_sale_fk FOREIGN KEY ( sale_id )
REFERENCES sale ( sale_id );

**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


I tried do like @Gordon Linoff (code below), but the value in the table sale.line_total continue null. I tried with BEFORE UPDATE, the same:



CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total +
(CASE WHEN sale_id = :NEW.sale_id THEN :NEW.line_total ELSE 0 END) -
(CASE WHEN sale_id = :OLD.sale_id THEN :OLD.line_total ELSE 0 END)
)
WHERE sale_id IN (:OLD.sale_id, :NEW.sale_id);


I just want update the sale.sale_total column with the sum of the values of the sales_line.line_total, after I insert data in the sales_line table.



**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


Thank you










share|improve this question

























  • I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

    – DViga
    Jan 1 at 21:27











  • Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

    – sticky bit
    Jan 1 at 21:36













  • Thank you for your advice @sticky bit I will make view.

    – DViga
    Jan 2 at 0:53














0












0








0








How can I to automate the column sale.sale_total which would be the sum of the values column sales_line.line_total. Can you make a trigger or procedure for these two questions?
can you help me?



Something like: sale.sale_total = (sale.sale_total + .sales_line.line_total).



CREATE TABLE product (
product_id NUMBER(4) NOT NULL,
category_id NUMBER(4) NOT NULL,
p_desc VARCHAR2(40),
cpu VARCHAR2(14),
ram VARCHAR2(14),
capacity VARCHAR2(14),
screen_size VARCHAR2(14),
battery VARCHAR2(14),
unit_price NUMBER(7, 2),
colour VARCHAR2(14),
qty_stock NUMBER(4)
);
ALTER TABLE product ADD CONSTRAINT product_pk PRIMARY KEY ( product_id );

CREATE TABLE sale (
sale_id NUMBER(4) NOT NULL,
sale_date DATE,
customer_id NUMBER(4) NOT NULL,
employee_id NUMBER(4) NOT NULL,
sale_total NUMBER(7, 2)
);

ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY ( sale_id );

CREATE TABLE sales_line (
sale_id NUMBER(4) NOT NULL,
product_id NUMBER(4) NOT NULL,
line_qty NUMBER(4),
line_total NUMBER(7, 2)
);

ALTER TABLE sales_line ADD CONSTRAINT index_3 PRIMARY KEY ( sale_id,
product_id );

ALTER TABLE product
ADD CONSTRAINT product_p_category_fk FOREIGN KEY ( category_id )
REFERENCES p_category ( category_id );

ALTER TABLE sale
ADD CONSTRAINT sale_customer_fk FOREIGN KEY ( customer_id )
REFERENCES customer ( customer_id );

ALTER TABLE sale
ADD CONSTRAINT sale_employee_id_fk FOREIGN KEY ( employee_id )
REFERENCES employee ( employee_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_product_fk FOREIGN KEY ( product_id )
REFERENCES product ( product_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_sale_fk FOREIGN KEY ( sale_id )
REFERENCES sale ( sale_id );

**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


I tried do like @Gordon Linoff (code below), but the value in the table sale.line_total continue null. I tried with BEFORE UPDATE, the same:



CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total +
(CASE WHEN sale_id = :NEW.sale_id THEN :NEW.line_total ELSE 0 END) -
(CASE WHEN sale_id = :OLD.sale_id THEN :OLD.line_total ELSE 0 END)
)
WHERE sale_id IN (:OLD.sale_id, :NEW.sale_id);


I just want update the sale.sale_total column with the sum of the values of the sales_line.line_total, after I insert data in the sales_line table.



**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


Thank you










share|improve this question
















How can I to automate the column sale.sale_total which would be the sum of the values column sales_line.line_total. Can you make a trigger or procedure for these two questions?
can you help me?



Something like: sale.sale_total = (sale.sale_total + .sales_line.line_total).



CREATE TABLE product (
product_id NUMBER(4) NOT NULL,
category_id NUMBER(4) NOT NULL,
p_desc VARCHAR2(40),
cpu VARCHAR2(14),
ram VARCHAR2(14),
capacity VARCHAR2(14),
screen_size VARCHAR2(14),
battery VARCHAR2(14),
unit_price NUMBER(7, 2),
colour VARCHAR2(14),
qty_stock NUMBER(4)
);
ALTER TABLE product ADD CONSTRAINT product_pk PRIMARY KEY ( product_id );

CREATE TABLE sale (
sale_id NUMBER(4) NOT NULL,
sale_date DATE,
customer_id NUMBER(4) NOT NULL,
employee_id NUMBER(4) NOT NULL,
sale_total NUMBER(7, 2)
);

ALTER TABLE sale ADD CONSTRAINT sale_pk PRIMARY KEY ( sale_id );

CREATE TABLE sales_line (
sale_id NUMBER(4) NOT NULL,
product_id NUMBER(4) NOT NULL,
line_qty NUMBER(4),
line_total NUMBER(7, 2)
);

ALTER TABLE sales_line ADD CONSTRAINT index_3 PRIMARY KEY ( sale_id,
product_id );

ALTER TABLE product
ADD CONSTRAINT product_p_category_fk FOREIGN KEY ( category_id )
REFERENCES p_category ( category_id );

ALTER TABLE sale
ADD CONSTRAINT sale_customer_fk FOREIGN KEY ( customer_id )
REFERENCES customer ( customer_id );

ALTER TABLE sale
ADD CONSTRAINT sale_employee_id_fk FOREIGN KEY ( employee_id )
REFERENCES employee ( employee_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_product_fk FOREIGN KEY ( product_id )
REFERENCES product ( product_id );

ALTER TABLE sales_line
ADD CONSTRAINT sales_line_sale_fk FOREIGN KEY ( sale_id )
REFERENCES sale ( sale_id );

**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


I tried do like @Gordon Linoff (code below), but the value in the table sale.line_total continue null. I tried with BEFORE UPDATE, the same:



CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total +
(CASE WHEN sale_id = :NEW.sale_id THEN :NEW.line_total ELSE 0 END) -
(CASE WHEN sale_id = :OLD.sale_id THEN :OLD.line_total ELSE 0 END)
)
WHERE sale_id IN (:OLD.sale_id, :NEW.sale_id);


I just want update the sale.sale_total column with the sum of the values of the sales_line.line_total, after I insert data in the sales_line table.



**Here are the triggers I have written so far:**

CREATE OR REPLACE TRIGGER trg_sale_total_ai AFTER INSERT OR UPDATE ON sales_line
FOR EACH ROW
BEGIN
UPDATE sale
SET sale_total = (sale_total + :NEW.line_total)
WHERE sale.sale_id = :NEW.sale_id;
END;


Thank you







sql database oracle triggers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 21:19







DViga

















asked Jan 1 at 21:13









DVigaDViga

276




276













  • I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

    – DViga
    Jan 1 at 21:27











  • Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

    – sticky bit
    Jan 1 at 21:36













  • Thank you for your advice @sticky bit I will make view.

    – DViga
    Jan 2 at 0:53



















  • I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

    – DViga
    Jan 1 at 21:27











  • Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

    – sticky bit
    Jan 1 at 21:36













  • Thank you for your advice @sticky bit I will make view.

    – DViga
    Jan 2 at 0:53

















I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

– DViga
Jan 1 at 21:27





I took a look at your @Ben site. It depends if I have the help I need to do this trigger. I do not want a system, I do not want a whole database, because as you've seen, I've done it, I'd just like a trigger help because I'm having trouble getting it to work and understand. If you can help me I'm very grateful. So professionals like you, can show their skills and get customers, like me. I do not have much knowledge in oracle express, but I'm trying and looking for anyone who can help I understand and make it work.

– DViga
Jan 1 at 21:27













Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

– sticky bit
Jan 1 at 21:36







Preferably don't physically store values, that can be computed from other columns. It bears the severe risk of producing inconsistencies! For example it seems like you totally forgot, that you'd also need a trigger on product to reflect changes to the unit_price. You haven't thought of a delete trigger on sales_line that updates sale accordingly. You'd have inconsistencies right away. And maybe there are even more problems. You better use views if this is for convenience.

– sticky bit
Jan 1 at 21:36















Thank you for your advice @sticky bit I will make view.

– DViga
Jan 2 at 0:53





Thank you for your advice @sticky bit I will make view.

– DViga
Jan 2 at 0:53












1 Answer
1






active

oldest

votes


















0














Your best bet is to execute



ALTER TABLE SALE
DROP COLUMN SALE_TOTAL;


This is not a column you want to have because, as you're just beginning to realize, maintaining it is going to be a nightmare. Instead, you want a function:



CREATE OR REPLACE FUNCTION COMPUTE_SALE_TOTAL(pinSale_id IN SALE.SALE_ID%TYPE)
RETURN NUMBER
AS
nSale_total NUMBER;
BEGIN
SELECT SUM(LINE_TOTAL)
INTO nSale_total
FROM SALES_LINE
WHERE SALE_ID = pinSale_id;

RETURN nSale_total;
END COMPUTE_SALE_TOTAL;


Then, any time you need to know what the total for a sale is, you call this function.



Best of luck.






share|improve this answer
























  • Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

    – DViga
    Jan 2 at 0:53











  • That's another good approach.

    – Bob Jarvis
    Jan 2 at 2:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998985%2fhow-to-fix-trigger-or-procedure-in-oracle-express-to-automate-values-in-a-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your best bet is to execute



ALTER TABLE SALE
DROP COLUMN SALE_TOTAL;


This is not a column you want to have because, as you're just beginning to realize, maintaining it is going to be a nightmare. Instead, you want a function:



CREATE OR REPLACE FUNCTION COMPUTE_SALE_TOTAL(pinSale_id IN SALE.SALE_ID%TYPE)
RETURN NUMBER
AS
nSale_total NUMBER;
BEGIN
SELECT SUM(LINE_TOTAL)
INTO nSale_total
FROM SALES_LINE
WHERE SALE_ID = pinSale_id;

RETURN nSale_total;
END COMPUTE_SALE_TOTAL;


Then, any time you need to know what the total for a sale is, you call this function.



Best of luck.






share|improve this answer
























  • Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

    – DViga
    Jan 2 at 0:53











  • That's another good approach.

    – Bob Jarvis
    Jan 2 at 2:19
















0














Your best bet is to execute



ALTER TABLE SALE
DROP COLUMN SALE_TOTAL;


This is not a column you want to have because, as you're just beginning to realize, maintaining it is going to be a nightmare. Instead, you want a function:



CREATE OR REPLACE FUNCTION COMPUTE_SALE_TOTAL(pinSale_id IN SALE.SALE_ID%TYPE)
RETURN NUMBER
AS
nSale_total NUMBER;
BEGIN
SELECT SUM(LINE_TOTAL)
INTO nSale_total
FROM SALES_LINE
WHERE SALE_ID = pinSale_id;

RETURN nSale_total;
END COMPUTE_SALE_TOTAL;


Then, any time you need to know what the total for a sale is, you call this function.



Best of luck.






share|improve this answer
























  • Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

    – DViga
    Jan 2 at 0:53











  • That's another good approach.

    – Bob Jarvis
    Jan 2 at 2:19














0












0








0







Your best bet is to execute



ALTER TABLE SALE
DROP COLUMN SALE_TOTAL;


This is not a column you want to have because, as you're just beginning to realize, maintaining it is going to be a nightmare. Instead, you want a function:



CREATE OR REPLACE FUNCTION COMPUTE_SALE_TOTAL(pinSale_id IN SALE.SALE_ID%TYPE)
RETURN NUMBER
AS
nSale_total NUMBER;
BEGIN
SELECT SUM(LINE_TOTAL)
INTO nSale_total
FROM SALES_LINE
WHERE SALE_ID = pinSale_id;

RETURN nSale_total;
END COMPUTE_SALE_TOTAL;


Then, any time you need to know what the total for a sale is, you call this function.



Best of luck.






share|improve this answer













Your best bet is to execute



ALTER TABLE SALE
DROP COLUMN SALE_TOTAL;


This is not a column you want to have because, as you're just beginning to realize, maintaining it is going to be a nightmare. Instead, you want a function:



CREATE OR REPLACE FUNCTION COMPUTE_SALE_TOTAL(pinSale_id IN SALE.SALE_ID%TYPE)
RETURN NUMBER
AS
nSale_total NUMBER;
BEGIN
SELECT SUM(LINE_TOTAL)
INTO nSale_total
FROM SALES_LINE
WHERE SALE_ID = pinSale_id;

RETURN nSale_total;
END COMPUTE_SALE_TOTAL;


Then, any time you need to know what the total for a sale is, you call this function.



Best of luck.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 21:38









Bob JarvisBob Jarvis

34.3k55985




34.3k55985













  • Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

    – DViga
    Jan 2 at 0:53











  • That's another good approach.

    – Bob Jarvis
    Jan 2 at 2:19



















  • Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

    – DViga
    Jan 2 at 0:53











  • That's another good approach.

    – Bob Jarvis
    Jan 2 at 2:19

















Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

– DViga
Jan 2 at 0:53





Thank you @Bob Jarvis for you advice and help. I will make view instead this trigger.

– DViga
Jan 2 at 0:53













That's another good approach.

– Bob Jarvis
Jan 2 at 2:19





That's another good approach.

– Bob Jarvis
Jan 2 at 2:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998985%2fhow-to-fix-trigger-or-procedure-in-oracle-express-to-automate-values-in-a-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'