SQL 连接来自 2 个不同服务器的表:R 与 SAS
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有直接的方法可以跨两台服务器加入。您可以在其中一个服务器(具有更多数据)中创建一个临时表,并用来自其他表/服务器的数据填充它。这样,您将移动最少量的数据(而不是从两个表中提取数据)并利用 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.