Advertisement

01.07.2008 at 09:59PM PST, ID: 23065740
[x]
Attachment Details

SQL Insert Trigger - Modify the new record during trigger

Asked by ctmfab in SQL Server 2005, Microsoft Visual Basic.Net, Visual Studio

Tags: ,

I'm using SQL Server 2005 with VB.Net.  My question is related to Insert triggers in SQL 2005.  

Is it possible to update columns of a new record during a Insert Trigger?  When the new record is inserted, I would like to set the values of several columns in those new records during the Insert trigger for the table.  A trivial example of such an attempt is given below (note this isn't my actual trigger, just a simple example to discuss).   I think the problem may be related to referencing the new records identity column (RowID in the example) during the Insert trigger.  I'm not 100% certain that the identity value is actually assigned before the Insert trigger fires, so this might be my problem.

When I try similar code for my database I get the following error.  Note that I'm directly modifying the data table in the Server Explorer from the VB.NET 2005 IDE.  When the new row is committed, the following error occurs.  The error is cleared by running the query again (right click data table and select Execute SQL from the popup menu).

"This row was successfully committed to the database.  However, a problem occurred when attempting to retrieve the data back after the commit.  Because of this, the displayed data in this row is read-only.  To fix this problem, please re-run the query."

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
ALTER TRIGGER Example 
ON dbo.Table1 
FOR INSERT
AS 
UPDATE Table1
SET Column2 =  'Modified'
FROM inserted  
INNER JOIN Table1 ON Table1.RowID = inserted.RowID
 
Loading Advertisement...
 
[+][-]01.07.2008 at 10:16PM PST, ID: 20606338

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, Microsoft Visual Basic.Net, Visual Studio
Tags: SQL, 2005
Sign Up Now!
Solution Provided By: appari
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.07.2008 at 10:40PM PST, ID: 20606440

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]01.07.2008 at 11:13PM PST, ID: 20606569

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.08.2008 at 12:27AM PST, ID: 20606818

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628