September 21, 2004 at 9:02 am
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
September 21, 2004 at 2:44 pm
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.
September 22, 2004 at 4:31 am
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!
September 22, 2004 at 7:44 am
Why don't you try to add an identity column?
For best performance make it a clustered primary key.
July 29, 2013 at 11:41 am
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
July 29, 2013 at 1:26 pm
sipadu (7/29/2013)
I have an examplecreate 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply