473,480 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trigger to Update Fields of Inserted Record

I am trying to update a field of a newly inserted record by using an
insert trigger. For example:

Table has 3 fields:
MyTable:
ID (int, Identity), Integer1(int), Integer2(int)
INSERT INTO MyTable (Integer1) VALUES (20)
And then on the trigger have the Integer2 column be set to Integer1 +
10. Everything I have seen mentions that an Insert trigger can not
modify the data that has just been inserted. Can anyone help me with
this? I would be glad to clarify if needed.
Thanks,
Pete
Sep 19 '08 #1
5 29022
Yes, you can do that. Here is example:

CREATE TABLE Foo (
keycol INT PRIMARY KEY,
col1 INT,
col2 INT);

GO

CREATE TRIGGER Foo$Insert
ON Foo
AFTER INSERT
AS
UPDATE Foo
SET col2 = I.col1 + 10
FROM Foo AS F
JOIN Inserted AS I
ON F.keycol = I.keycol;

GO

INSERT INTO Foo (keycol, col1) VALUES (1, 20);

SELECT keycol, col1, col2
FROM Foo;

--
Plamen Ratchev
http://www.SQLStudio.com
Sep 19 '08 #2
On Fri, 19 Sep 2008 12:57:46 -0700 (PDT), ME***********@gmail.com wrote:
>I am trying to update a field of a newly inserted record by using an
insert trigger. For example:

Table has 3 fields:
MyTable:
ID (int, Identity), Integer1(int), Integer2(int)
INSERT INTO MyTable (Integer1) VALUES (20)
And then on the trigger have the Integer2 column be set to Integer1 +
10. Everything I have seen mentions that an Insert trigger can not
modify the data that has just been inserted. Can anyone help me with
this? I would be glad to clarify if needed.
Hi Pete,

Plamen has already answered your question, but you should consider using
a derived column instead:

CREATE TABLE MyTable
(ID int NOT NULL IDENTITY PRIMARY KEY,
Integer1 int NOT NULL,
Integer2 AS Integer1 + 10);
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Sep 21 '08 #3
>I am trying to update a field [sic] of a newly inserted record [sic] byusing an
insert trigger. *<<

Your whole mindset is wrong. Rows are not records, columns are not
fields, A table must have a key and IDENTITY cannot be a key by
definition. What you are designing is a file system in SQL.

The language is more abstract than the punch card approach you have.
Data elements do not have to be materialized like they did with punch
cards. This second column should be computed in a VIEW (or with a
computed column, if you want to use proprietary features).

Next, we hare triggers almost as much as cursors and other procedural
code, but you need to know about them. We had to use them in the old
days before CHECK() and DRI actions were in the language. You might
write as many as 4-5 of them in your entire career today, if you work
with really strange databases/data.

Unfortunately, the T-SQL version is very weak. In Standard SQL, they
can be BEFORE or AFTER triggers, while T-SQL only has the INSTEAD OF
trigger for before operations. That is why you were having problems
writing a kludge -- you want to do the wrong thing with a bad tool.
Sep 22 '08 #4
Unfortunately, the T-SQL version is very weak. *In Standard SQL, they
can be BEFORE or AFTER triggers, while T-SQL only has the INSTEAD OF
trigger for before operations. *
Wrong. T-SQL has both INSTEAD OF and AFTER triggers. Someone who
claims to be an expert should know this, and in any event, Plamen
gives you an example of one in his response above.
That is why you were having problems
writing a kludge *-- you want to do the wrong thing with a bad tool.
Maybe you ought to update your knowledge of the tool before giving
advice about it?

Sep 22 '08 #5
>Wrong. *T-SQL has both INSTEAD OF and AFTER triggers. *<<

To quote myself:
"In Standard SQL, they can be BEFORE or AFTER triggers, while T-SQL
only has the INSTEAD OF trigger for before operations."

Is there an actual BEFORE trigger now? You might want to read what I
actually posted :)

Other than the standard objections to trigger-vs-declarations, I
dislike having only the INSTEAD OF option because we meant it to be
for VIEWs to avoid view update problems. This means that they will be
a little confusing to maintenance programmers and that any special
optimizations for view update will have to be checked for.
Sep 24 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2190
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...
1
3301
by: Alistair Hopkins | last post by:
Hi, I am trying to write a generic audit-trail trigger function which will record changes on a field-by-field basis to a single table for all audited tables. However, I find that I can only...
0
3560
by: Bill Smith | last post by:
I am looking for an example 'after insert' trigger that performs an update to a column in the master table. For example, I have an 'orders' table that contains a 'PartNum' column. I would like to...
1
1582
by: Craig | last post by:
In ASP.NET 2.0 and the formview control how do you get the value of the identity field of a newly inserted record? In ASP.NET 1.1 after the new record was saved you just said intNewIdentityID =...
8
1902
by: Doru Roman | last post by:
Hi, I insert a new record with a command: private SqlCommand comInsert; comInsert = conDataBase.CreateCommand(); comInsert.CommandType = CommandType.Text;
1
2481
by: Ripendra007 | last post by:
i have a table employee and i want to find the newly inserted record from employee table without using MAX And TOP ...it possible ? yes then How?
13
4401
rajiv07
by: rajiv07 | last post by:
Hi to all, I want to know how to select a last inserted record which is the primary key is not an integer. my table ramstr(Primary)----name--service XTC01-------Rajiv---service ...
0
1583
by: fig000 | last post by:
Hi, I'm using an objectdatasource. The insert procedure called by the objectdatasource is in a separate library file outside of the aspx and the codebehind that is using the objectdatasource in...
4
2488
semanticnotion
by: semanticnotion | last post by:
Hi guys how can i retrieve the last inserted record from datebase. my primary key is not auto_increment its type is bigint because i want to insert SSN no as a primary key. when i retrive the data...
0
6915
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...
0
7054
Oralloy
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,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
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,...
1
4794
isladogs
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...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.