使用 SSMS 时,可以在 SQL 脚本中指定数据库吗?
When using SSMS, can you specify the database within a SQL Script?
我在多个 Azure 数据库上工作,使用 AzureAD - SSMS 中的通用 MFA。
我通常有许多不同的脚本,比较不同数据库之间的结果,然后一个小时左右后,连接被服务器终止,如果我尝试重新 运行它,它告诉我无法识别 table,因为它不再知道要连接到哪个数据库。
有没有办法在 SSMS 中指定数据库,如果我在脚本之前尝试 USE dbDocument
,它会告诉我 Msg 40508, Level 16, State 1, Line 1 USE statement is not supported to switch between databases. Use a new connection to connect to a different database.
。
有没有办法解决这个问题,而不必右键单击有问题的数据库并点击新查询?
看着 this MS doc,我怀疑这根本不可能...
If a database other than the current database is provided, the USE statement does not switch between databases, and error code 40508 is returned. To change databases, you must directly connect to the database. The USE statement is marked as not applicable to SQL Database at the top of this page, because even though you can have the USE statement in a batch, it doesn't do anything.
Azure SQL 数据库不支持 USE 语句。一种解决方案是根据您要使用的数据库将您的工作分成几个不同的步骤,并为每个步骤连接到正确的数据库。
如果您想在多个 Azure 数据库上执行相同的查询,那么您可以使用以下选项之一:
- 使用horizontal partitioned databases based on shard map using the elastic database. Here你可以找到如何查询远程数据库。
- 您也可以使用elastic queries。
- 或者您可以根据 Azure Data Lake.
创建您的解决方案
我在多个 Azure 数据库上工作,使用 AzureAD - SSMS 中的通用 MFA。
我通常有许多不同的脚本,比较不同数据库之间的结果,然后一个小时左右后,连接被服务器终止,如果我尝试重新 运行它,它告诉我无法识别 table,因为它不再知道要连接到哪个数据库。
有没有办法在 SSMS 中指定数据库,如果我在脚本之前尝试 USE dbDocument
,它会告诉我 Msg 40508, Level 16, State 1, Line 1 USE statement is not supported to switch between databases. Use a new connection to connect to a different database.
。
有没有办法解决这个问题,而不必右键单击有问题的数据库并点击新查询?
看着 this MS doc,我怀疑这根本不可能...
If a database other than the current database is provided, the USE statement does not switch between databases, and error code 40508 is returned. To change databases, you must directly connect to the database. The USE statement is marked as not applicable to SQL Database at the top of this page, because even though you can have the USE statement in a batch, it doesn't do anything.
Azure SQL 数据库不支持 USE 语句。一种解决方案是根据您要使用的数据库将您的工作分成几个不同的步骤,并为每个步骤连接到正确的数据库。
如果您想在多个 Azure 数据库上执行相同的查询,那么您可以使用以下选项之一:
- 使用horizontal partitioned databases based on shard map using the elastic database. Here你可以找到如何查询远程数据库。
- 您也可以使用elastic queries。
- 或者您可以根据 Azure Data Lake. 创建您的解决方案