无法在Azure SQL中查询跨数据库。

huangapple go评论99阅读模式
英文:

Not able to query cross database in Azure SQL

问题

我尝试将数据插入到另一个数据库的另一个表中,但我遇到了一个错误:

> 在此版本的SQL Server中,对于数据库和/或服务器名称 'xxx.xxx.xxx' 的引用不受支持。

当尝试从另一个数据库中进行选择时,我收到相同的错误消息。

我的SQL代码:

  1. INSERT INTO xxx.xxx.xxx
  2. VALUES (xx,'xx', 'xxxx', xx)
英文:

I tried to insert data into another table, in another database, but I get an error:

> Reference to database and/or server name in 'xxx.xxx.xxx' is not supported in this version of SQL Server

I'm getting the same error when trying to select from another database.

My SQL code:

  1. INSERT INTO xxx.xxx.xxx
  2. VALUES (xx,'xx', 'xxxx', xx)

答案1

得分: 1

在Azure SQL数据库中,不支持跨数据库查询。您无法从一个数据库向另一个数据库插入记录。

您不能在Azure SQL中对跨数据库使用DML语句。

  • 唯一的插入记录的方法是连接到正确的数据库,或者如果您在SSMS/ADS/门户中,则切换数据库上下文,然后执行您的语句。
  • 关于跨数据库查询,您需要使用弹性查询,例如外部表和外部数据源,建议由Alberto Morillo提供。

示例代码:

  1. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password';
  2. CREATE DATABASE SCOPED CREDENTIAL samplecred2
  3. WITH IDENTITY = 'username',
  4. SECRET = 'password';
  5. CREATE EXTERNAL DATA SOURCE RemoteReferenceData
  6. WITH
  7. (
  8. TYPE=RDBMS,
  9. LOCATION='servername.database.windows.net',
  10. DATABASE_NAME='db name',
  11. CREDENTIAL= samplecred
  12. );
  13. CREATE EXTERNAL TABLE [dbo].[tableename]
  14. (
  15. Id int,
  16. Name varchar(20)
  17. )
  18. WITH
  19. (
  20. DATA_SOURCE = RemoteReferenceData,
  21. SCHEMA_NAME = 'schemaname',
  22. OBJECT_NAME = 'tablename'
  23. );

现在您可以在外部表上执行类似的查询。

参考链接 - Azure SQL数据库中的跨数据库查询 - Microsoft社区中心

英文:

In Azure SQL database Cross database quires are not supported You cannot insert records from one database to another database.

You cannot use DML statements on Cross database in azure SQL.

  • The only way to insert records is connect to the correct database or switch the database context if you're in SSMS/ADS/ portal Then you can execute your statements.
  • Regarding the cross database quires, you need to use elastic queries like external tables and external data sources suggested by Alberto Morillo

Sample Code:

  1. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password';
  2. CREATE DATABASE SCOPED CREDENTIAL samplecred2
  3. WITH IDENTITY = 'username',
  4. SECRET = 'password';
  5. CREATE EXTERNAL DATA SOURCE RemoteReferenceData
  6. WITH
  7. (
  8. TYPE=RDBMS,
  9. LOCATION='servername.database.windows.net',
  10. DATABASE_NAME='db name',
  11. CREDENTIAL= samplecred
  12. );
  13. CREATE EXTERNAL TABLE [dbo].[tableename]
  14. (
  15. Id int,
  16. Name varchar(20)
  17. )
  18. WITH
  19. (
  20. DATA_SOURCE = RemoteReferenceData,
  21. SCHEMA_NAME = 'schemaname',
  22. OBJECT_NAME = 'tablename'
  23. );

Now you can execute quires on the external table which looks similar.

Reference -Cross-database Query in Azure SQL Database - Microsoft Community Hub

huangapple
  • 本文由 发表于 2023年8月10日 19:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定