日期:2014-05-19  浏览次数:20452 次

为什么无法直接对视图进行更新操作
我建了个视图(命名为vCusInfo),SQL语句如下:
SELECT   Blog.dbo.oblog_user.username,   Blog.dbo.oblog_user.user_dir,  
            Blog.dbo.oblog_user.useremail,   Blog.dbo.oblog_user.password,  
            Blog.dbo.oblog_user.truename,   Blog.dbo.oblog_user.sex,  
            Blog.dbo.oblog_user.adddate,   Blog.dbo.oblog_user.province,  
            Blog.dbo.oblog_user.address,   Blog.dbo.oblog_user.city,  
            Blog.dbo.oblog_user.tel,   dbo.CusInfo.*
FROM   Blog.dbo.oblog_user   INNER   JOIN
            dbo.CusInfo   ON   Blog.dbo.oblog_user.userid   =   dbo.CusInfo.Bloguserid
oblog_user是另一个数据库的表
这里把本数据库的表CusInfo直接与另一个数据库的表oblog_user进行关联查询。
为什么我在直接对视图vCusInfo进行更新时,提示视图或函数vCusInfo不可更新因为修改会影响多个基表

------解决方案--------------------
Updatable Views
You can modify the data of an underlying base table through a view, as long as the following conditions are true:

Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.


The columns being modified in the view must directly reference the underlying data in the table columns. The columns cannot be derived in any other way, such as through the following:


An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR, and VARP.


A computation. The column cannot be computed from an expression that uses other columns. Columns that are formed by using the set operators UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also not updatable.


The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.
查找的MSDN资料
更新视图,你第一条就不行啊,数据来源于2个基本表,肯定不能直接更新视图
用触发器,可以在插入其中一个表时。触发,插入另一个表数据

------解决方案--------------------
应该分情况吧,不是什么视图都能更新。
就像是一组数据求和的视图,sum出来的结果,你怎么更新到对应表去?
------解决方案--------------------
(1)视图有可更新视图,和不可更新视图
(2)当用多个表建立视图时,如果对视图的更新将联系到多个表时,视图是不能更新的