Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2004, 11:36 AM
Authorized User
 
Join Date: Mar 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with update trigger

Hi all

Appreciate any help with this one, I am trying to implement a simple audit trail within a SQL2k database so I can see who is changing what

The main table the user updates is called drivers and the audit table is called simply audit.

My trigger code is this
create trigger trdrivers on [drivers] for update as
declare @old nVarChar(255)
declare @new nVarChar(255)
declare @rn int
declare @user nVarChar(10)
declare @datetime datetime
select @datetime=getdate()

select @rn=(select record_number from inserted)
select @user=(Select changed_by from inserted)
if update(salutation)
begin
    select @old=(select salutation from deleted)
    select @new=(select salutation from Inserted)
    insert into audit (audit_comment,host_record,audit_old_value,audit_n ew_value,audit_user,audit_date) values ('Salutation',@rn,@old,@new,@user,@datetime)
end

If I issue this update statement:- Update drivers set salutation='GB', occupation='dogsbody' where driver_code='123'
All works fine, I get two records written to the audit trail and one driver record updated.

Now if issue this: - update drivers set occupation='Staff' the query should affect loads of records but it fails with "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
" The error points to the select @rn line in the trigger.

If I understand the BOL correctly it says that the trigger should fire once for each record so Inserted and Deleted should only ever have either 0 or 1 row.

Anybody got any ideas?

Best wishes
Graham
 
Old September 2nd, 2004, 07:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Regarding error - Subquery returned more than 1 value

It is because you are trying to update more than one records and thereby, your magic tables, DELETED and INSERTED would have more than one records. So select @rn=(select record_number from inserted) fails, due to return of many values.

Let me look into your problem 1 and get back to you.

Cheers!



_________________________
- Vijay G
Strive for Perfection
 
Old September 4th, 2004, 12:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Regarding your 1st problem, I don't see any reason why you get 2 rows inserted into Audit table.
Quote:
quote:If I understand the BOL correctly it says that the trigger should fire once for each record so Inserted and Deleted should only ever have either 0 or 1 row.
When you issue an UPDATE statement that results in updation of more than 1 row, then INSERTED would have all the affected rows with NEW values, and DELETED would have all the affected rows with old values. It is not that they both have 0 or 1 row at any point of time. It depends on the update statement that was issued.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
After Update Trigger debbiecoates SQL Server 2000 2 February 15th, 2008 04:55 AM
Create trigger on update bvpsekhar MySQL 23 May 4th, 2007 01:01 AM
Trigger update problem (urgent) msbsam SQL Server 2000 6 February 26th, 2007 02:39 AM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.