SQL 连接来自 2 个不同服务器的表:R 与 SAS

发布于 2025-01-16 22:28:27 字数 1358 浏览 2 评论 0原文

我在 R 中设置了两个不同的连接:

connection_1 <- dbConnect(odbc::odbc(), driver = my_driver, database = "database_1", uid = "my_id", pwd = "my_pwd", server = "server_1", port = "my_port)

connection_2 <- dbConnect(odbc::odbc(), driver = my_driver, database = "database_2", uid = "my_id", pwd = "my_pwd", server = "server_2", port = "my_port)

我有一个表存储在“connection_1”(table_1)中,另一个表存储在“connection_2”(table_2)中。我想将这两个表连接在一起并将结果表保存在“connection_1”上:

dbGetQuery(connection_1, "create table my_table as select * from connection_1.table_1 a inner join connection_2.table_2 B on A.Key_1 = B.Key_2")

但我不确定这在 R 中是否可行。

  • 有谁知道我编写的代码是否可以更改来执行此操作?

  • 或者建立“connection_2”会自动取消“connection_1”吗?

谢谢你!

旁白:如果我使用 SAS,我可以像这样解决上述问题:

#connection 1

%let NZServer = 'server_1';
$ let NZSchema = 'my_schema_1';
% let NZDatawork = 'database_1';
$ let SAS_LIB = 'LIB_1';

LIBNAME  ....

#connection 2

%let NZServe = 'server_2';
$ let NZSchem = 'my_schema_2';
% let NZDatawor = 'database_2';
$ let SAS_LI = 'LIB_2';

#remove last letter from each word to make it different 

LIBNAME  ...;


# run earlier join:

proc sql outobs = 100;

create table LIB_1.a as select * from LIB_1.table_1 a inner join LIB_2.table_2 B on A.Key_1 = B.Key_2;

quit;

I have set up two different connections in R:

connection_1 <- dbConnect(odbc::odbc(), driver = my_driver, database = "database_1", uid = "my_id", pwd = "my_pwd", server = "server_1", port = "my_port)

connection_2 <- dbConnect(odbc::odbc(), driver = my_driver, database = "database_2", uid = "my_id", pwd = "my_pwd", server = "server_2", port = "my_port)

I have a table stored in "connection_1" (table_1), and another table stored in "connection_2" (table_2) . I would like to join these two tables together and save the resulting table on "connection_1":

dbGetQuery(connection_1, "create table my_table as select * from connection_1.table_1 a inner join connection_2.table_2 B on A.Key_1 = B.Key_2")

But I am not sure if this is possible in R.

  • Does anyone know if the code I have written can be changed to do this?

  • Or will establishing "connection_2" automatically cancel "connection_1"?

Thank you!

Aside: If I was using SAS, I could have solved the above problem like this:

#connection 1

%let NZServer = 'server_1';
$ let NZSchema = 'my_schema_1';
% let NZDatawork = 'database_1';
$ let SAS_LIB = 'LIB_1';

LIBNAME  ....

#connection 2

%let NZServe = 'server_2';
$ let NZSchem = 'my_schema_2';
% let NZDatawor = 'database_2';
$ let SAS_LI = 'LIB_2';

#remove last letter from each word to make it different 

LIBNAME  ...;


# run earlier join:

proc sql outobs = 100;

create table LIB_1.a as select * from LIB_1.table_1 a inner join LIB_2.table_2 B on A.Key_1 = B.Key_2;

quit;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2025-01-23 22:28:27

没有直接的方法可以跨两台服务器加入。您可以在其中一个服务器(具有更多数据)中创建一个临时表,并用来自其他表/服务器的数据填充它。这样,您将移动最少量的数据(而不是从两个表中提取数据)并利用 netezzas 共置联接来加速查询。

There is no straightforward way to join across two servers. You could create a temp table in one of the serer (that has more data) and populate it with data from the other table/server. That way you will be moving the least amount of data (as opposed to extracting from both tables) and utilizing netezzas colocated join to speed up your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文