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

Multiple Update Trigger Error.

I am trying to do a multi record update on a table but get an error from the update trigger.

Update -
Expand|Select|Wrap|Line Numbers
  1.  
  2. ALTER PROCEDURE [dbo].[uspOverviewUpdateCancelledFlag]
  3.     @ApplicationId int,
  4.     @Cancelled bit,
  5.     @LastUpdatedBy varchar(151)
  6.  
  7. AS
  8.  
  9. -- SET NOCOUNT ON added to prevent extra result sets from
  10. -- interfering with SELECT statements.
  11. SET NOCOUNT ON;
  12.  
  13. BEGIN
  14.     UPDATE dbo.tblOverview SET
  15.         cancelled = @Cancelled,
  16.         last_updated_by = @LastUpdatedBy,
  17.         last_updated_date = GETDATE(),
  18.         revision = revision + 1
  19.     WHERE
  20.         application_id = @ApplicationId
  21.     AND    ((overview_type_id = 1) OR (overview_type_id = 2 AND requirement_id != 13))    
  22.  
  23. END
  24.  
  25. RETURN
  26.  
  27.  
Trigger -

Expand|Select|Wrap|Line Numbers
  1. declare @Changes varchar(max)
  2. set @Changes = ''
  3.  
  4.         if ((select application_id from deleted) != (select application_id from inserted)  or 
  5.             (select application_id from deleted) IS NULL and (select application_id from inserted) IS NOT NULL)
  6.             set @Changes = @Changes + 'application_id  from [' + ISNULL(convert(varchar(100),(select 
  7.         application_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select application_id from inserted)),'Null') + '] '
  8.  
  9.         if ((select overview_type_id from deleted) != (select overview_type_id from inserted)  or 
  10.             (select overview_type_id from deleted) IS NULL and (select overview_type_id from inserted) IS NOT NULL)
  11.             set @Changes = @Changes + 'overview_type_id  from [' + ISNULL(convert(varchar(100),(select 
  12.         overview_type_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select overview_type_id from inserted)),'Null') + '] '
  13.  
  14.         if ((select requirement_id from deleted) != (select requirement_id from inserted)  or 
  15.             (select requirement_id from deleted) IS NULL and (select requirement_id from inserted) IS NOT NULL)
  16.             set @Changes = @Changes + 'requirement_id  from [' + ISNULL(convert(varchar(100),(select 
  17.         requirement_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select requirement_id from inserted)),'Null') + '] '
  18.  
  19.         if ((select deadline_date from deleted) != (select deadline_date from inserted)  or 
  20.             (select deadline_date from deleted) IS NULL and (select deadline_date from inserted) IS NOT NULL)
  21.             set @Changes = @Changes + 'deadline_date  from [' + ISNULL(convert(varchar(100),(select 
  22.         deadline_date from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select deadline_date from inserted)),'Null') + '] '
  23.  
  24.         if ((select description from deleted) != (select description from inserted)  or 
  25.             (select description from deleted) IS NULL and (select description from inserted) IS NOT NULL)
  26.             set @Changes = @Changes + 'description  from [' + ISNULL(convert(varchar(100),(select 
  27.         description from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select description from inserted)),'Null') + '] '
  28.  
  29.         if ((select completed from deleted) != (select completed from inserted)  or 
  30.             (select completed from deleted) IS NULL and (select completed from inserted) IS NOT NULL)
  31.             set @Changes = @Changes + 'completed  from [' + ISNULL(convert(varchar(100),(select 
  32.         completed from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select completed from inserted)),'Null') + '] '
  33.  
  34.         if ((select cancelled from deleted) != (select cancelled from inserted)  or 
  35.             (select cancelled from deleted) IS NULL and (select cancelled from inserted) IS NOT NULL)
  36.             set @Changes = @Changes + 'cancelled  from [' + ISNULL(convert(varchar(100),(select 
  37.         cancelled from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select cancelled from inserted)),'Null') + '] '
  38.  
  39. if (@Changes != '')
  40. BEGIN
  41. INSERT INTO dbo.tblAudit
  42. (
  43.     table_name,
  44.     row_id,
  45.     row_revision,
  46.     operation,
  47.     user_name,
  48.     date_time,
  49.     changes,
  50.     application_reference
  51. )
  52. select    'tblOverview',
  53.     inserted.id,
  54.     inserted.revision,
  55.     'Update',
  56.     inserted.last_updated_by,
  57.     inserted.last_updated_date,
  58.     @Changes,
  59.     application_reference
  60. from    inserted INNER JOIN
  61. tblapplication on tblapplication.id = application_id
  62.  
  63. END
  64.  
  65.  
The error I get when trying to update 2 records is -

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..

Can anyone help.

Thanks

Kevin.
Sep 1 '10 #1

✓ answered by ck9663

Is this code working before and you just experience the error recently?

What do you mean by:

Expand|Select|Wrap|Line Numbers
  1. (select application_id from deleted) != (select application_id from inserted)
  2.  
If you're trying to compare the old and new value of the application_id, you're going to have JOIN the two tables.

Trigger fires once for every update statement, not every updated record.

Also, I don't think you're allowed to use set to assign a value to a variable if the value is coming from a query returning multiple record set. Here's more with that.

Happy Coding!!!

~~ CK

3 1688
wizardry
201 100+
what i dont see before the subquery or after is that values clause.

insert into table_name(columns) values (columns data)
Sep 1 '10 #2
ck9663
2,878 Expert 2GB
Is this code working before and you just experience the error recently?

What do you mean by:

Expand|Select|Wrap|Line Numbers
  1. (select application_id from deleted) != (select application_id from inserted)
  2.  
If you're trying to compare the old and new value of the application_id, you're going to have JOIN the two tables.

Trigger fires once for every update statement, not every updated record.

Also, I don't think you're allowed to use set to assign a value to a variable if the value is coming from a query returning multiple record set. Here's more with that.

Happy Coding!!!

~~ CK
Sep 1 '10 #3
I rewrote the procedure that updates the table so that it updates each record separately, then the trigger is actioned each time.
Sep 7 '10 #4

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

Similar topics

1
by: Franklin Bowen | last post by:
Are there any limitations or gotchas to updating the same table which fired a trigger from within the trigger? Some example code below. Hmmm.... This example seems to be working fine so it...
7
by: Justin | last post by:
I am extremely new at SQL Server2000 and t-sql and I'm looking to create a simple trigger. For explanation sake, let's say I have 3 columns in one table ... Col_1, Col_2 and Col_3. The data type...
1
by: m3ckon | last post by:
Hi there, I'm a little stuck and would like some help I need to create an update trigger which will run an update query on another table. However, What I need to do is update the other...
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
1
by: Simon Holmes | last post by:
Hi, I am having trouble calling a UDF from a 'before update' trigger whereas I have no problems calling it from the 'after update' trigger. The trigger is as below : CREATE TRIGGER foo NO...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
13
by: dennis | last post by:
Hello, I'm having trouble solving the following problem with DB2 UDB 8.2. I need to create a trigger that performs certain extra constraint validations (temporal uniqueness). One of the tables...
1
by: Ryandmcbee | last post by:
I am trying to write an update trigger that will send one field from one table (header) to update one field in another table (multiple rows though). Here is what I have so far, the syntax is...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
0
by: MalingreM | last post by:
Hi, I'm quite new to sql2005, and I've the following problem. When I insert records in table Data one by one, the insert/update trigger fires correctly, but: when i insert multiple records at once;...
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
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: 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...
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
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,...
0
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...

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.