Home All Groups Group Topic Archive Search About

Trigger, Identity and error 8102 (URGENT)?

Author
15 Sep 2005 4:48 PM
Henry
Hi

I have a trigger where I want to convert at copy some data from one table
(alarm) to another tabel Alarmqueue.

Using this query
update alarm set alarmcount = alarmcount +1

Gives me this error
Server: Msg 8102, Level 16, State 1, Procedure trAlarmsUpdate, Line 10
Cannot update identity column 'AlarmEntryId'.

This all worked fine before IDENTITY(1, 1)
[AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
came along

Does anyone have a clue how to solve this?




The source table looks like this

CREATE TABLE [dbo].[Alarm] (
[AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
[SiteId] [int] NOT NULL ,
[DeviceId] [int] NOT NULL ,
[ObjectId] [int] NOT NULL ,
---SNIP---


Destination table looks like this
CREATE TABLE [dbo].[AlarmQueue] (
[AlarmEntryId] [int] NOT NULL ,
[SiteId] [int] NULL ,
[DeviceId] [int] NULL ,
[ObjectId] [int] NULL ,
--- SNIP ---


This is what the trigger looks like
CREATE TRIGGER  trAlarmsUpdate
ON Alarm
INSTEAD OF UPDATE
AS
BEGIN
  DECLARE @rows int
  DECLARE @err int
  DECLARE @ErrMsg varchar(200)

  UPDATE alarm
  SET
AlarmEntryId = i.AlarmEntryId,
SiteId = i.SiteId,
DeviceId = i.DeviceId,
ObjectId = i.ObjectId,
---SNIP---
AlarmCount = i.AlarmCount
  FROM inserted i
  WHERE Alarm.AlarmEntryID = i.AlarmEntryID and Alarm.ObjectID=i.ObjectID


  INSERT INTO AlarmQueue
(  AlarmEntryId,
SiteId,
DeviceId,
ObjectId,
---SNIP---
AlarmCount)

SELECT

AlarmEntryId,
SiteId,
DeviceId,
ObjectId,
---SNIP---
AlarmCount
FROM INSERTED


END




Cheers
Henry

Author
15 Sep 2005 4:56 PM
David Browne
Show quote
"Henry" <n**@il.com> wrote in message
news:OUKIdVhuFHA.1560@TK2MSFTNGP09.phx.gbl...
> Hi
>
> I have a trigger where I want to convert at copy some data from one table
> (alarm) to another tabel Alarmqueue.
>
> Using this query
> update alarm set alarmcount = alarmcount +1
>
> Gives me this error
> Server: Msg 8102, Level 16, State 1, Procedure trAlarmsUpdate, Line 10
> Cannot update identity column 'AlarmEntryId'.
>
> This all worked fine before IDENTITY(1, 1)
> [AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
> came along
>
> Does anyone have a clue how to solve this?
>
>
>

You should not be updating the key, especially since you're joining on it,
and so not really changing it.


  UPDATE alarm
  SET
AlarmEntryId = i.AlarmEntryId,
SiteId = i.SiteId,
DeviceId = i.DeviceId,
ObjectId = i.ObjectId,
---SNIP---
AlarmCount = i.AlarmCount
  FROM inserted i
  WHERE Alarm.AlarmEntryID = i.AlarmEntryID and Alarm.ObjectID=i.ObjectID

Should be


  UPDATE alarm
  SET
SiteId = i.SiteId,
DeviceId = i.DeviceId,
---SNIP---
AlarmCount = i.AlarmCount
  FROM inserted i
  WHERE Alarm.AlarmEntryID = i.AlarmEntryID and Alarm.ObjectID=i.ObjectID

David
Author
15 Sep 2005 4:59 PM
David Portas
Should be

UPDATE AlarmQueue
...

instead of

UPDATE alarm
...

--
David Portas
SQL Server MVP
--
Author
15 Sep 2005 5:04 PM
David Portas
Disregard that. David Browne has the right answer.

--
David Portas
SQL Server MVP
--
Author
15 Sep 2005 5:06 PM
Jerry Spivey
Henry,

To my knowledge you cannot update a column that has the IDENITY property.
Also, unless you're testing you'll probably want to remove the SELECT
statement from your trigger code.

HTH

Jerry
Show quote
"Henry" <n**@il.com> wrote in message
news:OUKIdVhuFHA.1560@TK2MSFTNGP09.phx.gbl...
> Hi
>
> I have a trigger where I want to convert at copy some data from one table
> (alarm) to another tabel Alarmqueue.
>
> Using this query
> update alarm set alarmcount = alarmcount +1
>
> Gives me this error
> Server: Msg 8102, Level 16, State 1, Procedure trAlarmsUpdate, Line 10
> Cannot update identity column 'AlarmEntryId'.
>
> This all worked fine before IDENTITY(1, 1)
> [AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
> came along
>
> Does anyone have a clue how to solve this?
>
>
>
>
> The source table looks like this
>
> CREATE TABLE [dbo].[Alarm] (
> [AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
> [SiteId] [int] NOT NULL ,
> [DeviceId] [int] NOT NULL ,
> [ObjectId] [int] NOT NULL ,
> ---SNIP---
>
>
> Destination table looks like this
> CREATE TABLE [dbo].[AlarmQueue] (
> [AlarmEntryId] [int] NOT NULL ,
> [SiteId] [int] NULL ,
> [DeviceId] [int] NULL ,
> [ObjectId] [int] NULL ,
> --- SNIP ---
>
>
> This is what the trigger looks like
> CREATE TRIGGER  trAlarmsUpdate
> ON Alarm
> INSTEAD OF UPDATE
> AS
> BEGIN
>  DECLARE @rows int
>  DECLARE @err int
>  DECLARE @ErrMsg varchar(200)
>
>  UPDATE alarm
>  SET
> AlarmEntryId = i.AlarmEntryId,
> SiteId = i.SiteId,
> DeviceId = i.DeviceId,
> ObjectId = i.ObjectId,
> ---SNIP---
> AlarmCount = i.AlarmCount
>  FROM inserted i
>  WHERE Alarm.AlarmEntryID = i.AlarmEntryID and Alarm.ObjectID=i.ObjectID
>
>
>  INSERT INTO AlarmQueue
> (  AlarmEntryId,
> SiteId,
> DeviceId,
> ObjectId,
> ---SNIP---
> AlarmCount)
>
> SELECT
>
> AlarmEntryId,
> SiteId,
> DeviceId,
> ObjectId,
> ---SNIP---
> AlarmCount
> FROM INSERTED
>
>
> END
>
>
>
>
> Cheers
> Henry
>
>
Author
15 Sep 2005 5:10 PM
Jerry Spivey
Closer look INSERT...SELECT - OK.
Show quote
"Jerry Spivey" <jspi***@vestas-awt.com> wrote in message
news:uvnOafhuFHA.3224@TK2MSFTNGP10.phx.gbl...
> Henry,
>
> To my knowledge you cannot update a column that has the IDENITY property.
> Also, unless you're testing you'll probably want to remove the SELECT
> statement from your trigger code.
>
> HTH
>
> Jerry
> "Henry" <n**@il.com> wrote in message
> news:OUKIdVhuFHA.1560@TK2MSFTNGP09.phx.gbl...
>> Hi
>>
>> I have a trigger where I want to convert at copy some data from one table
>> (alarm) to another tabel Alarmqueue.
>>
>> Using this query
>> update alarm set alarmcount = alarmcount +1
>>
>> Gives me this error
>> Server: Msg 8102, Level 16, State 1, Procedure trAlarmsUpdate, Line 10
>> Cannot update identity column 'AlarmEntryId'.
>>
>> This all worked fine before IDENTITY(1, 1)
>> [AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
>> came along
>>
>> Does anyone have a clue how to solve this?
>>
>>
>>
>>
>> The source table looks like this
>>
>> CREATE TABLE [dbo].[Alarm] (
>> [AlarmEntryId] [int] IDENTITY (1, 1) NOT NULL ,
>> [SiteId] [int] NOT NULL ,
>> [DeviceId] [int] NOT NULL ,
>> [ObjectId] [int] NOT NULL ,
>> ---SNIP---
>>
>>
>> Destination table looks like this
>> CREATE TABLE [dbo].[AlarmQueue] (
>> [AlarmEntryId] [int] NOT NULL ,
>> [SiteId] [int] NULL ,
>> [DeviceId] [int] NULL ,
>> [ObjectId] [int] NULL ,
>> --- SNIP ---
>>
>>
>> This is what the trigger looks like
>> CREATE TRIGGER  trAlarmsUpdate
>> ON Alarm
>> INSTEAD OF UPDATE
>> AS
>> BEGIN
>>  DECLARE @rows int
>>  DECLARE @err int
>>  DECLARE @ErrMsg varchar(200)
>>
>>  UPDATE alarm
>>  SET
>> AlarmEntryId = i.AlarmEntryId,
>> SiteId = i.SiteId,
>> DeviceId = i.DeviceId,
>> ObjectId = i.ObjectId,
>> ---SNIP---
>> AlarmCount = i.AlarmCount
>>  FROM inserted i
>>  WHERE Alarm.AlarmEntryID = i.AlarmEntryID and Alarm.ObjectID=i.ObjectID
>>
>>
>>  INSERT INTO AlarmQueue
>> (  AlarmEntryId,
>> SiteId,
>> DeviceId,
>> ObjectId,
>> ---SNIP---
>> AlarmCount)
>>
>> SELECT
>>
>> AlarmEntryId,
>> SiteId,
>> DeviceId,
>> ObjectId,
>> ---SNIP---
>> AlarmCount
>> FROM INSERTED
>>
>>
>> END
>>
>>
>>
>>
>> Cheers
>> Henry
>>
>>
>
>
Author
16 Sep 2005 11:19 AM
Henry
Thanks

I have figured out what the problems is (several).

regards
Henry

AddThis Social Bookmark Button