Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

create update trigger Expand / Collapse
Author
Message
Posted Thursday, December 13, 2007 7:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 8, 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.
Post #432832
Posted Thursday, December 13, 2007 7:53 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 2:27 PM
Points: 33,267, Visits: 15,434
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #432851
Posted Thursday, December 13, 2007 8:41 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 8, 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.

Post #432893
Posted Thursday, December 13, 2007 10:34 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:25 PM
Points: 43,008, Visits: 36,164
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

Post #432975
Posted Wednesday, December 19, 2007 8:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 8, 2012 5:56 AM
Points: 40, Visits: 277
The view is based on several tables
Post #434785
Posted Wednesday, January 16, 2008 6:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, October 22, 2013 11:14 AM
Points: 140, Visits: 312
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

Post #443585
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse