Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL Invalid Object Name

I have

cursor.execute("select RM_ID FROM Sales.dbo.MARKETING where VERSION = 'SomeVersion'")

which gives me the traceback error:

pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Sales.dbo.MARKETING'. (208) (SQLExecDirectW)")

I have several other lines of code:

cursor.execute("select RUNDATEEST FROM Sales.dbo.MARKETING where VERSION = 'SomeVersion'")

that are exactly the same except for the column name that give me no error. I'm not sure what my mistake is.

Of note:

  • I have already checked the table for the column name to make sure it exists.
  • I have noticed that this column is the key for this table. Perhaps a different syntax is required for keys?
  • When I execute the query in SQL Server, it runs just fine.
like image 636
James.Wyst Avatar asked Mar 17 '14 17:03

James.Wyst


People also ask

What is invalid object name in SQL?

This typically means 1 of 2 things... you've referenced an object (table, trigger, stored procedure,etc) that doesn't actually exist (i.e., you executed a query to update a table, and that table doesn't exist). Or, the table exists, but you didn't reference it correctly...

What is SQL object name?

In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier. A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.

Why it is showing invalid column name in SQL?

An invalid column name error in SQL means that the column name violates the conditions of the column name. If you reference an object that does not exist in the table or the name exists, but you did not reference it correctly, you will get this error.


1 Answers

Try to surround schema and table names with brackets:

[Sales].[dbo].[MARKETING]

Perhaps you need to surround column names in the same way.

like image 102
toscanelli Avatar answered Sep 28 '22 18:09

toscanelli