1

I have been following a SQL course for the past few weeks and I never have had this problem: my database is connected but I get the error message saying "Invalid Object Name". Please see picture.enter image description here

3
  • 1
    You're running the query in the master database, not AdventureWorksLT2012. Voting to close this as it is a very basic error. Nov 8, 2017 at 20:25
  • @LowlyDBA: ...and you never started at the beginning? That's just plain rude.
    – InteXX
    Apr 27, 2018 at 22:33
  • Alan, pay this 'superior' man no heed. You're on the right track. I upvoted your question.
    – InteXX
    Apr 27, 2018 at 22:35

3 Answers 3

4

Your SQL Query is in the wrong database. You are trying to select from the AdventureWorksLT2012 database, but you're connected to the Master database.

If you look at the top left of your window, you will see a bar at the bottom of the ribbon that contains a drop-down list. In it, you can see "master" is the current database.

There are three ways to resolve this in your queries. After you do so, the red lines under your table and column names should all vanish, as they're warning you that the tables and columns don't exist in the current selected database.

  1. Change the drop-down to the correct database. This will fix your query window for that window and that window only, but is the most common solution.
  2. Change your queries to reference the database. This would be queries like Select * from AdventureWorksLT2012.SalesLT.SalesOrderDetail for example. Setting the DB name in your query means it will no longer matter which database your current context is.
  3. Start the query off with a USE [AdventureWorksLT2012]; Go; statement. This will switch your context to the correct database. After running the query, the screen will show this database in the drop down list I mentioned in option 1.
1

Your SSMS screenshot says you're in the master (look in the toobar area) database instead of the Adventureworks database.

1

You say you're new to SQL, well you'll find that there's A LOT to learn and mistakes like being in the wrong DB will soon become second nature. Other contributors will have pointed you in the right direction in this instance but I'd say stick with it. It can be a rewarding and challenging career, but you won't become an expert overnight!

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.