为什么我不能“SELECT INTO”名称以数字开头的数据库?

时间:2022-05-08 22:50:24
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 near '7202'.

'7202'附近的语法错误。

1 个解决方案

#1


9  

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

您可以使用数字作为表名(或数据库名称或模式名称 - 请参阅我的注释),但是,您需要使用[]来转义它:

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

Where [123] represents database, [456] represents schema and [789] represents a table.

其中[123]表示数据库,[456]表示模式,[789]表示表。

Edit

In the interests of standardisation, e.g. if there is a chance of future portability between RDBMS, note that preference should be given to use double quotes " instead of [] for escaping, e.g.

为了标准化,例如如果RDBMS之间存在未来可移植性的可能性,请注意应优先使用双引号“而不是[]进行转义,例如:

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

You will however need to ensure that set QUOTED_IDENTIFIER ON is set on the connection.

但是,您需要确保在连接上设置了QUOTED_IDENTIFIER设置。

If the names of objects are being constructed dynamically, note that it is also possible to override the default escaping of the QUOTENAME function by providing a character delimiter as the second parameter, e.g.:

如果动态构造对象的名称,请注意,也可以通过提供字符分隔符作为第二个参数来覆盖QUOTENAME函数的默认转义,例如:

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

#1


9  

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

您可以使用数字作为表名(或数据库名称或模式名称 - 请参阅我的注释),但是,您需要使用[]来转义它:

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

Where [123] represents database, [456] represents schema and [789] represents a table.

其中[123]表示数据库,[456]表示模式,[789]表示表。

Edit

In the interests of standardisation, e.g. if there is a chance of future portability between RDBMS, note that preference should be given to use double quotes " instead of [] for escaping, e.g.

为了标准化,例如如果RDBMS之间存在未来可移植性的可能性,请注意应优先使用双引号“而不是[]进行转义,例如:

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

You will however need to ensure that set QUOTED_IDENTIFIER ON is set on the connection.

但是,您需要确保在连接上设置了QUOTED_IDENTIFIER设置。

If the names of objects are being constructed dynamically, note that it is also possible to override the default escaping of the QUOTENAME function by providing a character delimiter as the second parameter, e.g.:

如果动态构造对象的名称,请注意,也可以通过提供字符分隔符作为第二个参数来覆盖QUOTENAME函数的默认转义,例如:

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