?
Solved

Trigger on view

Posted on 2009-02-11
10
Medium Priority
?
198 Views
Last Modified: 2012-05-06
Hi
I have the following trigger which is on a view which is querying a table on another database. The trigger is not working. Can anyone see the problem.

Regards
JoeBo747

ALTER Trigger Trigger5
ON dbo.Vehicle_Assignments_Trigger 
 
INSTEAD OF  INSERT,  UPDATE 
AS
	
	declare @JobID integer
	declare @deviceid integer
	declare @flag tinyint
	declare @Job integer
	
	select @jobid = (select dwjobitemIDfk from inserted)
	select @deviceid = (select dwVehicleIDfk from inserted)
	select @flag = (select dwAssignFlags from inserted)
	
select @job  =(SELECT COUNT(*) AS [Exists]
FROM         PDA_Assignments
WHERE     (PDAid = @DeviceID) AND (dwJobIDFK = @JobID) AND (AssignFlags = @Flag))
	
	if @job = 0
begin
 
INSERT INTO Transport_Test.dbo.PDA_Assignments(dwjobidfk, PDAid, AssignDate, AssignFlags)
select dwjobitemIDfk, dwVehicleidfk, GETDATE(),  dwAssignFlags
from inserted 
 
end

Open in new window

0
Comment
Question by:JoeBo747
  • 6
  • 4
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 23612727
what errors do you get?
is the view following the prerequirements for having a trigger?
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 23613463
Hi angelIII,

The view is a straight query on the table, no alterations are made to this table, how should  I test this trigger  for errors.
Regards
JoeBo747
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 23613505
Hi angelIII,

If I highlight and run the code  I get:

Executing selected script from mssql:://myserver/Transport_TEST/dbo/ViewTrigger/dbo/Trigger5

No rows affected.
(0 row(s) returned)
Finished executing selected script from mssql:://mywserver/Transport_TEST/dbo/ViewTrigger/dbo/Trigger5
Regards
JoeBo747
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 23614059
is that when you run the ALTER TRIGGER code?
in that case, the output is normal.

you need to run a INSERT statement against the view to make the trigger actually "run".
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 23648502
Hi angelIII

I have tried inserting records but the trigger does not fire!

Regards
JoeBo747
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 23648541
let me suggest this trigger code rewrite
ALTER Trigger Trigger5
ON dbo.Vehicle_Assignments_Trigger 
 
INSTEAD OF  INSERT,  UPDATE 
AS
 
INSERT INTO Transport_Test.dbo.PDA_Assignments(dwjobidfk, PDAid, AssignDate, AssignFlags)
select dwjobitemIDfk, dwVehicleidfk, GETDATE(),  dwAssignFlags
from inserted i
where not exists ( 
  select null from PDA_Assignments a
   WHERE  a.PDAid = i.dwVehicleIDfk 
     AND  a.dwJobIDFK = i.dwjobitemIDfk 
     AND  a.AssignFlags = i.dwAssignFlags 
 )
 
 

Open in new window

0
 
LVL 1

Author Comment

by:JoeBo747
ID: 23671914
Hi angelIII
Thanks for sticking with me on this one; I have tried the posted trigger on the view without success, so to prove the triggers I have tried them both on the table. They both work fine but I had to change the
Instead of to after
to get the desired result. The view will not accept this syntax, it will only accept the instead of statement. Is there any work around for this as I do not really want the trigger on the table?
Regards
JoeBo747
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 23672180
>I have tried the posted trigger on the view without success,

what errors/problems do you actually get?

can you post the table and view DDL, and post the "insert" statements for the table to fill sample data, and the "insert" statements you run against the view to demonstrate the problem?

otherwise, we might be overlooking something "obvious" +...
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 23672463
Hi angelIII:

Am I correct in my assumption that any updates made to the table will be mirrored in the view so that an insert statement made against the table will fire the trigger on the view?

Regards
JoeBo747
0
 
LVL 1

Author Closing Comment

by:JoeBo747
ID: 31545615
I now appreciate the difference of a trigger on a view and that of one on a table; I appear to have been barking up the wrong tree your advice and code is always welcome.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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