SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create update trigger


create update trigger

Author
Message
icampbell
icampbell
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: Administrators
Points: 65401 Visits: 19118
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
My Blog: www.voiceofthedba.com
icampbell
icampbell
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90755 Visits: 45284
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


icampbell
icampbell
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 277
The view is based on several tables
Keith DuAime
Keith DuAime
SSC-Enthusiastic
SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)

Group: General Forum Members
Points: 172 Visits: 344
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search