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

前端 未结 12 612
生来不讨喜
生来不讨喜 2020-12-04 11:35

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 whe

相关标签:
12条回答
  • 2020-12-04 11:40

    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.

    0 讨论(0)
  • 2020-12-04 11:45

    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:

    0 讨论(0)
  • 2020-12-04 11:46

    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.

    0 讨论(0)
  • 2020-12-04 11:46

    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.

    0 讨论(0)
  • 2020-12-04 11:49

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

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

    0 讨论(0)
  • 2020-12-04 11:49

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

    0 讨论(0)
提交回复
热议问题