Skip to content Skip to sidebar Skip to footer

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.StaffPrefer

Solution 1:

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 orfunction'v_ViewName' is not updatable because the modification affects multiple base tables.

Solution 2:

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

Solution 3:

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)

Solution 4:

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:

deletefromT778deletefromB778deletefromH778

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

Post a Comment for "Sql Updatable View With Joined Tables"