Re: Sum a column every time a record is created

Tech-Archive recommends: Fix windows errors by optimizing your registry




ecnalch wrote:
I want to sum the Quantity field each time I create a record. I am defaulting
a 1 in the Quantity field

The DEFAULT must be a scalar (i.e. a single value) and either a literal
value (e.g. not a calculation) or a niladic function (e.g. DATE()).

Consider this table

CREATE TABLE Test (
amount INTEGER DEFAULT 0 NOT NULL,
CHECK (amount >= 0),
total_amount_when_added INTEGER DEFAULT 0 NOT NULL,
CHECK (total_amount_when_added >= 0)
);

To determine the value for the total_amount_when_added column on
insert, you could create a SQL procedure (parameterized query):

CREATE PROCEDURE TestProc (
new_amount INTEGER = 1
) AS
INSERT INTO Test (amount, total_amount_when_added)
SELECT new_amount AS amount,
IIF(SUM(amount) IS NULL, 0, SUM(amount))
+ IIF(new_amount IS NULL, 1, new_amount)
AS total_amount_when_added
FROM Test;

You would then remove INSERT permissions from the base table and grant
them to the procedure. This way, you would ensure that all inserts to
the table go via your procedure i.e. the value in the
total_amount_when_added column is under the control of the database
designer, not the front end code.

Jamie.

--

.



Relevant Pages

  • Re: Perl Help For Newbie -- SOLVED
    ... > I want to take the first amount and multiply it by 1.5 and replace it, ... > I am brand new to Perl but have been reading and experimenting for the ... # assign each item in array to scalar then do stuff to scalar ...
    (freebsd-questions)
  • Re: Boolean tests [was Re: Attack a sacred Python Cow]
    ... This function is also wrong assuming that because amount compare to zero, ... here (he already expect a scalar due to the name of the variable). ... This is exactly the problem ABC is intended to solve. ...
    (comp.lang.python)
  • Re: Questions (Space)
    ... A scalar is just an amount: something that you can represent with a ... Speed is a scalar because you just need one number to specify ... So is mass: you just say that something has a mass ... (I'm sticking to the easier questions in this ...
    (rec.arts.sf.composition)
  • Re: WPEX,,,,LOL
    ... calculation with a majority of the sites I have dealt with. ... earning from other players that I recommend to sites. ... base my opinion on what little experience I have earning rakeback. ... not 10% of the amount collected at the tables I played on. ...
    (rec.gambling.poker)
  • Re: I need some help with a research project
    ... to help get readmitted to the University of Texas at Austin Aerospace Engineering Ph.D program. ... This orbit is characterized by a specific total amount of fuel used and the total time of flight. ... One must then make time/fuel cost tradeoffs to ... There is, then, no need for a calculation much better than that, and certainly not to ten significant digits. ...
    (comp.lang.fortran)