Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Updatable View with joined tables

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?

like image 816
Red Taz Avatar asked Sep 02 '11 08:09

Red Taz


2 Answers

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.
like image 145
Mikael Eriksson Avatar answered Oct 14 '22 18:10

Mikael Eriksson


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

like image 20
Nathan Q Avatar answered Oct 14 '22 19:10

Nathan Q