Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update field in INSERTED inside trigger Expand / Collapse
Author
Message
Posted Tuesday, September 21, 2004 9:02 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 2, 2007 4:28 AM
Points: 36, Visits: 1

Hello,

I have a table that constantly has rows inserted, and one of the fields is always empty. I want to create an Insert trigger on the table and populate the empty field with data extracted from several of the other fields.

e.g.

CREATE TRIGGER tr_PutInValue ON MyTable
FOR INSERT
AS

update INSERTED
set EmptyField = DataField1 + ':' + DataField2 + ':' + GetDate()

But when I try to create this trigger, SQL tells me

Server: Msg 286, Level 16, State 1, Procedure trI_syslog, Line 119
The logical tables INSERTED and DELETED cannot be updated.

If I can't modify values in the Inserted table, then how can I achieve what I want?

-Steve




Post #138035
Posted Tuesday, September 21, 2004 2:44 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, December 29, 2006 4:30 PM
Points: 354, Visits: 1

inserted/updated tables are just views of the log.  when you want to update the value in the record, update against the real table name joining to "inserted" to identify which rows were modified.

But it would be better to do what you want done in the insert statement.

Post #138100
Posted Wednesday, September 22, 2004 4:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 2, 2007 4:28 AM
Points: 36, Visits: 1

Thanks for the advice, John.  The inserts are being done by a 3rd-party application that I have no control over, so hence using the trigger.

Unfortunately the table has no indexes, so to join INSERTED with the main table in an insert trigger I believe would cause a huge performance hit (the main table will have hundred of thousands of rows, and has no distinguishable unique fields to use for a primary key)

Most likely candidate for an index is the DateTime field called DateTimeLocal. It is not unique, but the most rows with identical values I've seen so far is less than 100, so a non-unique index on this field could work.

Wish me luck!

 




Post #138181
Posted Wednesday, September 22, 2004 7:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 6, 2007 7:13 AM
Points: 6, Visits: 1

Why don't you try to add an identity column?

For best performance make it a clustered primary key.

Post #138225
Posted Monday, July 29, 2013 11:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 29, 2013 11:40 AM
Points: 1, Visits: 2
I have an example


create TRIGGER InsertarPeriodoForUpdate
ON dbo.calificacion_IQA
for update
AS
BEGIN
SET NOCOUNT ON;
declare @id int
declare @mes int

select @mes=mes_calificadcion, @id = id_calificacion_iqa from updated

if @mes=1 or @mes = 2 or @mes = 3
begin

Update dbo.calificacion_IQA
set cuarto = 1
where id_calificacion_iqa=@id
end
if @mes=4 or @mes = 5 or @mes = 6
begin
Update dbo.calificacion_IQA
set cuarto = 2
where id_calificacion_iqa=@id
end
if @mes=7 or @mes = 8 or @mes = 9
begin
Update dbo.calificacion_IQA
set cuarto = 3
where id_calificacion_iqa=@id
end
if @mes=10 or @mes = 11 or @mes = 12
begin
Update dbo.calificacion_IQA
set cuarto = 4
where id_calificacion_iqa=@id
end

END
GO
Post #1478686
Posted Monday, July 29, 2013 1:26 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 13,228, Visits: 12,705
sipadu (7/29/2013)
I have an example


create TRIGGER InsertarPeriodoForUpdate
ON dbo.calificacion_IQA
for update
AS
BEGIN
SET NOCOUNT ON;
declare @id int
declare @mes int

select @mes=mes_calificadcion, @id = id_calificacion_iqa from updated

if @mes=1 or @mes = 2 or @mes = 3
begin

Update dbo.calificacion_IQA
set cuarto = 1
where id_calificacion_iqa=@id
end
if @mes=4 or @mes = 5 or @mes = 6
begin
Update dbo.calificacion_IQA
set cuarto = 2
where id_calificacion_iqa=@id
end
if @mes=7 or @mes = 8 or @mes = 9
begin
Update dbo.calificacion_IQA
set cuarto = 3
where id_calificacion_iqa=@id
end
if @mes=10 or @mes = 11 or @mes = 12
begin
Update dbo.calificacion_IQA
set cuarto = 4
where id_calificacion_iqa=@id
end

END
GO


Not only is this thread 9 years old your code is broken on a number of levels. There is no "updated" in triggers in sql server. And even worse is that your code will not support multiple rows. The logic posted is entirely to flawed to even have a chance at being correct.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1478725
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse