van van - 28 days ago 10
SQL Question

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.

Answer

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 becuase of views or functions (data sources) that it calls too.

  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 or with a Logon/user identity that causes the View to alter what it shows.