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

Problem in Update Trigger in SQL Server 7.0 - Need some help!

Hi for all on this...

I'm using MS SQL Server 7.0 SP4 in some customers to store some data
from an aplication developed by me.
I created an trigger to run on update.
When I run an update command on it's table and the where condition
returns only one row, the trigger is executed ok, but if where
condition returns more than one rows, the trigger don't run.

I don't know what happens there...
If any can help on this, I will be thankfull!

Regards!

Lucio
Jul 23 '05 #1
7 2032
Hi

You didn't post your code to it is hard to comment. Triggers need to cater
for multiple rows being effected by the statement that triggers them. It is
a common (and there are many examples of bad trigger code in these
newsgroups) such as this one http://tinyurl.com/aphnp

John

"Lucio Chiessi" <lu******************@vorio.eti.br> wrote in message
news:d9**********@domitilla.aioe.org...
Hi for all on this...

I'm using MS SQL Server 7.0 SP4 in some customers to store some data
from an aplication developed by me.
I created an trigger to run on update.
When I run an update command on it's table and the where condition
returns only one row, the trigger is executed ok, but if where
condition returns more than one rows, the trigger don't run.

I don't know what happens there...
If any can help on this, I will be thankfull!

Regards!

Lucio

Jul 23 '05 #2
Lucio Chiessi (lu******************@vorio.eti.br) writes:
I'm using MS SQL Server 7.0 SP4 in some customers to store some data
from an aplication developed by me.
I created an trigger to run on update.
When I run an update command on it's table and the where condition
returns only one row, the trigger is executed ok, but if where
condition returns more than one rows, the trigger don't run.

I don't know what happens there...
If any can help on this, I will be thankfull!


A common mistake is to believe that a trigger firers once per row. In
fact it fires once per statement.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
hI John. Thanks a lot for your reply!

I'm posting now my code. If you can help me, thanks a lot twice!

================================================== ===============

CREATE TRIGGER [Reg_Chg_Preco] ON [dbo].[PRECOS_ESPECIAIS]
FOR UPDATE
AS
Declare @cProdNo as char(10)
Declare @cFrClNo as char(10)
Declare @lIsCompra as bit
Declare @nPrecAnt as numeric(15,5)
Declare @nPrecNov as numeric(15,5)

IF Update(PRECO)
Begin
Select @nPrecAnt = del.PRECO from deleted as del
Select @cProdNo = ins.PRODNO, @cFrClNo = ins.FRCLNO,@nPrecNov =
ins.PRECO, @lIsCompra = ins.ISCOMPRA from inserted as ins
Insert into HIST_CHG_PRECO values
(GetDate(),@cProdNo,@cFrClNo,@lIsCompra,@nPrecAnt, @nPrecNov)
End
GO

================================================== ================

John Bell escreveu:
Hi

You didn't post your code to it is hard to comment. Triggers need to cater
for multiple rows being effected by the statement that triggers them.


Jul 23 '05 #4
Ooops!!

I think that triggers was fired once per row. Am I wrong so!?
My trigger code was made to be used once per row and not once per
statement.
There is another way to made trigger be fired once per row?

Thanks a lot for your replay!!

Lucio

Jul 23 '05 #5
Hi

This is almost exactly the same as the example in the link I posted,
although what is not clear is what your key is for the tables. If this is
PRECO and that is your only way of uniquely identifying a record, then you
may want to think creating an alternate key that will not change.

John
"Lucio Chiessi [VORio]" <lu***@vorio.eti.br> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
hI John. Thanks a lot for your reply!

I'm posting now my code. If you can help me, thanks a lot twice!

================================================== ===============

CREATE TRIGGER [Reg_Chg_Preco] ON [dbo].[PRECOS_ESPECIAIS]
FOR UPDATE
AS
Declare @cProdNo as char(10)
Declare @cFrClNo as char(10)
Declare @lIsCompra as bit
Declare @nPrecAnt as numeric(15,5)
Declare @nPrecNov as numeric(15,5)

