473,407 Members | 2,320 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.

help needed with update trigger in SQL Server 2005

Hi,
I am trying to write a trigger that will run when someone updates the delivery charge within the specified table. It will work, but will only record one change to the table. My question is, if multiple records are changes with an update, how do I get the trigger to record all the changes, instead of one?
Thank You,
Jason


Create Table DeliveryType (
DeliveryTypeID int not null
Constraint pk_DeliveryType_DeliveryTypeID primary key,
DeliveryTypeDescription varchar(10) not null,
DeliveryCharge smallmoney not null
)

Create Table DeliveryTypeChanges (
ChangeID int identity(1,1) not null
Constraint pk_DeliveryTypeChanges_ChangeID primary key,
ChangeDateTime datetime not null,
DeliveryTypeDescription varchar(10) not null,
OldDeliveryCharge smallmoney not null,
NewDeliveryCharge smallmoney not null
)

CREATE Trigger tr_Update_RecordDeliveryChargeChanges
On DeliveryType
For Update As
declare @rowcount as int
declare @ChangeDateTime as datetime
declare @DeliveryTypeDescription as varchar(10)
declare @OldDeliveryCharge as smallmoney
declare @NewDeliveryCharge as smallmoney
If @@RowCount > 0
Begin
If update (DeliveryCharge)
Begin
begin transaction
select @ChangeDateTime = getdate(),
@DeliveryTypeDescription = Inserted.DeliveryTypeDescription,
@OldDeliveryCharge = Deleted.DeliveryCharge,
@NewDeliveryCharge = Inserted.DeliveryCharge
from Inserted inner join Deleted
on Inserted.DeliveryTypeID = Deleted.DeliveryTypeID
insert DeliveryTypeChanges(ChangeDateTime, DeliveryTypeDescription, OldDeliveryCharge, NewDeliveryCharge)
values (@ChangeDateTime, @DeliveryTypeDescription, @OldDeliveryCharge, @NewDeliveryCharge)
set @rowcount = @@rowcount
if @@rowcount = 1
Begin
Commit transaction
End
Else
Begin
Rollback transaction
RaisError('Error recording to change table, contact admin', 16, 1)
End
End
End
Return
Apr 8 '07 #1
3 20013
Hi,
I am trying to write a trigger that will run when someone updates the delivery charge within the specified table. It will work, but will only record one change to the table. My question is, if multiple records are changes with an update, how do I get the trigger to record all the changes, instead of one?
Thank You,
Jason


Create Table DeliveryType (
DeliveryTypeID int not null
Constraint pk_DeliveryType_DeliveryTypeID primary key,
DeliveryTypeDescription varchar(10) not null,
DeliveryCharge smallmoney not null
)

Create Table DeliveryTypeChanges (
ChangeID int identity(1,1) not null
Constraint pk_DeliveryTypeChanges_ChangeID primary key,
ChangeDateTime datetime not null,
DeliveryTypeDescription varchar(10) not null,
OldDeliveryCharge smallmoney not null,
NewDeliveryCharge smallmoney not null
)

CREATE Trigger tr_Update_RecordDeliveryChargeChanges
On DeliveryType
For Update As
declare @rowcount as int
declare @ChangeDateTime as datetime
declare @DeliveryTypeDescription as varchar(10)
declare @OldDeliveryCharge as smallmoney
declare @NewDeliveryCharge as smallmoney
If @@RowCount > 0
Begin
If update (DeliveryCharge)
Begin
begin transaction
select @ChangeDateTime = getdate(),
@DeliveryTypeDescription = Inserted.DeliveryTypeDescription,
@OldDeliveryCharge = Deleted.DeliveryCharge,
@NewDeliveryCharge = Inserted.DeliveryCharge
from Inserted inner join Deleted
on Inserted.DeliveryTypeID = Deleted.DeliveryTypeID
insert DeliveryTypeChanges(ChangeDateTime, DeliveryTypeDescription, OldDeliveryCharge, NewDeliveryCharge)
values (@ChangeDateTime, @DeliveryTypeDescription, @OldDeliveryCharge, @NewDeliveryCharge)
set @rowcount = @@rowcount
if @@rowcount = 1
Begin
Commit transaction
End
Else
Begin
Rollback transaction
RaisError('Error recording to change table, contact admin', 16, 1)
End
End
End
Return


Hi,

Theres no problem with your script. I tried the multiple update in my database and found no problem with your trigger.
Apr 9 '07 #2
iburyak
1,017 Expert 512MB
Try this trigger instead:

[PHP]CREATE Trigger tr_Update_RecordDeliveryChargeChanges
On DeliveryType
For Update As


If update (DeliveryCharge)
Begin

Insert DeliveryTypeChanges(ChangeDateTime, DeliveryTypeDescription, OldDeliveryCharge, NewDeliveryCharge)
Select getdate(), Inserted.DeliveryTypeDescription,Deleted.DeliveryC harge,Inserted.DeliveryCharge
From Inserted inner join Deleted
on Inserted.DeliveryTypeID = Deleted.DeliveryTypeID

If @@error <> 0
Begin
RaisError('Error recording to change table, contact admin', 16, 1)
End
End[/PHP]

There is no point to have a transaction for one statement so I removed it.

This way it does record multiple updates and return informative error message in case of an error.
The way you did it - you stored values in variables before insert which can hold one value at a time.

Good Luck.
Irina.
Apr 9 '07 #3
In SQL Server you need to loop all rows (using INSERTED) and process them.
Oct 18 '10 #4

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

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
3
by: V T | last post by:
Hello all, SQL Server 2000 documentation http://www.microsoft.com/technet/prodtechnol/sql/2000/reskit/part10/c3761.mspx states that if view is using "NOT NULL" columns of a base table, then...
2
by: Dima Gofman | last post by:
I have a trigger on UPDATE on a table. I'm running some maintenance UPDATE and DELETE queries which I want the trigger to ignore but at the same time I want other UPDATE queries that other users...
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...
1
by: rdraider | last post by:
Hi all, I know squat about triggers so was hoping somebody could point me in the right direction. I wanted to copy an email address field from a salesman table to a note field in a customer...
0
by: gshawn3 | last post by:
Hi, I am having a hard time creating a Trigger to update an Oracle database. I am using a SQL Server 2005 Express database on a Win XP Pro SP2 desktop, linked to an Oracle 10g database on a...
2
by: dba_222 | last post by:
Dear Experts, I'm an Oracle guy, who is being given more SQL Server assignments lately. I've been looking for things on the web about this, but I can't anything so far. In Oracle, I you...
9
by: dotnetfellow | last post by:
The system has worked for two years. SQL Server 2000 runs on Windows 2000 Server "A". Another instance of SQL Server 2000 was moved from Windows 2000 Server "B" old to Windows 2003 Server "B"...
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...
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: 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
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,...
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
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,...

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.