带有连接表的 SQL 可更新视图
我有一个看起来与此类似的视图,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的语句应该可以正常工作,因为您只修改一个表 (
StaffPreferences
) 中的列。如果您尝试在同一更新语句中更新不同表中的列,您将收到错误。
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.
http://download.oracle.com/docs /cd/B10501_01/server.920/a96521/views.htm#391
http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/views.htm#391
我认为您可以看到,如果
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
withStaffId
25, but no matching row inStaffPreferences
. There are various right things you could do (preserve the appearance that this is a table, perform an insert inStaffPreferences
; 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)
这是我解决问题的方法。
就我而言,它是表,而不是视图,但我需要在引用表的数据构造中找到引用该表的模式 ID,例如
our_schema
。我运行了以下命令:
这给了我 id 778(示例)
然后我查看了该 ID 显示的位置,前缀为 T、B 或 H。
在我们的示例中,我们有表、基本表和历史表,其中数据为存储。
然后我运行:
这允许我删除数据并绕过该限制。
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:
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:
This allowed me to delete the data and bypass that restriction.