December 13, 2007 at 7:18 am
I have a view called view_badge and a table called t_badge.
Both the table and the view have the same structure
create table t_badge
YEAR (varchar(12),not null),
STU_CODE (varchar(12) primary key,not null),
JOIN_CODE (varchar(12),not null),
FORENAME1 (varchar(30),null),
FORENAME2 (varchar(30),null),
SURNAME (varchar(40),null),
DOB (datetime,null)
COURSE (varchar(18),not null),
END_DATE (datetime,null))
how would a create an update trigger on view_badge to update t_badge on updates to view_badge.
December 13, 2007 at 7:53 am
I'm not sure what you mean? If you update the view, it updates the table. The view is just a window into the table, there is nothing in the view to update.
If you need a trigger, then create it on the table and when someone updates the view, which updates the table, the trigger will fire.
December 13, 2007 at 8:41 am
The view is not created from the table,
The table is created from the view.
So i created the table then inserted the data from the view.
I have a script which fires every minutes if there any any new rows.
December 13, 2007 at 10:34 am
icampbell (12/13/2007)
The view is not created from the table,The table is created from the view.
So what's view based on? Another table? Several tables?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 19, 2007 at 8:04 am
The view is based on several tables
January 16, 2008 at 6:38 am
Keep in mind that this only works when the view is updated and not the underlying table changes that make up the view. If you need to track the changes made to the underlying tables that make up the view you will have to create multiple triggers, one on each table, and/or just create a procedure to handle everything.
IF OBJECT_ID('tr_view_badge') IS NOT NULL
DROP TRIGGER tr_view_badge
go
create trigger tr_view_badge
on view_badge
instead of insert, update, delete
as
BEGIN
DECLARE @action CHAR(1)
IF(EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted))
SET @action = 'U'
ELSE IF(EXISTS(SELECT * FROM inserted))
SET @action = 'I'
ELSE IF(EXISTS(SELECT * FROM deleted))
SET @action = 'D'
IF @action IS NULL
return
--Do what you need to the table based on the action
END
GO
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply