473,840 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on trigger - work for update but not inserted

2 New Member
Good afternoon

i have created a trigger to update the sum of a field to another table. this works for updates but not for inserted. please help


here is the script
create trigger edp1
on opportunities_c ontacts
for insert, update, delete as
declare @x money
select @x = sum(a.amount)

from opportunities a, inserted b, opportunities_c ontacts c
where b.contact_id = c.contact_id
and a.id = c.opportunity_i d
update contacts_cstm
set contacts_cstm.b rr_actual_cury_ c = @x
from contacts_cstm, inserted b

where contacts_cstm.i d_c = b.contact_id

tables
Contacts
contacts_cstm
opportunity
opportunities_c ontacts c
contacts has a rec id = contact.id
contacts_cstm = contacts_cstm_i d_c which links to contacts
opportunity = opportunity.id
opportunities_c ontacts = id
opportunities_c ontacts.contact _id =contact_cstm.i d_c
opportunities_c ontacts.opportu nity_id=opportu nities.id

i need a sum of all the oppertunities.a mount per contact to update the contacts_cstm.b rr_actual_cury_ c with the sum amount of all the related oppertunities.

thank you
Jul 26 '07 #1
3 1613
bwestover
39 New Member
Use CODE tags around your code so its easier to read in the forums.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Code looks like this when CODE tags are used
  3.  
  4. Select Foo 
  5. From Bar
  6. Where Condition = TRUE
  7.  
  8.  
What is the error you get when you try an insert? Are you doing a single row insert, or multiline?
Jul 27 '07 #2
PaulvanZyl
2 New Member
sorry

Here is the script

[HTML]create trigger edp1
on opportunities_c ontacts
for insert, update, delete as
declare @x money
select @x = sum(a.amount)
from opportunities a, inserted b, opportunities_c ontacts c
where b.contact_id = c.contact_id
and a.id = c.opportunity_i d
update contacts_cstm
set contacts_cstm.b rr_actual_cury_ c = @x
from contacts_cstm, inserted b
where contacts_cstm.i d_c = b.contact_id[/HTML]

---Tables---
[HTML]Contacts
contacts_cstm
opportunity
opportunities_c ontacts c
contacts has a rec id = contact.id
contacts_cstm = contacts_cstm_i d_c which links to contacts
opportunity = opportunity.id
opportunities_c ontacts = id
opportunities_c ontacts.contact _id =contact_cstm.i d_c
opportunities_c ontacts.opportu nity_id=opportu nitie s.id[/HTML]

i need a sum of all the oppertunities.a mount per contact to update the contacts_cstm.b rr_actual_cury_ c with the sum amount of all the related oppertunities.

thank you
Jul 27 '07 #3
bwestover
39 New Member
Im still lost. Maybe someone else can help?

Im having a hard time decoding your table relationships.

If Im understanding your correctly your trying to generate a sum from one table based on the inserted ID and insert that sum into another table.

Does the record your trying for in the destination table alreay exist or do you need to create it?

You might need to create separate triggers for Insert and Update since you want to create a record in one instance, and Update an existing record in the other.
Aug 1 '07 #4

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

Similar topics

1
935
by: Thierry Marneffe | last post by:
Hello I would like to get the content of a field based in the field Name. Suppose a table with a field Named 'LastName' for wich there is a trigger after update I store the field name in a local variable Set @ColName = 'LastName' How can I retrieve the value of the @ColName from the inserted table using
3
1769
by: Curtis Gilchrist | last post by:
I'm trying my hand at triggers and it doesn't seem to be working for me. I have a very simple database that consists of one table: Employees. I want to create a trigger that will limit the EMP_TITLE field to either Ms., Mr., or Mrs. I am using the following code: CREATE trigger triTitleCheck ON employee FOR insert, update AS declare @v1 varchar
9
3468
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM, VARCHAR(10) WO.PROBLEMCODE, VARCHAR(8)
7
1769
by: rkrueger | last post by:
Given the following 3 Tables: CREATE TABLE ( NOT NULL , NOT NULL CONSTRAINT DEFAULT (getdate()), NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
4
1571
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 trTBL ON TBL FOR UPDATE, INSERT, DELETE as update TBL set fld = 'value' from inserted, TBL
3
3734
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code in the application, but I'd rather not! DDL for table and trigger below. TIA
15
2588
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
8
7888
by: Benzine | last post by:
Hi, I have an issue with my replication at the moment. I will try to describe the scenario accurately. I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect to the publisher to upload/download changes. I have a trigger set up on one table which updates another, here is an example of the trigger: "CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
4
2765
by: rcamarda | last post by:
I have a UDF that cleans a field of control characters and I use it like this select dbo.udf_CleanAlphaNum(Address1) as Address1 from Leads It works great. I use it to clean several fields from a vendors SQL server. The downside is I have to first load the data into my database so I can use my function to clean the data THEN proceed to load it into the destination table. I thought I could create a trigger on the
0
9860
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9699
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10301
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7838
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4076
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.