Thread View: borland.public.interbase.sql
10 messages
10 total messages
Started by Isaac
Sat, 17 May 2008 23:01
trigger to get sum
Author: Isaac
Date: Sat, 17 May 2008 23:01
Date: Sat, 17 May 2008 23:01
27 lines
1274 bytes
1274 bytes
Hello,
I am new to triggers and I wonder if I can get sum of a field in the
table (a) for the current item (f1) and then update another field in
table (b) like that:
Table a
f1 pk (id)
f2 float
table b
fb1 pk
fb2 float <<--- need update
trigger body:
AS
DECLARE VARIABLE sum_var float;
begin
sum_var = select sum(f2) from table_a
where f1 = ?;
update table_b set fb2 = sum_var where fb1 = ??;
end
Please advice
Thanks
Re: trigger to get sum
Author: Bill Todd [TeamB]
Date: Sun, 18 May 2008 00:57
Date: Sun, 18 May 2008 00:57
17 lines
1472 bytes
1472 bytes
The code below shows the correct syntax for the SELECT statment. What
do the ? and ?? in the WHERE clauses represent? Are you going to pass
these values in as input parameters? You might find it helpful to read
the chapters on stored procedures and triggers in the Data Definition
Guide.
AS
DECLARE VARIABLE sum_var float;
begin
select sum(f2) from table_a
where f1 = ? into :sum_var;
update table_b set fb2 = sum_var where fb1 = ??;
end
--
Bill Todd (TeamB)
Re: trigger to get sum
Author: Isaac
Date: Sun, 18 May 2008 13:05
Date: Sun, 18 May 2008 13:05
11 lines
1240 bytes
1240 bytes
Ok thanks but how I can pass the current record value to the select
parameter ?
select sum(f2) from table_a
where f1 = {current record value of table_a)
- Which is better to let the trigger do this or to create a procedure
and execute it ?
sorry but I cant find an answer in the manual
Thanks
Re: trigger to get sum
Author: Bill Todd [TeamB]
Date: Sun, 18 May 2008 15:39
Date: Sun, 18 May 2008 15:39
33 lines
2154 bytes
2154 bytes
Isaac wrote: > Ok thanks but how I can pass the current record value to the select > parameter ? > A trigger executes when you make a change to a record in a table. You can access all of the field values in the record that caused the trigger to fire using the OLD and NEW context variables. In a before insert trigger only the NEW values exist; in a before delete trigger only the OLD values exist; in a before update trigger both the OLD and NEW values exist. Assuming a before update trigger then OLD.FIELD1 is the original value in FIELD1 and NEW.FIELD1 is the new value of FIELD1. This is all explained in the Data Definition Guide. > > - Which is better to let the trigger do this or to create a procedure > and execute it ? Since I have no idea what you are trying to do or when you are trying to do it I do not know whether you should use a stored procedure or trigger. > > sorry but I cant find an answer in the manual The OLD and NEW context variables are explained on page 10-8 of the Data Definition Guide. -- Bill Todd (TeamB)
Re: trigger to get sum
Author: Isaac
Date: Sun, 18 May 2008 17:19
Date: Sun, 18 May 2008 17:19
10 lines
1427 bytes
1427 bytes
Here is what I am trying to do: I have a table that will store average cost for an item so I want to sum all quantities and divide on total cost for that item, this average cost is giving me headache I also need to sum the beginning balance quantity and cost on all purchase orders all this should be done on the fly whenever an item quantity or purchase price is changed in any order. Thanks
Re: trigger to get sum
Author: Isaac
Date: Sun, 18 May 2008 19:27
Date: Sun, 18 May 2008 19:27
8 lines
1268 bytes
1268 bytes
Ok I wrote on trigger it fires after updating the table and it works good but I need the same calculations to be executed after insert and after delete is it ok to make the same trigger exactly 3 times ? - How this will interfere with other updates done inside the application it self ? Thanks
Re: trigger to get sum
Author: Bill Todd [TeamB]
Date: Sun, 18 May 2008 20:34
Date: Sun, 18 May 2008 20:34
19 lines
1763 bytes
1763 bytes
Isaac wrote: > Ok I wrote on trigger it fires after updating the table and it works > good but I need the same calculations to be executed after insert and > after delete is it ok to make the same trigger exactly 3 times ? Yes. > > - How this will interfere with other updates done inside the > application it self ? It does not interfere with anything. Any time an insert, update or delete is performed the appropriate trigger will fire. It does not matter what causes the data to change. The trigger will always execute. -- Bill Todd (TeamB)
Re: trigger to get sum
Author: Isaac
Date: Sun, 18 May 2008 21:37
Date: Sun, 18 May 2008 21:37
1 lines
1129 bytes
1129 bytes
can I write one procedure and call it from 3 triggers ?
Re: trigger to get sum
Author: Isaac
Date: Sun, 18 May 2008 22:00
Date: Sun, 18 May 2008 22:00
10 lines
1280 bytes
1280 bytes
Isaac wrote: > can I write one procedure and call it from 3 triggers ? never mind ?EXECUTE PROCEDURE proc_name [var [, var �]] [RETURNING_VALUES var [, var �]] :)
Re: trigger to get sum
Author: Bill Todd [TeamB]
Date: Sun, 18 May 2008 23:04
Date: Sun, 18 May 2008 23:04
8 lines
1307 bytes
1307 bytes
Isaac wrote: > can I write one procedure and call it from 3 triggers ? Yes. -- Bill Todd (TeamB)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads