4

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?

Solomon Rutzky
  • 70,048
  • 8
  • 160
  • 306
wsb
  • 81
  • 1
  • 5

2 Answers2

7

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.

Solomon Rutzky
  • 70,048
  • 8
  • 160
  • 306
-1
con.Execute("SELECT H.DocEntry, DocNum, LineId,  CreateDate, Creator" +
                " FROM \"@RETURNABLEGP_H\" H" +
                " INNER JOIN \"@RETURNABLEGP_R\" R ON H.DocEntry = R.DocEntry" +
                " WHERE CreateDate = '2022-01-06'", ref dt);

This query execute fine with C# and MSSQL. I also face the same issue mentioned above. So when dealing with table names starting with @ sign you have to put in double quotation (") marks as shown in above query.

Marcello Miorelli
  • 17,274
  • 53
  • 180
  • 320