Trigger on view

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

LVL 1
JoeBo747Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
what errors do you get?
is the view following the prerequirements for having a trigger?
0
JoeBo747Author Commented:
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
JoeBo747Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
JoeBo747Author Commented:
Hi angelIII

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

Regards
JoeBo747
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoeBo747Author Commented:
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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
JoeBo747Author Commented:
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
JoeBo747Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.