IF Update(PRECO)
Begin
Select @nPrecAnt = del.PRECO from deleted as del
Select @cProdNo = ins.PRODNO, @cFrClNo = ins.FRCLNO,@nPrecNov =
ins.PRECO, @lIsCompra = ins.ISCOMPRA from inserted as ins
Insert into HIST_CHG_PRECO values
(GetDate(),@cProdNo,@cFrClNo,@lIsCompra,@nPrecAnt, @nPrecNov)
End
GO

================================================== ================

John Bell escreveu:
Hi

You didn't post your code to it is hard to comment. Triggers need to
cater
for multiple rows being effected by the statement that triggers them.

Jul 23 '05 #6
You can't make a trigger fire once per row in SQL 2000. As John mentioned,
you need a unique row identifier that does not change so that you can
correlate the before/after values in your update trigger. You can then use
a single insert statement in your trigger like the example below:

INSERT INTO HIST_CHG_PRECO
SELECT
GetDate(),
ins.PRODNO,
ins.FRCLNO,
ins.ISCOMPRA,
del.PRECO,
ins.PRECO
FROM inserted ins
JOIN deleted del ON ins.PK = del.PK

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Lucio Chiessi [VORio]" <lu***@vorio.eti.br> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Ooops!!

I think that triggers was fired once per row. Am I wrong so!?
My trigger code was made to be used once per row and not once per
statement.
There is another way to made trigger be fired once per row?

Thanks a lot for your replay!!

Lucio

Jul 23 '05 #7
Hi Dan.
Help so much!!! Solved my problem...
My thanks to you, John Bell and Erland Sommarskog.
I apreciate very much your help...

Best Regards from Rio de Janeiro - Brazil

Lucio

Dan Guzman escreveu:
You can't make a trigger fire once per row in SQL 2000. As John mentioned,
you need a unique row identifier that does not change so that you can
correlate the before/after values in your update trigger. You can then use
a single insert statement in your trigger like the example below:

INSERT INTO HIST_CHG_PRECO
SELECT
GetDate(),
ins.PRODNO,
ins.FRCLNO,
ins.ISCOMPRA,
del.PRECO,
ins.PRECO
FROM inserted ins
JOIN deleted del ON ins.PK = del.PK

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Lucio Chiessi [VORio]" <lu***@vorio.eti.br> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Ooops!!

I think that triggers was fired once per row. Am I wrong so!?
My trigger code was made to be used once per row and not once per
statement.
There is another way to made trigger be fired once per row?

Thanks a lot for your replay!!

Lucio


Jul 23 '05 #8

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

Similar topics

1
by: Dunc | last post by:
I'm new to Postgres, and getting nowhere with a PL/Perl trigger that I'm trying to write - hopefully, someone can give me some insight into what I'm doing wrong. My trigger is designed to reformat...
2
by: Manoj S. P. | last post by:
Hi Folks, My basic requirement is I want to write a trigger on a table based on certain conditions post-update from another table in another database. The actors in this scenario are:...
1
by: teddysnips | last post by:
SQL Server 2000 I have a stored procedure that uses an extended SPROC to send an email notification to my customers when a document is distributed. However, the SPROC has an unexpected side...
4
by: SUKRU | last post by:
Hello everybody. Unfortunately I am pretty new to sql-server 2000 I need some help with a Trigger I created. I created a trigger witch takes the id of the affected row and does a update on a...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
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...
3
by: silversubey | last post by:
I am trying to setup a trigger that sends an email if a field is changed to specific data. The trigger works when ever the field is changed, but I only need an email if the field is changed to 'In...
3
by: Yas | last post by:
Hi everyone I am trying to create a DELETE Trigger. I have 2 tables. Table1 and Table2. Table 2 has all the same fields and records as Table1 + 1 extra column "date_removed" I would like that...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
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
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...
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
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...
0
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,...

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.