12

Since i am new with sql server, my question is

  1. Can we update views?

  2. Can we update real table data in views?

1
  • 1
    A view is just a select over a table - a view doesn't "have" data itself.... If you want to update data - update the underlying table; once you do, the view will immediately show this updated data
    – marc_s
    Commented Aug 31, 2011 at 5:32

2 Answers 2

11

If you update a view you will actually be updating the underlying table. A view is nothing physical, just a representation of the data underneath it.

In most circumstances you can perform UPDATE statements via VIEWS as long as they're setup to cater for UPDATEs (you can read more about enabling views for UPDATES here: http://msdn.microsoft.com/en-us/library/25614805(v=vs.80).aspx ) but it's generally considered less-than-great practise to do so. It makes things become very convoluted so you'd have to have a good reason to be doing it that way.

1
  • 2
    My theriy is that since you can only update one table in a view at a time and you therfore need to know which field views are in which table, it makes no sense not to directly update the tables. The exceptions would be when you are using views to restrict the user to a subset of the data in one table. Then it makes sense to update through the view.
    – HLGEM
    Commented Aug 31, 2011 at 14:02
-1

Yes underlying table data can be updated by Updating a view.

The point to note here is, as long as the View is created based on one single table then direct "Update View" statement would work.

But if the view is created based on multiple tables then a direct Update statement won't work. We need to make use of "Instead of Triggers" in those scenarios.

3
  • 15
    Not quite true. The View can be updated if based on multiple tables as long as the update only affects one table. Conversely a View can be based on a single table but still not be updatable (e.g. if it contains a GROUP BY ) Commented Aug 31, 2011 at 13:12
  • This answer should really be deleted it is false.
    – JonH
    Commented Mar 5, 2019 at 14:32
  • 2
    The answer may be false but @Martin Smith 's comment is gold.
    – Wtower
    Commented Dec 8, 2021 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.