Just a day before I got a very interesting email. Here is the email (modified a bit to make it relevant to this blog post).
“Pinal,
We are facing a very strange issue.
One of our query related to backup files and backup set has stopped working suddenly in SSMS. It works fine in application where we have and in the stored procedure but when we have it in our SSMS it gives following error.
Msg 208, Level 16, State 1, Line 1
Invalid object name ‘dbo.backupfile’.
Here are our queries which we are trying to execute.
SELECT name, database_name, backup_size, TYPE,
compatibility_level, backup_set_id
FROM dbo.backupset;
SELECT logical_name, backup_size, file_type
FROM dbo.backupfile;
This query gives us details related to backupset and backup files when the backup was taken.”
When I receive this kind of email, usually I have no answers directly. The claim that it works in stored procedure and in application but not in SSMS gives me no real data. I have requested him to very first check following two things:
- If he is connected to correct server? His answer was yes.
- If he has enough permissions? His answer was he was logged in as an admin.
This means there was something more to it and I requested him to send me a screenshot of the his SSMS. He promptly sends that to me and as soon as I receive the screen shot I knew what was going on.
Before I say anything take a look at the screenshot yourself and see if you can figure out why his queries are not working in SSMS. Just to make your life a bit easy, I have already given a hint in the image.
The answer is very simple, the context of the database is master database. To execute above two queries the context of the database has to be msdb. Tables backupset and backupfile belong to the database msdb only.
Here are two workaround or solution to above problem:
1) Change context to MSDB
Above two queries when they will run as following they will not error out and will give the accurate desired result.
USE msdb
GO
SELECT name, database_name, backup_size, TYPE,
compatibility_level, backup_set_id
FROM dbo.backupset;
SELECT logical_name, backup_size, file_type
FROM dbo.backupfile;
2) Prefix the query with msdb
There are cases above script used in stored procedure or part of big query, it is not possible to change the context of the whole query to any specific database. Use three part naming convention and prefix them with msdb.
SELECT name, database_name, backup_size, TYPE,
compatibility_level, backup_set_id
FROM msdb.dbo.backupset;
SELECT logical_name, backup_size, file_type
FROM msdb.dbo.backupfile;
Very simple solution but sometime keeps people wondering for an answer.
Reference: Pinal Dave (https://blog.sqlauthority.com)
64 Comments. Leave new
Little bit Tricky…..
It implies that you should have your basics clear like why “temp db” and why “master”
lol I’ve been using sql for years and I still get this error. It pays to check you have the basics covered before you go off thinking there is some serious connection issues and what not.
Hi, I got the same error in case of backupfiles.
thank you
I tried BOTH “fixes”–and still have the same error. What now?
this error come out to me today, I could not solve the problem until I disconnected n connected again, that way I solve this problem , of course I had to show my database in “availables bata bases”
hi, I had the same problem today, I SOLVE THIS PROBLEM WHEN I DISCONNECTED I CONNECTED AGAIN, , of couse I always put my database name in “availlable databases” list
You have solved lot of issues I have encountered in sql.
Thanks a lot!:)
Thanks
There is a ‘master’ drop down table to the right of the ‘New Query’ box. Make sure to select the correct category and you should find that will resolve your issue.
thanq very much :)
OMG, really tricky
thanks you. :)
thanq solved my problem…
If you are using the right database and the intellisense is still underlining objects you have just created, refresh it in SSMS.
Edit -> Intellisense -> Refresh Local Cache
Or, CTRL+SHIFT+R
No more red squigly line!
Hi…. I was getting the same error. I checked my Database name in top dropdown, it was preselected correctly. Then I used the query USE [myDatabaseName] and it still dint work. Suddenly saw your post and tried ctrl+shift+R and it worked. Thanks alot
thank you very much. helped me much information.
Really Pinal X’tra Ordinary..sometimes if something (brain) dnt work..people like ‘U’ are with us..
Thanks
Thanks alot
It may cause due to cache is not updated so
-> edit ->intelisence ->refresh local cahe
or simply
ctrl+shift+R
thanks am facing this same issue
thanx u save my day
Thank YOu