question

Newbie Bala avatar image
Newbie Bala asked

Update a View?

Can we update a View containing more tables?
sql-server-2005view
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
It depends on how your view is created. But it is possible to create a view that joins table, and that is updateable: --Create the tables create table Table1 (id int identity(1,1) primary key, f1 varchar(100)) create table Table2 (id int identity(1,1) primary key, Table1ID int, f2 varchar(100) CONSTRAINT FK_Table2_Table1 FOREIGN KEY (Table1ID) REFERENCES Table1(id)) --Insert some values insert into table2 select 1,'first row in T2' insert into table2 select 1,'second row in T2' insert into table2 select 2,'third row in T2' insert into table2 select 2,'fourth row in T2' --Create a view that joins the tables CREATE view v1 as select T1.id as T1ID, T2.ID as T2ID, f1,f2 FROM Table1 t1 inner join table2 t2 on t1.id = t2.Table1ID --And update values in both tables, using update-statement against the view. update v1 set f1='First row in Table1' where t1id=1 update v1 set f2='This row is related to Table1.ID = 1' where t1id=1
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Cyborg avatar image
Cyborg answered
You are updating the base table thorough the view, You can update the base tables through views, but there are so many restrictions. please refer [MSDN : Updatable Views][1], [and here][2] for more details. [1]: http://msdn.microsoft.com/en-us/library/ms187956.aspx [2]: http://msdn.microsoft.com/en-us/library/ms180800.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.