Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Instead of trigger on view

Author  Topic 

thendraljazz
Starting Member

26 Posts

Posted - 2011-09-21 : 07:48:08
Hi

i tried to create instead of trigger on view.but it will not come.
i don't know how to use instead of trigger in sql server.

so can anyone give example to create this.

thanks

Thendral

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-21 : 08:47:55
can you post your trigger so that we can suggest what to be changed

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

karthikeyan.marlen
Starting Member

16 Posts

Posted - 2011-09-21 : 08:51:54
Hi instead of trigger is (substituted to ) purpose,we use this for view,

as view is not updatable we can update table (where the view derives from)

Eg

create trigger trigs
insted of insert
as
begin
insert into table1(sno ,name) select (sno,name ) from inserted
end


karthikeyan
Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-09-25 : 02:24:01
Just to correct what karthikeyan has mentioned: Views can be updated directly as long as it is being created based on one single table. Insteadof triggers needs to be used for updating a view if it is been created based on multiple tables.

A complete sample of how to use INSTEADOF Trigger is up there in MSDN. Check this out http://msdn.microsoft.com/en-us/library/ms175521.aspx

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-25 : 02:39:52
quote:
Originally posted by vmvadivel

Just to correct what karthikeyan has mentioned: Views can be updated directly as long as it is being created based on one single table. Insteadof triggers needs to be used for updating a view if it is been created based on multiple tables.

A complete sample of how to use INSTEADOF Trigger is up there in MSDN. Check this out http://msdn.microsoft.com/en-us/library/ms175521.aspx

Best Regards
Vadivel

http://vadivel.blogspot.com


Not exactly. you can update a view directly even if its based on more than one table but UPDATE should only reference the columns from the single base table at any one time.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-09-25 : 02:49:47
Yep thats what I actually intend to mean :) May be lemme edit the post.

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-25 : 02:51:59
see an example here



--base tables
create table atest
( ID int IDENTITY(1,1),
a varchar(50)
)
create table btest
( ID int IDENTITY(1,1),
b varchar(50)
)
INSERT atest(a)
SELECT 'test' union all
SELECT 'tip' union all
SELECT 'tin' union all
SELECT 'mind' union all
SELECT 'pot' union all
SELECT 'top'

INSERT btest(b)
SELECT 'wqdew' union all
SELECT 'xvfvv' union all
SELECT 'dbgwtr' union all
SELECT 'fe' union all
SELECT 'poewgert' union all
SELECT 'ewgqg'

select * from atest
select * from btest

--creation of view
create view ab
as
select a.id,a.a,b.b
from atest a
join btest b
on b.id = a.id


select * from ab

--updating only atest's column
update ab
set a=a+'123'

select * from ab

--updating only btest's column
update ab
set b=b+'345'
select * from ab

--updating multiple base tables column this will fail with error
update ab
set a=a+'678',
b=b+'910'


output
----------------------------
Original view

id a b
-------------------------------
1 test wqdew
2 tip xvfvv
3 tin dbgwtr
4 mind fe
5 pot poewgert
6 top ewgqg

after atest column update

id a b
------------------------------
1 test123 wqdew
2 tip123 xvfvv
3 tin123 dbgwtr
4 mind123 fe
5 pot123 poewgert
6 top123 ewgqg

after btest column update
--------------------------------
id a b
--------------------------------
1 test123 wqdew345
2 tip123 xvfvv345
3 tin123 dbgwtr345
4 mind123 fe345
5 pot123 poewgert345
6 top123 ewgqg345


after both tables column update



Msg 4405, Level 16, State 1, Line 1
View or function 'ab' is not updatable because the modification affects multiple base tables.



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -