Why can't I "SELECT INTO" a database whose name starts with a number?

Main.dbName = "7202" 

query = "select * into " + Main.dbName + ".dbo.[AccountReceivableHistory]
        from " + dbOrigin + ".dbo.[AccountReceivableHistory] where
        AccountReceivableHistory].Date >= '2012-12-27' and    
        AccountReceivableHistory].Date < '2012-12-28'"

      

The error says

Syntax error about "7202".

+3


source to share


1 answer


You can use a number for the table name (or database name or schema name - see my comment), however you will need to avoid this with []

:

select *
into [123].[456].[789] 
from OtherTable;

      

Where [123]

represents a database, [456]

represents a schema, and [789]

represents a table.

Edit

In the interest of standardization , for example. if there is any chance of future portability between RDBMSs, note that preference should be given to using double quotes "

instead of []

escaping, for example.



SELECT * from "123"."456"."789";

      

However, you need to make sure the connection is established set QUOTED_IDENTIFIER ON

.

If the object names are built dynamically, note that you can also override the default escalation of the function QUOTENAME

by providing a character separator as the second parameter, for example:

select QUOTENAME('123', '"');

      

+9


source







All Articles