|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 5:56 AM
Points: 40,
Visits: 277
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Yesterday @ 6:14 PM
Points: 31,421,
Visits: 13,734
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 5:56 AM
Points: 40,
Visits: 277
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:54 AM
Points: 37,692,
Visits: 29,951
|
|
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 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 5:56 AM
Points: 40,
Visits: 277
|
|
| The view is based on several tables
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 8:43 AM
Points: 140,
Visits: 311
|
|
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
|
|
|
|