2014-09-10 87 views
1

我有3个表,即Customer_1,属于Schema_1的cust_accts和属于Schema_2的Customer_2。所有表都在同一个数据库中。如何从2个不同模式连接2个表,但在同一个数据库中

我有这样

declare 

l_cust_id customer_1.customer_id%type; 

begin 
select customer_id into l_cust_id 
from cust_accts 
where 
customer_number=''; 

insert into Customer_2 
(
column_1, 
column_2, 
) 
select 
d1.column_1, 
d1.column_2 
from 
customer_1 d1 
where d1.customer_id=l_cust_id; 
commit; 
end; 

一个SQL查询现在在这里,我的问题是所有我应该把模式名称在这些表的面前,就像这样,所有我必须把schema_1.customer_1。并且请不要将所有必需的db连接,连接都给予访问这些表。

在此先感谢您的答案。

+0

在表格之前引用架构。 – Sathya 2014-09-10 08:18:16

+0

嗨,谢谢,所以在每个地方我都必须参考模式礼仪? – user2647888 2014-09-10 08:22:08

+0

该文档告诉您如何引用[其他模式对象](http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements009.htm#SQLRF51134);和[每个命令的语法](http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2126073)都会显示模式的添加位置。也不确定你的意思是关于连接和链接,如果这是在同一个数据库。 – 2014-09-10 08:23:39

回答

2

假设您没有任何同义词并且已授予必要的特权,则需要为不在当前模式中的任何对象指定模式。如果您已连接的Schema_1那么你需要前缀由Schema_2拥有的对象:

insert into Schema_2.Customer_2 (...) 
select ... from customer_1 d1 
where ... 

如果你连为Schema_2那么你需要前缀由Schema_1拥有的对象:

insert into Customer_2 (...) 
select ... from Schema_1.customer_1 d1 
where ... 

(“连接为”是一种简化,如果你是新的我觉得就足够了,你可以在会话中改变你的当前模式,但将只是现在混淆!)

如果你想确切相同的SQL对任何用户或具有正确权限的任何其他用户均有效恩,你可以前缀的所有对象:

insert into Schema_2.Customer_2 (...) 
select ... from Schema_1.customer_1 d1 
where ... 

但随后或许应该通过简单的程序包管理。

该文档有更多关于schema, objects, references and name resolution

+0

嗨,亚历克斯,谢谢你的回答,现在我知道了有关前缀的事情后,我看到了你分享的链接。 – user2647888 2014-09-11 14:14:52

2

以下SQL完美工作:

SELECT COUNT(*) 从SCHEMA1.TABLE1 ST1 INNER JOIN SCHEMA2.TABLE2 ST2 ON ST1.COMMON_FIELD = ST1.COMMON_FIELD;

相关问题