| Author |
Topic |
|
yns.emre
Starting Member
11 Posts |
Posted - 2009-01-08 : 06:26:04
|
| i have a view,can i update he view like normal sql update or not? |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-08 : 06:48:14
|
| Yes, we can update the view also |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-01-08 : 07:55:29
|
| but the view must be built on only one table |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-08 : 08:04:16
|
quote: Originally posted by darkdusky but the view must be built on only one table
So not true at all.If you have a primary key in all related tables in the view, you can update through the view.See this exampleCREATE TABLE Table1 ( col1 INT PRIMARY KEY )CREATE TABLE Table2 ( pk INT IDENTITY(1, 1) PRIMARY KEY, col2 INT FOREIGN KEY REFERENCES Table1(col1), col3 INT )INSERT Table1SELECT 1 UNION ALLSELECT 2INSERT Table2SELECT 1, 1 UNION ALLSELECT 2, 2GOCREATE VIEW vwTableASSELECT t1.col1, t2.pk, t2.col2, t2.col3FROM Table1 AS t1INNER JOIN Table2 AS t2 ON t2.col2 = t1.col1GOSELECT *FROM vwTableUPDATE vwTableSET col3 = 5WHERE col1 = 2SELECT *FROM vwTableDROP VIEW vwTableDROP TABLE Table2, Table1 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-08 : 08:24:49
|
| even otherwise you can make a view updatable using an INSTEAD OF TRIGGER which actually does update on base tables. |
 |
|
|
revelator
Starting Member
32 Posts |
Posted - 2009-01-08 : 09:12:34
|
But only one base table can be modified, not both. To continue your example, try to execute this statement instead of the UPDATE posted previously.UPDATE vwTableSET col3 = 5, col1 = 5WHERE col1 = 2quote: Originally posted by PesoSo not true at all.If you have a primary key in all related tables in the view, you can update through the view.See this exampleCREATE TABLE Table1 ( col1 INT PRIMARY KEY )CREATE TABLE Table2 ( pk INT IDENTITY(1, 1) PRIMARY KEY, col2 INT FOREIGN KEY REFERENCES Table1(col1), col3 INT )INSERT Table1SELECT 1 UNION ALLSELECT 2INSERT Table2SELECT 1, 1 UNION ALLSELECT 2, 2GOCREATE VIEW vwTableASSELECT t1.col1, t2.pk, t2.col2, t2.col3FROM Table1 AS t1INNER JOIN Table2 AS t2 ON t2.col2 = t1.col1GOSELECT *FROM vwTableUPDATE vwTableSET col3 = 5WHERE col1 = 2SELECT *FROM vwTableDROP VIEW vwTableDROP TABLE Table2, Table1 E 12°55'05.63"N 56°04'39.26"
|
 |
|
|
yns.emre
Starting Member
11 Posts |
Posted - 2009-01-08 : 09:16:06
|
| thanks everyone i will try |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
revelator
Starting Member
32 Posts |
Posted - 2009-01-08 : 09:26:53
|
Nice article Peso, thanks! :)quote: Originally posted by Peso That is almost always the case, that affecting only one base table is allowed when updating a view.There are cases that affects two (or more) tables when updating, like partitioning.See http://weblogs.sqlteam.com/peterl/archive/2007/09/24/Horizontal-partitioning.aspx E 12°55'05.63"N 56°04'39.26"
Waiting for the Great Leap Forwards |
 |
|
|
|