Search code examples
asp.netsqlsql-servertablename

Tablename as Numeric in SQL Server using Asp.net


I have the following code:

SqlConnection connection = new SqlConnection(@"Data Source=MY-PC;Initial Catalog=master;Integrated Security=True");
connection.Open();
 using (SqlCommand command = new SqlCommand("CREATE TABLE Table1 (Weight INT, Name TEXT, Breed TEXT)", connection))
  {
     command.ExecuteNonQuery();
  }
 connection.Close();

Using this I am able to add a new table called "Table1". But as soon as I change the table name to only numbers, such as using "CREATE TABLE 123456 (Weight INT, Name TEXT, Breed TEXT)".

I get

SQLException: Incorrect syntax near '123456'.

How can I create a tablename with only numbers?


Solution

  • In SQL-Server table names cannot start with a number. See the Rules for Regular Identifiers.

    The first character [of an identifier] must be one of the following:

    • A letter as defined by the Unicode Standard 2.0. The Unicode definition of letters includes Latin characters from a through z and from A through Z, in addition to letter characters from other languages.

    • The underscore (_), "at" sign (@), or number sign (#).
      Certain symbols at the beginning of an identifier have special meaning in SQL Server. An identifier beginning with the "at" sign denotes a local variable or parameter. An identifier beginning with a number sign denotes a temporary table or procedure. An identifier beginning with double number signs (##) denotes a global temporary object. Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, it is recommended that you do not use names that start with @@.