1

Here are the steps to understand what happened (I am using Microsoft SQL Server Management Studio 2017):

  1. I imported an Excel file as a table
  2. I renamed my table to dbo.export
  3. To test, I created a new query and simply wrote the statement SELECT * FROM dbo.export - it runs fine
  4. I save my query
  5. I close SSMS
  6. I reopen SSMS and open my query
  7. When I try to execute my query now, it gives me the error:

Msg 208, Level 16, State 1, Line 1. Invalid object name 'dbo.export'

I made no changes to anything. My code now suddenly doesn't work. Is SQL Server somehow changing the object type of my table? I really have no idea what's the issue since I changed absolutely nothing.

2
  • 2
    When you imported the excel sheet, it has created a database for you... that DB must have a name (let's say MyTestDataBase). When you reopen the management console, make sure you select that database. Commented Aug 31, 2019 at 23:42
  • 1
    I suspect you are pointed to the master database. USE yourdatabasename Commented Aug 31, 2019 at 23:42

6 Answers 6

4

You are probably in the master database. Either select your database where it says "master" in SSMS: enter image description here

or use the name of the database in your query like this:

DBNAME.dbo.export

or in the beginning of the query, type

USE DBNAME;

And then go on with your query SELECT * FROM export;

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the database in the first line then use your selection query:

USE databaseName;

SELECT * FROM export;

Comments

1

You must have created a Database for importing the excelsheet. Make sure you select the same database when you run the query:

enter image description here

In the example above, AdventureWorks2012 is the selected Database.

Alternatively you can include the DB name in your query:

SELECT * FROM myDbName.dbo.export

Comments

1

When you close your query the default database will change to the master database.

Every time you open a query, you should select the database. Choose one of these ways.

1) Select your database from the combo-box on toolbar on top of the SSMS menu.

2) Just write this command before your T-SQL statement.

 USE 'place your database name'

Comments

0

Just solved mine

  1. include the databasename and the tablename ie select * from databasename.dbo.tablename

The above should solve the problem 👌

1 Comment

As far as I can tell, everything in your answer already appears in the other answers. Can you tell me what your answer contains that does not appear in any of the other answers?
0

in 2024, I also experienced the same problem. I tried all the methods mentioned but all failed. Then I realized that I could use MSSM to figure out the problem. I connected to the remote database and then chose the 'select top 1000 rows'. The script showed as select top 1000 * from databasename.schemaname.tablename.

I don't know why people didn't mention this previously. The correct way of executing a query mentioning a table is always as below no matter what tools do you use:

use databasename; # if you didn't use a database in the first place
select * from schemaname.tablename;
go

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.