0

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?

Satinder singh
  • 10,100
  • 16
  • 60
  • 102
Dimo
  • 3,238
  • 6
  • 29
  • 46

2 Answers2

1

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 @@.

Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362
0

In standard SQL,tables names that start with numbers are not allowed.You can start the name of the table with a letter and then you can use number.Note that dynamically creating a tables is a sign that you need to work on your database design.

Ahmed
  • 452
  • 3
  • 7
  • But if your requirement remains the same then you can try this. CREATE TABLE [47] (ID int,val varchar(150)) But using such dynamical names lowers the performance of your database. – Ahmed Jun 30 '13 at 13:52