We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Trigger on view

JoeBo747
JoeBo747 asked
on
Medium Priority
209 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

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
what errors do you get?
is the view following the prerequirements for having a trigger?

Author

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

Author

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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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".

Author

Commented:
Hi angelIII

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

Regards
JoeBo747
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>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" +...

Author

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

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.