Solved

Trigger - RE-write

Posted on 2013-01-02
15
269 Views
Last Modified: 2013-01-04
Create TRIGGER [dbo].[Add_ODD_ROAD_Seg] ON  [dbo].[Segment]
for update 
AS 
if UPDATE(L_F_ADD)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 
END

if UPDATE(L_T_ADD)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 
END

if UPDATE(R_F_ADD)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 

END
if UPDATE(R_T_ADD)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 
END

if UPDATE(Left_ODD)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 
END

if UPDATE(ROADJUR)
BEGIN

INSERT INTO dbo.Segment_History
select * from inserted 
	
END

Open in new window


I am re building this trigger from a old enviroment into our new work one.
 I'm getting the following error.
Msg 311, Level 16, State 1, Procedure Add_ODD_ROAD_Seg, Line 7
Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables.

It looks to me there is a better way to write the old query.
Can someone help me?
Thanks
0
Comment
Question by:PtboGiser
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 5

Expert Comment

by:mlaise
ID: 38737668
The error is related to the data fields your tables are using.  If you use inserted you can not call forward text, ntext, or image columns in the trigger (which I assume you are doing with the SELECT all command you are using.

You can set the trigger to INSTEAD which allows access to those fields.

Here is a quick walkthrough of how to re-work the trigger: http://www.thetugboatcomplex.com/?p=13
0
 

Author Comment

by:PtboGiser
ID: 38737736
Data Type problem, i see why.

Is there a better way to write the trigger? (disregard the Error i have fixed that issue) Its seems clunky to me?
0
 
LVL 5

Expert Comment

by:mlaise
ID: 38737799
It really depends on what you expect it to do.  Without knowing what you expect it to do it makes it a bit hard for me to modify it.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:PtboGiser
ID: 38740221
The Trigger is designed to insert the changed row into the Segment_History table upon an insert, delete or update of the row.
So if any of the values in the above stated columns change insert the record into the Segment_history table.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 166 total points
ID: 38742551
So if all 6 columns are involved in an UPDATE statement you would like to see the same row inserted 6 times in the Segment_History table?
0
 

Author Comment

by:PtboGiser
ID: 38744505
If one or a couple of the 6 columns are updated just insert the entire record once into the History table.
No need for having the record inserted mutliple times for each change.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 334 total points
ID: 38744558
CREATE TRIGGER [dbo].[Add_ODD_ROAD_Seg]
ON  [dbo].[Segment]
FOR UPDATE
AS
INSERT INTO dbo.Segment_History
SELECT * FROM inserted



IF an UPDATE occurred, at least one column changed.  Since you're capturing the entire row when a column changes, I don't see why you need any checks at all, unless you're limiting it to just certain columns changing.

If so, you can use an IF UPDATE() with ORs for that:

IF UPDATE(col1) OR UPDATE(col2) OR ...
    INSERT INTO dbo.Segment_History
    SELECT * FROM inserted
0
 

Author Comment

by:PtboGiser
ID: 38744873
so like this then

USE [CNTY_GIS]
GO
/****** Object:  Trigger [dbo].[Segment_Update]    Script Date: 01/04/2013 13:57:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[Segment_Update] 
ON  [dbo].[Segment]
FOR UPDATE 
AS 
IF UPDATE(L_F_ADD) OR UPDATE(L_T_ADD) OR UPDATE(R_T_ADD)OR UPDATE(R_F_ADD) OR UPDATE(L_T_ADD) or UPDATE(ROADJUR)
    SELECT * FROM inserted

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 38744882
If a change is made on any one of those columns the trigger will fire and send it to the Segment_History table.

I will need a seperate Trigger to deal with deleted\retired segments. I;ll work on that now.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 334 total points
ID: 38744894
Yep.  But you still need the INSERT part of it of course :-) :


...
IF UPDATE(L_F_ADD) OR UPDATE(L_T_ADD) OR UPDATE(R_T_ADD)OR UPDATE(R_F_ADD) OR UPDATE(L_T_ADD) or UPDATE(ROADJUR)
    INSERT INTO dbo.Segment_History
    SELECT * FROM inserted
0
 

Author Comment

by:PtboGiser
ID: 38744936
Thanks for your help as always guys. Some of this is starting to make sense!
0
 

Author Comment

by:PtboGiser
ID: 38744961
Yes added the insert. a little early posting back
Thanks again Scott
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38745397
If a change is made on any one of those columns the trigger will fire
Actually no, that is a myth.  The UPDATE(ColumnName) does not indicate that the value changed.   In order to find out if the value really changed you have to inspect the values of the INSERTED and DELETED logical tables.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38745420
>>
If a change is made on any one of those columns the trigger will fire
Actually no, that is a myth.  The UPDATE(ColumnName) does not indicate that the value changed.  
<<

I state something, you state something entirely different, then state that what I said was a "myth".

I NEVER stated the UPDATE() reflected a true value change on that specific column.  I said the trigger would be fired if any of the columns actually changed in value.

Your claim about what I stated is the only "myth" here.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38745998
I NEVER stated the UPDATE() reflected a true value change on that specific column.
Calm down, Scott, I never said you had.  If you look carefully my comment was addressing the author's quote, not yours.

Of course I know that you know full well that UPDATE() has nothing to do with the actual value getting changed.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question