SQL 视图中的 DML 语句

发布于 2024-12-06 19:29:11 字数 84 浏览 1 评论 0原文

我正在尝试在该视图中创建一个视图,我想根据特定条件插入一条记录或更新一条记录,以便我们可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-12-13 19:29:11

简短回答:是的。但也有限制。

例如:(取自 http://msdn.microsoft .com/en-us/library/ms180800(v=sql.90).aspx )

任何修改(包括 UPDATE、INSERT 和 DELETE 语句)都必须引用来自只有一张基表。

在视图中修改的列必须直接引用表列中的基础数据。它们不能以任何其他方式导出,例如通过:

  1. 聚合函数(AVG、COUNT、SUM、MIN、MAX、GROUPING、STDEV、STDEVP、VAR 和 VARP)。 2. 计算;无法使用其他列从表达式计算该列。使用集合运算符(UNION、UNION ALL、CROSSJOIN、EXCEPT 和 INTERSECT)形成的列相当于计算,并且不可更新。

正在修改的列不受 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:

  1. An aggregate function (AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR and VARP). 2. A computation; the column cannot be computed from an expression using other columns. Columns formed using set operators (UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT) amount to a computation and are also not updatable.

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 or update 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.

转角预定愛 2024-12-13 19:29:11

不可以,视图中不能有插入或更新语句。请参阅CREATE VIEW 的文档,

CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ] 
[ WITH <view_attribute> [ ,...n ] ] 
AS select_statement 
[ WITH CHECK OPTION ] [ ; ]

它强制定义必须是SELECT 语句。

No you cannot have an insert or update statement in a view. Refer to the documentation for CREATE VIEW

CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ] 
[ WITH <view_attribute> [ ,...n ] ] 
AS select_statement 
[ WITH CHECK OPTION ] [ ; ]

It enforces that the definition must be a SELECT statement.

我不是你的备胎 2024-12-13 19:29:11

使用返回表的存储过程或函数可能会更好。

It would probably be better to instead use a stored procedure or function that returns a table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文