SQL 视图中的 DML 语句
我正在尝试在该视图中创建一个视图,我想根据特定条件插入一条记录或更新一条记录,以便我们可以在 SQL 视图中插入或更新。我们可以在视图中插入或更新语句吗?
I am trying to create a view in that view I want to insert a record or update a record based on perticular condition so can we insert or update in SQL view. Can we have insert or update statement in view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短回答:是的。但也有限制。
例如:(取自 http://msdn.microsoft .com/en-us/library/ms180800(v=sql.90).aspx )
任何修改(包括 UPDATE、INSERT 和 DELETE 语句)都必须引用来自只有一张基表。
在视图中修改的列必须直接引用表列中的基础数据。它们不能以任何其他方式导出,例如通过:
正在修改的列不受 GROUP BY、HAVING 或 DISTINCT 子句的影响。
当同时指定了WITH CHECK OPTION时,TOP不能在视图的select_statement中的任何地方使用。
Sooo...如果它是您想要实现的相对直接的
插入
或更新
,那么这是很有可能的 - 但查询(和视图)越复杂,它就越困难。如果可以的话,我还会提供一些反对这样做的建议 - 在我看来,它增加了不必要的复杂性,并且(作为必须解开使用此过程的其他查询的人)维护起来确实很痛苦。
Short answer: Yes. But there are restrictions.
Eg: (taken from http://msdn.microsoft.com/en-us/library/ms180800(v=sql.90).aspx )
Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.
The columns that are being modified in the view must reference the underlying data in the table columns directly. They cannot be derived in any other way, such as through:
The columns that are being modified cannot be affected by GROUP BY, HAVING, or DISTINCT clauses.
TOP cannot be used anywhere in the select_statement of the view when WITH CHECK OPTION is also specified.
Sooo... if it is a relatively straight forward
insert
orupdate
you're trying to achieve, then it is very possible - but the more complex the query (and the view), the more difficult it becomes.I'd also offer some advice against doing this if you can - in my opinion it adds an unnecessary layer of complication and (as someone who has had to unravel others queries that use this process) it is a real pain to maintain.
不可以,视图中不能有插入或更新语句。请参阅
CREATE VIEW
的文档,它强制定义必须是
SELECT
语句。No you cannot have an insert or update statement in a view. Refer to the documentation for
CREATE VIEW
It enforces that the definition must be a
SELECT
statement.使用返回表的存储过程或函数可能会更好。
It would probably be better to instead use a stored procedure or function that returns a table.