I am trying to do a multi record update on a table but get an error from the update trigger.
Update - -
-
ALTER PROCEDURE [dbo].[uspOverviewUpdateCancelledFlag]
-
@ApplicationId int,
-
@Cancelled bit,
-
@LastUpdatedBy varchar(151)
-
-
AS
-
-
-- SET NOCOUNT ON added to prevent extra result sets from
-
-- interfering with SELECT statements.
-
SET NOCOUNT ON;
-
-
BEGIN
-
UPDATE dbo.tblOverview SET
-
cancelled = @Cancelled,
-
last_updated_by = @LastUpdatedBy,
-
last_updated_date = GETDATE(),
-
revision = revision + 1
-
WHERE
-
application_id = @ApplicationId
-
AND ((overview_type_id = 1) OR (overview_type_id = 2 AND requirement_id != 13))
-
-
END
-
-
RETURN
-
-
Trigger - -
declare @Changes varchar(max)
-
set @Changes = ''
-
-
if ((select application_id from deleted) != (select application_id from inserted) or
-
(select application_id from deleted) IS NULL and (select application_id from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'application_id from [' + ISNULL(convert(varchar(100),(select
-
application_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select application_id from inserted)),'Null') + '] '
-
-
if ((select overview_type_id from deleted) != (select overview_type_id from inserted) or
-
(select overview_type_id from deleted) IS NULL and (select overview_type_id from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'overview_type_id from [' + ISNULL(convert(varchar(100),(select
-
overview_type_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select overview_type_id from inserted)),'Null') + '] '
-
-
if ((select requirement_id from deleted) != (select requirement_id from inserted) or
-
(select requirement_id from deleted) IS NULL and (select requirement_id from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'requirement_id from [' + ISNULL(convert(varchar(100),(select
-
requirement_id from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select requirement_id from inserted)),'Null') + '] '
-
-
if ((select deadline_date from deleted) != (select deadline_date from inserted) or
-
(select deadline_date from deleted) IS NULL and (select deadline_date from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'deadline_date from [' + ISNULL(convert(varchar(100),(select
-
deadline_date from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select deadline_date from inserted)),'Null') + '] '
-
-
if ((select description from deleted) != (select description from inserted) or
-
(select description from deleted) IS NULL and (select description from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'description from [' + ISNULL(convert(varchar(100),(select
-
description from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select description from inserted)),'Null') + '] '
-
-
if ((select completed from deleted) != (select completed from inserted) or
-
(select completed from deleted) IS NULL and (select completed from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'completed from [' + ISNULL(convert(varchar(100),(select
-
completed from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select completed from inserted)),'Null') + '] '
-
-
if ((select cancelled from deleted) != (select cancelled from inserted) or
-
(select cancelled from deleted) IS NULL and (select cancelled from inserted) IS NOT NULL)
-
set @Changes = @Changes + 'cancelled from [' + ISNULL(convert(varchar(100),(select
-
cancelled from deleted)),'Null') + '] to [' + ISNULL(convert(varchar(100),(select cancelled from inserted)),'Null') + '] '
-
-
if (@Changes != '')
-
BEGIN
-
INSERT INTO dbo.tblAudit
-
(
-
table_name,
-
row_id,
-
row_revision,
-
operation,
-
user_name,
-
date_time,
-
changes,
-
application_reference
-
)
-
select 'tblOverview',
-
inserted.id,
-
inserted.revision,
-
'Update',
-
inserted.last_updated_by,
-
inserted.last_updated_date,
-
@Changes,
-
application_reference
-
from inserted INNER JOIN
-
tblapplication on tblapplication.id = application_id
-
-
END
-
-
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.
Is this code working before and you just experience the error recently?
What do you mean by: -
(select application_id from deleted) != (select application_id from inserted)
-
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
what i dont see before the subquery or after is that values clause.
insert into table_name(columns) values (columns data)
Is this code working before and you just experience the error recently?
What do you mean by: -
(select application_id from deleted) != (select application_id from inserted)
-
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
I rewrote the procedure that updates the table so that it updates each record separately, then the trigger is actioned each time.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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;...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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...
|
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: 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...
| |