473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Calculating Trigger Syntax

I'm a newbee to the community and to writing sql. I would like to write a new trigger for some fields in a table I created and was hoping someone could help me. I know how to start a new tigger through enterprise manager, but it's the syntax that I'm not familiar with at all. Basically, I have three number fields for each particular ID. I want to add all these three fields up into a "total field" I would like to have a trigger to do the calculation when at least one of the three number fields is populated. Any ideas? Your help is very greatly appreciated. Thanks so much.

newsqler
Dec 26 '06 #1
10 3677
ronverdonk
4,258 Expert 4TB
For the MySQL trigger syntax, and creating and dropping triggers, see Chapter 19. Triggers . Be aware that this works on MySQL 5.1 and up only!

Ronald :cool:
Dec 27 '06 #2
Thanks, I'll check this out. I'm sorry, I should have mentioned that I'm using SQL 2000. Will that make a difference? Thanks for your help.
Dec 27 '06 #3
ronverdonk
4,258 Expert 4TB
Then you have asked your question in the wrong forum. Therefore I have shown you the MySQL information.
For SQL Server 2000 you must look in the appropriate forum. I will transfer this question to that forum now.

Ronald :cool:
Dec 27 '06 #4
almaz
168 Expert 100+
For your task calculated field is the best choice:
Expand|Select|Wrap|Line Numbers
  1. create table #a
  2. (
  3.  id int identity(1, 1),
  4.  val1 int not null default (0),
  5.  val2 int not null default (0),
  6.  val3 int not null default (0),
  7.  total as (val1 + val2 + val3) 
  8. )
  9.  
  10. insert #a default values
  11. insert #a default values
  12. insert #a default values
  13. insert #a default values
  14.  
  15. select * from #a
  16. update #a set val2=id
  17. select * from #a
  18.  
Dec 27 '06 #5
This is great, thank you so much for your help. So, using your code, I already have a table called 'TABLE1' and three fields in that same table 'FIELD1', 'FIELD2', 'FIELD3' that I want to sum into 'FIELD4'. How would I use your code with this scenario? Thanks again so much.
Jan 2 '07 #6
almaz
168 Expert 100+
You should create FIELD4 as a calculated field. For your scenario it will be:
Expand|Select|Wrap|Line Numbers
  1. alter table TABLE1 drop column FIELD4
  2. alter table TABLE1 add FIELD4 as (FIELD1 + FIELD2 + FIELD3)
Jan 3 '07 #7
I tried this out and got an error message. Here is the code and the error message I received:

code:

CREATE TRIGGER [name_of_trigger] ON [dbo].[table1]
FOR UPDATE
AS

alter table table1 drop column field4
alter table table1 add field4 as (field1 + field2 + field3)

error message:

odbc--update on a linked table 'dbo_table1' failed.
[microsoft][odbc sql server driver][SQL server] alter table drop column failed because column 'field4' does not exist in table 'table1' (#4924)

Any ideas? Thanks so much.
Jan 3 '07 #8
almaz
168 Expert 100+
it should not be a trigger, just execute it once and that's all
Jan 4 '07 #9
However, I need it as a trigger because records will be changing all the time. the trigger would help us calculate the total of the three fields every time any of the fields change.
Jan 4 '07 #10
b1randon
171 Expert 100+
However, I need it as a trigger because records will be changing all the time. the trigger would help us calculate the total of the three fields every time any of the fields change.
A calculated field's value is dynamic and saves the overhead of rewriting the record every time. The calculated field will update itself as the values are added.
Jan 4 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Fernand St-Georges | last post by:
Hi, can someone tell how to write a Trigger; I am familiar with Sybase Sql Anywhere trigger syntax. Actually I have three tables MEMBER, CONTRACT and PAYMENT I need to update the MEMBER.BALANCE...
2
by: William F. O'Neill | last post by:
Am using SQL Server 2000 on WINXP Pro. Have a requirement to change some Oracle triggers to SQL 2000. I have modied this one Insert Trigger, but get an error when I attempt to compile: CREATE...
4
by: nosbtr1 | last post by:
When a row gets modified and it invokes a trigger, we would like to be able to update the row that was modified inside the trigger. This is (basically) how we are doing it now: CREATE TRIGGER...
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
12
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
5
by: Bob Stearns | last post by:
I have two (actually many) dates in a table I want to validate on insertion. The following works in the case of only one WHEN clause but fails with two (or more), with the (improper?...
3
by: Axel | last post by:
Hello, I have detail sections in several subforms that are used to fill daily order data per product. Each row contains textboxes for the weekdays and a locked textbox (txtTotal) for the week's...
1
by: deepdata | last post by:
Hi, I am creating a trigger in DB2 express version. When i use the following syntax to create trigger CREATE TRIGGER USER_PK_TRIGGER BEFORE INSERT On users REFERENCING NEW As N FOR EACH...
4
zachster17
by: zachster17 | last post by:
Hello all, I have 2 tables (tblAddProviderProfessional and tblAddProvider). There are other tables such as tblAddProviderFacility and so forth. tblAddProvider is a table that has similar data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.