Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database VIEW does not reflect the data in the underying TABLE

Input:

The customer claims that the application (.NET) when querying for some data returns data different from when the customer looks into the data table directly.

I understand there could be various reasons and in completely different places. My purpose is not to solve it here, but rather to ask experienced DBAs and DB developers if:

Is it possible for a VIEW to show data that does not match the underlying TABLE(s)?

  • What are possible causes/reasons for this?
  • Can an UPDATE statement on a view cause future SELECTs to return 'updated' data, when the table really does not?

Possible causes (please comment on those with question-marks):

  1. the reason is that there are two separate transactions, which would explain the customers' confusion.
  2. the underlying table was altered, but the view was not refreshed (using sp_refreshview)
  3. a different user is connecting and can see different data due to permissions ?
  4. programmer error: wrong tables/columns, wrong filters (all-in-one here)
  5. corruption occurs: DBCC CHECKDB should help
  6. can SELECT ... FOR UPDATE cause this ???
  7. ? __

What really happened (THE ANSWER):

Column positions were altered in some tables: Apparently the customer gave full database access to a consultant for database usage analysis. That great guy changed the order of the columns to see the few audit fields at the beginning of the table when using SELECT * ... clauses.

Using dbGhost the database schema was compared to the schema of the backup taken few days before the problem appeared, and the column position differences were discovered.

What came next was nothing related to programming, but more an issue of politics.

Therefore the sp_refreshview was the solution. I just took one step more to find who caused the problem. Thank you all.

like image 605
van Avatar asked Jun 18 '09 16:06

van


People also ask

Will changes in view reflects on table?

Yes. The data "in" a view has no existence independent from the tables that make up the view. The view is, in essence, a stored SELECT statement that masquerades as a table. The data is stored in the original tables and only "assembled" into the view when you want to look at it.

Does view get updated when table is updated?

Yes , View gets updated. Please note that View is a logical statement and the data in view is not stored anywhere, the data is returned from database every time we select data from view.

What is the purpose of database views?

A database view is a subset of a database and is based on a query that runs on one or more database tables. Database views are saved in the database as named queries and can be used to save frequently used, complex queries.

What is the difference between a table and a view?

A view is a database object that allows generating a logical subset of data from one or more tables. A table is a database object or an entity that stores the data of a database.


2 Answers

Yes, sort of.

Possible Causes:

  1. The View needs to be refreshed or recompiled. Happens when source column definitions change and the View (or something it depends on) is using "*", can be nasty. Call sp_RefreshView. Can also happen because of views or functions (data sources) that it calls as well.

  2. The View is looking at something different from what they/you think. They are looking at the wrong table or view.

  3. The View is transforming the data in an unexpected way. It works right, just not like they expected.

  4. The View is returning a different subset of the data than expected. Again, it works right, just not like they think.

  5. They are looking at the wrong database/server or with a Logon/user identity that causes the View to alter what it shows. Particularly nefarious because unlike Management Studio, most client programs do not tell you what database/server they are pointed at.

like image 161
RBarryYoung Avatar answered Sep 18 '22 21:09

RBarryYoung


it is possible if the underlying table has been changed and sp_refreshview has not been ran against the view, so the view will have missing columns if those were added to the table.

To see what I mean read how to make sure that the view will have the underlying table changes by using sp_refreshview

like image 43
SQLMenace Avatar answered Sep 18 '22 21:09

SQLMenace