There is a table in my database that starts with the @ symbol. I am able to query it successfully using the following query:

select * from [@tablename]

This format works in SSMS and Crystal Reports.

However, I have a C# application where the same query give an 'invalid object name' error on the table name.

Any suggestions?

  • 1
    How are you issuing the query to the database? EF, ADO, stored procedure. hard-coded sql? Can you post an example? – Mr.Brownstone Feb 16 '16 at 20:51
  • 8
    Why don't you fix the table name? At best, the current name is terribly misleading, at worst it is an identifier rule nightmare requiring square brackets in all cases and not functioning at all in some of those. What happens in C# if you reference it correctly (with schema prefix)? – Aaron Bertrand Feb 16 '16 at 20:54

I do not believe that there is a specific problem with referencing tables, in C#, having names starting with @. I was able to create a table with a name of @TableName and was able to execute SELECT SUM(Col1) FROM [@TableName]; successfully. I used SqlConnection and SqlCommand, not EF or DataSet / DataTable. Are you sure that you are connecting to the database that contains this table?

Most likely the error message is very accurate and you are not in the correct database. Either specify the desired database in the Connection String, or use a fully qualified name: [DatabaseName].[SchemaName].[@TableName].

Of course, if you have a choice, then you should not choose to name any object starting with @ as that is the symbol used to denote local variables, system variables, and input/output parameters.

  • Thanks for the help. When I use the fully qualified name [dbname].[dbo].[@tablename] then it works. However, there are a few other tables that also start with @ that I run the same select statement on and none of them require the fully qualified name, only this particular table. Not sure why. I would change the name but I am dealing with a 3rd party system, so that's not an option. – wsb Feb 17 '16 at 15:08
  • @wsb Are all of the tables in the same DB? Are they all in the same Schema? Try removing the DB name so it is just [dbo].[@tablename] and see what happens. If it stops working, then it is in a different DB than what you are connecting to. If it still works, then you need to give us the exact name of that table else I can't test it. – Solomon Rutzky Feb 17 '16 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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