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

Help on trigger - work for update but not inserted

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_contacts
for insert, update, delete as
declare @x money
select @x = sum(a.amount)

from opportunities a, inserted b, opportunities_contacts c
where b.contact_id = c.contact_id
and a.id = c.opportunity_id
update contacts_cstm
set contacts_cstm.brr_actual_cury_c = @x
from contacts_cstm, inserted b

where contacts_cstm.id_c = b.contact_id

tables
Contacts
contacts_cstm
opportunity
opportunities_contacts c
contacts has a rec id = contact.id
contacts_cstm = contacts_cstm_id_c which links to contacts
opportunity = opportunity.id
opportunities_contacts = id
opportunities_contacts.contact_id =contact_cstm.id_c
opportunities_contacts.opportunity_id=opportunitie s.id

i need a sum of all the oppertunities.amount per contact to update the contacts_cstm.brr_actual_cury_c with the sum amount of all the related oppertunities.

thank you
Jul 26 '07 #1
3 1599
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
sorry

Here is the script

[HTML]create trigger edp1
on opportunities_contacts
for insert, update, delete as
declare @x money
select @x = sum(a.amount)
from opportunities a, inserted b, opportunities_contacts c
where b.contact_id = c.contact_id
and a.id = c.opportunity_id
update contacts_cstm
set contacts_cstm.brr_actual_cury_c = @x
from contacts_cstm, inserted b
where contacts_cstm.id_c = b.contact_id[/HTML]

---Tables---
[HTML]Contacts
contacts_cstm
opportunity
opportunities_contacts c
contacts has a rec id = contact.id
contacts_cstm = contacts_cstm_id_c which links to contacts
opportunity = opportunity.id
opportunities_contacts = id
opportunities_contacts.contact_id =contact_cstm.id_c
opportunities_contacts.opportunity_id=opportunitie s.id[/HTML]

i need a sum of all the oppertunities.amount per contact to update the contacts_cstm.brr_actual_cury_c with the sum amount of all the related oppertunities.

thank you
Jul 27 '07 #3
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
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...
3
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...
9
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,...
7
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
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...
3
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...
15
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...
8
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...
4
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.