Bryan
Bryan

Reputation: 43

SQL Server Mgmt Studio shows "invalid column name" when listing columns?

I'm used to scripting in Python or Matlab, and my first couple hours with SQL have been infuriating. I would like to make a list of columns appear on the screen in any way, shape, or form; but when I use commands like

select * 
from "2Second Log.dbo.TagTable.Columns"

I keep getting the error:

Invalid column name '[the first column in my table]'.

even though I never explicitly asked for [the first column in my table], it found it for me. How can you correctly identify the first column name, and then still claim it's invalid!? Babies will be strangled.

My data structure, query, and the error message.

This db was generated by Allen Bradley's FactoryTalk software. What I would really like to do is produce an actual list of "TagName" strings...but I get the same error when I try that. If there were a way to actually double click the table and open it up and look at it (like in Matlab), that would be ideal.

Upvotes: 1

Views: 1996

Answers (2)

NonProgrammer
NonProgrammer

Reputation: 1387

always use brackets when names/field have spaces or dashes.

select * from [2Second Log].dbo.TagTable

Upvotes: 1

Cpt. Monac
Cpt. Monac

Reputation: 769

Echoing juergen's suggestion in the comment above. It looks like you're running the query on the master database, not the 2Second Log database that actually has your table. (You can tell this by looking at the database in the dropdown in the top left of your screenshot). Two things you can do:

  • Change the dropdown in the top left to 2Second Log. This will target your query to a different database
  • Put your database name in brackets as suggested by juergen i.e. select * from [2Second Log].dbo.TagTable

As an side, if you're looking for a good SQL tutorial, I highly recommend the Mode SQL tutorial. It's a fantastic interactive platform to get your SQL feet wet.

Upvotes: 3

Related Questions