question

Bab avatar image
Bab asked

View doesnt return any record sometimes

This is really strange!! Never saw this before. I have a view. when running just a simple query like this, it doesn't return any records. select * from vwRequest where request = 'Request' but if i run the query from the view itself i get the result. Not sure what could be the issue. or if this is some kind of bug in SQL. The view is joining with 2 other views. If there is any issue with other two views then the query from the view shouldn't bring any results. so i am thinking if the issue is with the view name but that couldn't be possible. If any one run into issue like this and have any kind of suggestion or resolution, please share.
view
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tom Staab avatar image
Tom Staab answered
It's difficult to say for certain what the problem is, but I have a suggestion I'd like you to try. When you say running the query from the view returns different results than querying the view, how are you testing that? The best way would be to use a sub-select or CTE something like this: WITH vwRequest AS ( SELECT ... (view contents) ) SELECT * FROM vwRequest WHERE request = 'Request' ; If you just add the "WHERE request = 'Request'" to the view definition itself, it can sometimes return different results depending on the view definition. I'll give a simple example here. Assume a table with an Id column with values 1 through 5. CREATE TABLE #t (Id int); INSERT #t (Id) SELECT * FROM (VALUES(1),(2),(3),(4),(5)) t(Id) ; Now let's consider the following view along with the 2 SELECT statements after it. GO CREATE VIEW vwMaxId AS SELECT Id = MAX(Id) FROM #t ; GO SELECT * FROM vwMaxId WHERE Id = 3; -- filter from view SELECT Id = MAX(Id) FROM #t WHERE Id = 3; -- filter using view definition The first query won't return any rows, but the second will return one row with Id = 3. I hope this helps. Please let us know whether or not it does and if you have any further questions.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Bab avatar image
Bab answered
Thanks Tom for your input.. After keep researching, I found that i had "Select Top 1000 .." on the view and that's why when querying the view it doesn't return any records sometime.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.