通过 dblink 选择 *
当尝试通过循环游标(通过 dblink 从源表中选择)来更新表时,我遇到了一些麻烦。
我有两个数据库DB1,DB2。
它们是两个不同的数据库实例。 我在 DB1 中使用以下语句:
CURSOR TestCursor IS
SELECT a.*, 'A' TEST_COL_A, 'B' TEST_COL_B
FROM rpt.SOURCE@DB2 a;
BEGIN
For C1 in TestCursor loop
INSERT into RPT.TARGET
(
/*The company_name and cust_id are select from SOURCE table from DB2*/
COMPANY_NAME, CUST_ID, TEST_COL_A, TEST_COL_B
)
values
(
C1.COMPANY_NAME, C1.CUST_ID, C1.TEST_COL_A , C1.TEST_COL_B
) ;
End loop;
/*Some code...*/
End
一切正常,直到我将列“NEW_COL”添加到 SOURCE table@DB2
插入数据得到错误的值。
正如我所期望的, TEST_COL_A 的值应该是“A”。
但是,它包含我在 SOURCE 表中添加的 NEW_COL 值。
TEST_COL_B 的值包含“A”。
有人遇到同样的问题吗? 看来oracle在编译时缓存了表列。 有没有办法在不重新编译的情况下向源表添加列?
I have some trouble when trying to update a table by looping cursor which select from source table through dblink.
I have two database DB1, DB2.
They are two different database instance.
And I am using this following statement in DB1:
CURSOR TestCursor IS
SELECT a.*, 'A' TEST_COL_A, 'B' TEST_COL_B
FROM rpt.SOURCE@DB2 a;
BEGIN
For C1 in TestCursor loop
INSERT into RPT.TARGET
(
/*The company_name and cust_id are select from SOURCE table from DB2*/
COMPANY_NAME, CUST_ID, TEST_COL_A, TEST_COL_B
)
values
(
C1.COMPANY_NAME, C1.CUST_ID, C1.TEST_COL_A , C1.TEST_COL_B
) ;
End loop;
/*Some code...*/
End
Everything works fine until I add a column "NEW_COL" to SOURCE table@DB2
The insert data got the wrong value.
The value of TEST_COL_A , as I expect, should be 'A'.
However, it contains the value of NEW_COL which i add at SOURCE table.
And the value of TEST_COL_B contains 'A'.
Have anyone encounter the same issue?
It seems like oracle cache the table columns when it compile.
Is there any way to add a column to source table without recompile?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此:
在这种情况下,您不会完全看到错误,但原因是相同的。如果您使用显式列名称而不是
*
,也不会有问题,无论如何,这通常更安全。如果您使用*
,则无法避免重新编译(除非,我认为*
是选择列表中的最后一项,在这种情况下,任何额外的列最后不会造成问题——只要他们的名字不冲突)。According to this:
In this case you aren't quite seeing errors, but the cause is the same. You also wouldn't have a problem if you used explicit column names instead of
*
, which is usually safer anyway. If you're using*
you can't avoid recompiling (unless, I suppose, the*
is the last item in the select list, in which case any extra columns on the end wouldn't cause a problem - as long as their names didn't clash).我建议您在 DB1 中使用单个集合处理插入语句,而不是一次一行游标 for 循环进行插入,例如:
基本原理:
处理[这是一次非常慢的处理]。
select *
构造是危险的,因为您遇到[和其他类似原因]。
I recommend that you use a single set processing insert statement in DB1 rather than a row at a time cursor for loop for the insert, for example:
Rationale:
processing [which is really slow-at-a-time processing].
select *
construct is dangerous for the reason youencountered [and other similar reasons].