带有连接表的 SQL 可更新视图

发布于 2024-12-02 18:06:29 字数 573 浏览 3 评论 0原文

我有一个看起来与此类似的视图,

SELECT  dbo.Staff.StaffId, dbo.Staff.StaffName, dbo.StaffPreferences.filter_type
FROM    dbo.Staff LEFT OUTER JOIN
        dbo.StaffPreferences ON dbo.Staff.StaffId = dbo.StaffPreferences.StaffId

我正在尝试使用更新 StaffPreferences.filter_type

UPDATE vw_Staff SET filter_type=1 WHERE StaffId=25

我在 MSDN 文章中读到了这一点,

任何修改,包括 UPDATE、INSERT 和 DELETE 语句, 必须仅引用一个基表中的列。

这是否意味着我只能更新 dbo.Staff 中的字段(这是我目前所能实现的)在这种情况下,“基表”的定义不会扩展到任何后续连接的表吗?

I have a view that looks similar to this,

SELECT  dbo.Staff.StaffId, dbo.Staff.StaffName, dbo.StaffPreferences.filter_type
FROM    dbo.Staff LEFT OUTER JOIN
        dbo.StaffPreferences ON dbo.Staff.StaffId = dbo.StaffPreferences.StaffId

I'm trying to update StaffPreferences.filter_type using,

UPDATE vw_Staff SET filter_type=1 WHERE StaffId=25

I have read this in an MSDN article,

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

Does this mean that I can only update fields in dbo.Staff (which is all I can currently achieve) In this context does the definition of 'base table' not extend to any subsequently joined tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

佞臣 2024-12-09 18:06:29

您的语句应该可以正常工作,因为您只修改一个表 (StaffPreferences) 中的列。

如果您尝试在同一更新语句中更新不同表中的列,您将收到错误。

Msg 4405, Level 16, State 1, Line 7
View or function 'v_ViewName' is not updatable because the modification affects multiple base tables.

Your statement should work just fine since you are only modifying column(s) from one table (StaffPreferences).

If you tried to update a columns from different tables in the same update statement you would get an error.

Msg 4405, Level 16, State 1, Line 7
View or function 'v_ViewName' is not updatable because the modification affects multiple base tables.
哆兒滾 2024-12-09 18:06:29

可更新连接视图的规则如下:

一般规则

对连接视图的任何 INSERT、UPDATE 或 DELETE 操作只能进行修改
一次一个底层基表。

更新规则连接视图的所有可更新列必须映射到
键保留表的列。请参阅“密钥保留表”了解
键保留表的讨论。如果视图是用
WITH CHECK OPTION 子句,然后是所有连接列和所有列
重复的表不可更新。

删除规则

只要只有一行,就可以删除连接视图中的行
连接中保留键的表。如果视图是用WITH定义的
重复 CHECK OPTION 子句和键保留表,然后
无法从视图中删除行。

INSERT 规则 INSERT 语句不得显式或
隐式引用非键保留表的列。如果
连接视图是使用WITH CHECK OPTION子句INSERT定义的
不允许发表声明。

http://download.oracle.com/docs /cd/B10501_01/server.920/a96521/views.htm#391

The rules for updatable join views are as follows:

General Rule

Any INSERT, UPDATE, or DELETE operation on a join view can modify only
one underlying base table at a time.

UPDATE Rule All updatable columns of a join view must map to
columns of a key-preserved table. See "Key-Preserved Tables" for a
discussion of key-preserved tables. If the view is defined with the
WITH CHECK OPTION clause, then all join columns and all columns of
repeated tables are non-updatable.

DELETE Rule

Rows from a join view can be deleted as long as there is exactly one
key-preserved table in the join. If the view is defined with the WITH
CHECK OPTION clause and the key preserved table is repeated, then the
rows cannot be deleted from the view.

INSERT Rule An INSERT statement must not explicitly or
implicitly refer to the columns of a nonkey preserved table. If the
join view is defined with the WITH CHECK OPTION clause, INSERT
statements are not permitted.

http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/views.htm#391

海之角 2024-12-09 18:06:29

我认为您可以看到,如果 Staff 中存在 StaffId 25 的行,但 StaffPreferences 中没有匹配的行,则可能会出现一些问题。您可以做各种正确的事情(保留这是一个表的外观,在 StaffPreferences 中执行插入;拒绝更新;等等)。

我认为此时,SQL Server 引擎将放弃,您必须编写一个触发器来实现您想要的行为(无论是什么。您需要考虑连接工作/不工作的所有情况)

I think you can see some of the problems that might occur if there's a row in Staff with StaffId 25, but no matching row in StaffPreferences. There are various right things you could do (preserve the appearance that this is a table, perform an insert in StaffPreferences; reject the update; etc).

I think at this point, the SQL Server engine will give up, and you'll have to write a trigger that implements the behaviour you want (whatever that may be. You need to consider all of the cases for the join working/not working)

风向决定发型 2024-12-09 18:06:29

这是我解决问题的方法。

就我而言,它是表,而不是视图,但我需要在引用表的数据构造中找到引用该表的模式 ID,例如 our_schema

我运行了以下命令:

select schemaid from our_schema where name = "MY:Form"

这给了我 id 778(示例)

然后我查看了该 ID 显示的位置,前缀为 T、B 或 H。

在我们的示例中,我们有表、基本表和历史表,其中数据为存储。

然后我运行:

delete from T778
delete from B778
delete from H778

这允许我删除数据并绕过该限制。

Here is how I solved it.

In my case it was table, not a view, but I needed to find the schema id that referenced the table in the data construction in a reference table, say called our_schema.

I ran the following:

select schemaid from our_schema where name = "MY:Form"

This gave me the id as 778 (example)

Then I looked where this ID was showing up with a prefix of T, B, or H.

In our case we have Table, Base and History tables where the data is stored.

I then ran:

delete from T778
delete from B778
delete from H778

This allowed me to delete the data and bypass that restriction.

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