473,388 Members | 1,552 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,388 software developers and data experts.

to trigger or not to trigger

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 sum. The week's sum is
also contained in the underlying table and I would like to update it
whenever any of the MonOrder .. SunOrder are changed. I believe the
easiest way to do this is in the backend (SQL Server) using a trigger
(?) - it would save me to go through the many subforms and finding the
event that is triggered when txtTotal.Value changes.

If there is a simple alternative to using a trigger, please tell me.

I think it can be achieved with a simple trigger that updates a row in
one table based on updates of corresponding fields of same row. Its a
"week total" field that sums up values of 7 singular "day" fields. Here
is what I have come up with using the TSQL documentation.

CREATE TRIGGER trigger_stocksum ON tblStock FOR UPDATE
AS
IF UPDATE(MonOrder) OR UPDATE(TueOrder) OR UPDATE(WedOrder) OR
UPDATE(ThursOrder) OR UPDATE(FriOrder) OR UPDATE(SatOrder) OR
UPDATE(SunOrder)
SET TotalOrder =
(MonOrder+TueOrder+WedOrder+ThursOrder+FriOrder+Sa tOrder+SunOrder)

all these fields (including TotalOrder) are contained in tblStock, I
get a syntax error
Server: Msg 170, Level 15, State 1, Procedure trigger_stocksum, Line 4
Line 4: Incorrect syntax near '='.

Can somebody please help me?

Nov 13 '05 #1
3 2073
Hi group,

after some research on
http://groups.google.ie/group/comp.d...qlserver?hl=en

I have opted for changing the field to a computed field.
this is the new table
(snipped):

<snip>

ALTER TABLE dbo.tblStock
DROP CONSTRAINT DF_tblStock_SunOrder
GO
CREATE TABLE dbo.Tmp_tblStock
(
YearWeek int NOT NULL,
ProductCode nvarchar(50) NOT NULL,
StockonHand int NULL,
StockonOrder int NULL,
TotalOrder AS
MonOrder+TueOrder+WedOrder+ThursOrder+FriOrder+Sat Order+SunOrder,

MonOrder int NULL,
TueOrder int NULL,
WedOrder int NULL,
ThursOrder int NULL,
FriOrder int NULL,
SatOrder int NULL,
SunOrder int NULL,
Dummy1 nvarchar(50

) NULL
) ON [PRIMARY]
GO
<snip>
the reason I can not normalize the table so easily (as much as I would
like to) is that it is currenty used in at least 22 queries and 11
forms in the frontend. It is used during Stock upload (importing a raw
table which is normalized in the same way) and stock reporting and in
all cases the separate storage of days in week fields seems to be quite
intuitive and performance appears to work quite well, so I am not too
bothered about changing it any time soon. Apart from that the data of
this table is not used anywhere else so this is more like a view
anyway. I am quite glad though that I did not create a trigger as it
would not work on row level anyway and would decrease performance.

hth
axel

Nov 13 '05 #2
On 2 Jun 2005 04:37:44 -0700, "Axel" <ax***@gofree.indigo.ie> wrote:
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 sum. The week's sum is
also contained in the underlying table and I would like to update it
whenever any of the MonOrder .. SunOrder are changed. I believe the
easiest way to do this is in the backend (SQL Server) using a trigger
(?) - it would save me to go through the many subforms and finding the
event that is triggered when txtTotal.Value changes.

If there is a simple alternative to using a trigger, please tell me.

I think it can be achieved with a simple trigger that updates a row in
one table based on updates of corresponding fields of same row. Its a
"week total" field that sums up values of 7 singular "day" fields. Here
is what I have come up with using the TSQL documentation.

CREATE TRIGGER trigger_stocksum ON tblStock FOR UPDATE
AS
IF UPDATE(MonOrder) OR UPDATE(TueOrder) OR UPDATE(WedOrder) OR
UPDATE(ThursOrder) OR UPDATE(FriOrder) OR UPDATE(SatOrder) OR
UPDATE(SunOrder)
SET TotalOrder =
(MonOrder+TueOrder+WedOrder+ThursOrder+FriOrder+S atOrder+SunOrder)

all these fields (including TotalOrder) are contained in tblStock, I
get a syntax error
Server: Msg 170, Level 15, State 1, Procedure trigger_stocksum, Line 4
Line 4: Incorrect syntax near '='.

Can somebody please help me?

Hi
Why store the sum in a table at all? Normal practice would be to
calculate it in a view or query.
If you are using forms, the afterupdate events can be used in place of
triggers.
David
Nov 13 '05 #3
> Why store the sum in a table at all? Normal practice would be to calculate it in a view or query.

Hi David,

true. The only reason why I did it this way was that I did not have
time to go through the frontend Access database (my colleague had
written most of this) to find all the places where the field was used.
But since it now is a calculated field it does not even take any
storage space, so I think thats a pretty neat solution.

I did not create a view because I have never done that before - and I
already had 'wasted' over an hour to investigate triggers and their
syntax.

In hindsight I could probably also have created a pass through query in
Access to replace the original table - which would have lead to the
same goal. For me it was important not to break the existing code or to
have the customer relink any tables.

regards,
Axel

PS: I am using google's web interface to create this message any idea
how I can hide my email addy? According to this page
"The group you are posting to is a Usenet group. Messages posted to
this group will make your email visible to anyone on the Internet."
To rephrase the question, does anybody know how I can add this group to
my thunderbird newsgroups?

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mary | last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger" table containing numerous triggers - each of which is activated by an UPDATE to a different column of this "trigger" table. When...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.