Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update field in INSERTED inside trigger


Update field in INSERTED inside trigger

Author
Message
xnl28
xnl28
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
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





john-132685
john-132685
Old Hand
Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)Old Hand (354 reputation)

Group: General Forum Members
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.


xnl28
xnl28
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
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!

 





Arif Agovic
Arif Agovic
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 1

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

For best performance make it a clustered primary key.


sipadu
sipadu
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16527 Visits: 16992
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search