14

In SQL Server 2005, I have some views created by using a SELECT statement. Can we write UPDATE and DELETE statements in views?

halfer
  • 19,824
  • 17
  • 99
  • 186
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

7 Answers7

25

from this MSDN article: Modifying Data Through a View,

  • 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:

    • An aggregate function (AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR and VARP).
    • 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.

and see the article for remaining ...

heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
14

Well you can delete from a view if that is what you are asking, but you can't have a view that deletes information. The view is a portion of data from the underlying tables. Provided that you have permissions, you can do the same data manipulation in views that you can do to a table directly.

So you can do something like:

DELETE FROM my_View WHERE id = 3;

When to use views
What is a view

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • so we can't write like this create view myview as delete from emp where dept=10 end i think it is not write am i right – Surya sasidhar Jun 27 '10 at 14:01
  • 1
    @Surya - No. You can't do that. I can't really envisage why you would want to either. You would use a stored procedure for that. – Martin Smith Jun 27 '10 at 14:11
3

What if I do following

Create view table1_View 
as 
   select * 
   from table1 

go 

delete 
from table1_view

I checked and this command delete all data from table1

Taryn
  • 242,637
  • 56
  • 362
  • 405
Subhash
  • 31
  • 1
  • 2
    Because it derives from only ONE base table. As per MSDN you can do Insert, update, delete on a view as long as it is derived from just a single table. – SamuraiJack Dec 02 '15 at 18:04
2

**

Using Views to Update Data:

** A view can be used in a query that updates data, subject to a few restrictions. Bear in mind that a view is not a table and contains no data—the actual modification always takes place at the table level. Views cannot be used as a mechanism to override any constraints, rules, or referential integrity defined in the base tables.

Restrictions on Updating Data Through Views You can insert, update, and delete rows in a view, subject to the following limitations:

If the view contains joins between multiple tables, you can only insert and update one table in the view, and you can't delete rows.

You can't directly modify data in views based on union queries. You can't modify data in views that use GROUP BY or DISTINCT statements.

All columns being modified are subject to the same restrictions as if the statements were being executed directly against the base table.

Text and image columns can't be modified through views.

There is no checking of view criteria. For example, if the view selects all customers who live in Paris, and data is modified to either add or edit a row that does not have City = 'Paris', the data will be modified in the base table but not shown in the view, unless WITH CHECK OPTION is used when defining the view. For more information Check this Article

Srikanth
  • 980
  • 3
  • 16
  • 30
1

In addition to the limited updating allowed on the view itself, you can use an INSTEAD OF trigger to perform much more involved changes. INSTEAD OF basically lets you intercept the update or delete, and perform almost any change. References: MSDN Article.

Ken Redler
  • 23,863
  • 8
  • 57
  • 69
1

More explanation on Srinivas answer on the point of..

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

There is a workaround for this using INSTEAD OF of triggers as long as you can manage your base table structure. INSTEAD OF triggers allow you to override an INSERT, UPDATE, or DELETE operation on a view. For example, you might define an INSTEAD OF INSERT trigger on a view to replace the standard INSERT statement.

Assume you created the below view:

CREATE VIEW AuthorsNames

AS

SELECT au_id, au_fname, au_lname

FROM authors 

You might want to insert data to columns not visible in the view. To do so, create an INSTEAD OF trigger on the view to handle inserts.

CREATE TRIGGER ShowInsert on AuthorsNames

INSTEAD OF INSERT

AS

BEGIN

INSERT INTO authors

   SELECT address, au_fname, au_id, au_lname, city, contract, phone, state, zip

   FROM inserted

END

Using this method you can insert into multiple tables but this becomes more complex if you are dealing with lot of underlying tables. Ref MSDN

Niranga
  • 768
  • 12
  • 24
0

The Answer lies on if the view is an UPDATABLE VIEW or a NON UPDATABLE VIEW.

An updatable view is a view which contains all the not null columns from the underlying table.

If so , ur update , delete and insert query on the view affects the data on the underlying real table .

Thats it .....