SQL Server reports 'Invalid column name', but the column is present and the query works through management studio

SqlSql Server

Sql Problem Overview


I've hit a bit of an impasse. I have a query that is generated by some C# code. The query works fine in Microsoft SQL Server Management Studio when run against the same database.

However when my code tries to run the same query I get the same error about an invalid column and an exception is thrown. All queries that reference this column are failing.

The column in question was recently added to the database. It is a date column called Incident_Begin_Time_ts .

An example that fails is:

select * from PerfDiag 
where Incident_Begin_Time_ts > '2010-01-01 00:00:00';

Other queries like Select MAX(Incident_Being_Time_ts); also fail when run in code because it thinks the column is missing.

Any ideas?

Sql Solutions


Solution 1 - Sql

Just press Ctrl + Shift + R and see...

In SQL Server Management Studio, Ctrl+Shift+R refreshes the local cache.

Solution 2 - Sql

I suspect that you have two tables with the same name. One is owned by the schema 'dbo' (dbo.PerfDiag), and the other is owned by the default schema of the account used to connect to SQL Server (something like userid.PerfDiag).

When you have an unqualified reference to a schema object (such as a table) — one not qualified by schema name — the object reference must be resolved. Name resolution occurs by searching in the following sequence for an object of the appropriate type (table) with the specified name. The name resolves to the first match:

  • Under the default schema of the user.
  • Under the schema 'dbo'.

The unqualified reference is bound to the first match in the above sequence.

As a general recommended practice, one should always qualify references to schema objects, for performance reasons:

  • An unqualified reference may invalidate a cached execution plan for the stored procedure or query, since the schema to which the reference was bound may change depending on the credentials executing the stored procedure or query. This results in recompilation of the query/stored procedure, a performance hit. Recompilations cause compile locks to be taken out, blocking others from accessing the needed resource(s).

  • Name resolution slows down query execution as two probes must be made to resolve to the likely version of the object (that owned by 'dbo'). This is the usual case. The only time a single probe will resolve the name is if the current user owns an object of the specified name and type.

[Edited to further note]

The other possibilities are (in no particular order):

  • You aren't connected to the database you think you are.
  • You aren't connected to the SQL Server instance you think you are.

Double check your connect strings and ensure that they explicitly specify the SQL Server instance name and the database name.

Solution 3 - Sql

In my case I restart Microsoft SQL Sever Management Studio and this works well for me.

Solution 4 - Sql

If you are running this inside a transaction and a SQL statement before this drops/alters the table you can also get this message.

Solution 5 - Sql

I eventually shut-down and restarted Microsoft SQL Server Management Studio; and that fixed it for me. But at other times, just starting a new query window was enough.

Solution 6 - Sql

If you are using variables with the same name as your column, it could be that you forgot the '@' variable marker. In an INSERT statement it will be detected as a column.

Solution 7 - Sql

Just had the exact same problem. I renamed some aliased columns in a temporary table which is further used by another part of the same code. For some reason, this was not captured by SQL Server Management Studio and it complained about invalid column names.

What I simply did is create a new query, copy paste the SQL code from the old query to this new query and run it again. This seemed to refresh the environment correctly.

Solution 8 - Sql

In my case I was trying to get the value from wrong ResultSet when querying multiple SQL statements.

Solution 9 - Sql

In my case it seems the problem was a weird caching problem. The solutions above didn't work.

If your code was working fine and you added a column to one of your tables and it gives the 'invalid column name' error, and the solutions above doesn't work, try this: First run only the section of code for creating that modified table and then run the whole code.

Solution 10 - Sql

Including this answer because this was the top result for "invalid column name sql" on google and I didn't see this answer here. In my case, I was getting Invalid Column Name, Id1 because I had used the wrong id in my .HasForeignKey statement in my Entity Framework C# code. Once I changed it to match the .HasOne() object's id, the error was gone.

Solution 11 - Sql

I've gotten this error when running a scalar function using a table value, but the Select statement in my scalar function RETURN clause was missing the "FROM table" portion. :facepalms:

Solution 12 - Sql

Also happens when you forget to change the ConnectionString and ask a table that has no idea about the changes you're making locally.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkidl33tView Question on Stackoverflow
Solution 1 - SqlMangeshView Answer on Stackoverflow
Solution 2 - SqlNicholas CareyView Answer on Stackoverflow
Solution 3 - SqlRexhiView Answer on Stackoverflow
Solution 4 - SqlStaggView Answer on Stackoverflow
Solution 5 - SqlIAM_AL_XView Answer on Stackoverflow
Solution 6 - SqlDévan CoetzeeView Answer on Stackoverflow
Solution 7 - SqlApplePieView Answer on Stackoverflow
Solution 8 - SqlDeepak KatariaView Answer on Stackoverflow
Solution 9 - SqlLoMaPhView Answer on Stackoverflow
Solution 10 - SqlDanielView Answer on Stackoverflow
Solution 11 - SqlcdabelView Answer on Stackoverflow
Solution 12 - SqlИво НедевView Answer on Stackoverflow