Oracle:在存储过程中使用数据库链接:表或视图不存在

发布于 2024-12-10 18:42:25 字数 2084 浏览 0 评论 0原文

我目前遇到一个问题,无法在存储过程中引用链接数据库中的表。我收到错误消息:

ORA-00942: 表或视图不存在

以下是我在主机(运行 Oracle 10g)上设置到远程数据库(运行 Oracle 11g)的数据库链接所采取的步骤。步骤是准确的,但有些名称已更改,但保持一致。

  1. 更新 tnsnames.ora,添加新条目:

    <前><代码>REMOTE_DB = (描述= (地址 =(协议 = TCP) (主机 = 10.10.10.10) (队列大小 = 20) (端口=1521) ) (连接数据= (SERVICE_NAME = 远程服务) ) )
  2. 创建数据库链接,作为稍后将创建和执行存储过程的用户:

    创建数据库链接remote_link
    连接到“remote_user”
    由“remote_pass”标识
    使用“REMOTE_DB”;
    
  3. 通过从中选择来证明数据库链接正在工作:

    从remote_table@remote_link中选择id;
    
    ID
    -------------------------------------------------- ------------------------------------------
    8ac6eb9b-fcc1-4574-8604-c9fd4412b917
    c9e7ee51-2314-4002-a684-7817b181267b
    cc395a81-56dd-4d68-9bba-fa926dad4fc7
    d6b450e0-3f36-411a-ba14-2acc18b9c008
    
  4. 创建依赖于工作数据库链接的存储过程:

    创建或替换
    过程 test_remote_db_link
    作为
    v_id varchar(50);
    开始   
        从remote_table@remote_link中选择id到v_id,其中id ='c9e7ee51-2314-4002-a684-7817b181267b';
        dbms_output.put_line('v_id : ' || v_id);
    结束测试_远程_数据库_链接;
    
  5. 在盯着以下错误消息一整天后爆炸自己的头:

    错误(10,27):PL/SQL:ORA-00942:表或视图不存在
    

我尝试了很多方法来尝试解决这个问题,包括:

  1. 创建数据库链接时,不在用户名和密码周围使用引号。链接创建良好,但从中选择会出现此错误:

    第 1 行出现错误:
    ORA-01017: 用户名/密码无效;登录被拒绝
    ORA-02063: TWS_LINK 的前一行
    
  2. 尝试了大写/小写的用户名和密码的各种组合。收到与 1 相同的错误。

  3. 尝试在用户名和密码周围使用单引号而不是双引号。收到此错误:

    第 1 行出现错误:
    ORA-00987: 用户名缺失或无效
    
  4. 证明我可以通过使用 sqlplus 连接到远程数据库来完全访问远程数据库:

    [oracle]$ sqlplus remote_user/remote_pass@REMOTE_DB
    
    SQL*Plus:版本 10.2.0.1.0 - 于 2011 年 10 月 20 日星期四 22:23:12 投入生产
    
    版权所有 (c) 1982、2005,Oracle。版权所有。
    
    
    连接到:
    Oracle Database 11g 企业版版本 11.2.0.2.0 - 64 位生产版
    具有分区、OLAP、数据挖掘和真实应用程序测试选项
    
    SQL> 
    

我不知道下一步该做什么。下一步可能是开始查看远程数据库上的问题,也许看看其他数据库是否可以连接到它。另一个方法是查看从主机 10g 到远程 11g 的不兼容性。

I currently have an issue whereby I cannot reference a table in a linked database within a stored procedure. I get the error message:

ORA-00942: table or view does not exist

Here are the steps I took on the host machine (running oracle 10g) to set up the database link to the remote database (running oracle 11g). The steps are accurate, but some some names have been changed, though they have been kept consistent.

  1. Update tnsnames.ora, adding a new entry:

    REMOTE_DB =
        (DESCRIPTION =
            (ADDRESS = (PROTOCOL = TCP)
                       (HOST = 10.10.10.10)
                       (QUEUESIZE = 20)
                       (PORT = 1521)
            )
            (CONNECT_DATA =
                       (SERVICE_NAME = remote_service)
            )
        )
    
  2. Create database link, as the user who will later be creating and executing the stored procedure:

    create database link remote_link
    connect to "remote_user"
    identified by "remote_pass"
    using 'REMOTE_DB';
    
  3. Prove database link is working by selecting from it:

    select id from remote_table@remote_link;
    
    id
    --------------------------------------------------------------------------------
    8ac6eb9b-fcc1-4574-8604-c9fd4412b917
    c9e7ee51-2314-4002-a684-7817b181267b
    cc395a81-56dd-4d68-9bba-fa926dad4fc7
    d6b450e0-3f36-411a-ba14-2acc18b9c008
    
  4. Create stored procedure that depends on working database link:

    create or replace
    PROCEDURE test_remote_db_link
    AS
    v_id varchar(50);
    BEGIN   
        select id into v_id from remote_table@remote_link where id = 'c9e7ee51-2314-4002-a684-7817b181267b';
        dbms_output.put_line('v_id : ' || v_id);
    END test_remote_db_link;
    
  5. Explode own head after staring at the following error message for over an entire working day:

    Error(10,27): PL/SQL: ORA-00942: table or view does not exist
    

I have tried many things to try to sort this issue out, including:

  1. When creating the database link, not using quotes around the username and password. Link creates fine, but selecting from it gives me this error:

    ERROR at line 1:
    ORA-01017: invalid username/password; logon denied
    ORA-02063: preceding line from TWS_LINK
    
  2. Tried various combinations of username and password in upper/lowercase. Received same error as 1.

  3. Tried single quotes instead of double quotes around username and password. Recieved this error:

    ERROR at line 1:
    ORA-00987: missing or invalid username(s)
    
  4. Proved I have full access to the remote db by connecting into it with sqlplus:

    [oracle]$ sqlplus remote_user/remote_pass@REMOTE_DB
    
    SQL*Plus: Release 10.2.0.1.0 - Production on Thu Oct 20 22:23:12 2011
    
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    
    
    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    
    SQL> 
    

I'm not sure what to do next. The possible next step is to start looking at issues on the remote database, and perhaps see if other databases can connect to it. Another would be to look at incompatibilities going from host 10g to remote 11g.

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

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

发布评论

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

评论(3

做个少女永远怀春 2024-12-17 18:42:25

好的,所以我能够让这个工作,某种程度。

事实证明,在创建数据库链接时,用户名和密码字段周围的双引号导致了问题。总结一下:

如果它们存在,并且按如下方式创建链接:

create database link remote_link
connect to "remote_user"
identified by "remote_pass"
using 'REMOTE_DB';
  1. 远程数据库可以通过sql查询
  2. 存储过程无法编译,接收ORA-942错误
  3. 由于程序无法编译,因此无法执行

当双引号不存在时:

create database link remote_link
connect to remote_user
identified by remote_pass
using 'REMOTE_DB';
  1. 远程数据库无法通过sql查询,收到无效密码错误(问题中有详细说明)
  2. 所存储的程序可以编译且没有错误。
  3. 存储过程按预期执行,通过数据库链接检索数据并显示它。

因此,即使无法通过 sql 查询远程数据库,收到无效密码错误,使用相同连接信息的过程也可以正常编译和执行。

我相信你会同意,这是一个奇怪的事件状态,我真的偶然发现让它在我的场景中发挥作用。我不太确定我会称其为解决方案,因为还有很多未解答的问题。

希望如果有人通过谷歌来到这里,他们会发现这个答案很有用,并且至少能让他们的代码运行。

GC。

OK so I was able to get this working, of sorts.

It turns out that when creating the database link, the double quotes around the username and password fields were causing the issue. To summarise:

If they were present, and the link created as so:

create database link remote_link
connect to "remote_user"
identified by "remote_pass"
using 'REMOTE_DB';
  1. The remote database could be queried via sql
  2. The stored procedure could not be compiled, recieving the ORA-942 error
  3. As the procedure could not be compiled, it could not be executed

When the double quotes are not present:

create database link remote_link
connect to remote_user
identified by remote_pass
using 'REMOTE_DB';
  1. The remote database could not be queried via sql, recieving an invalid password error (detailed in the question)
  2. The stored procedure could be compiled with no errors.
  3. The stored procedure executes as expected, retrieving data from across the database link, and displaying it.

So, even though the remote database cannot be querued via sql, recieving an invalid password error, the procedure that uses this same connection information compiles and executes normally.

I'm sure you'll agree, this is a curious state of events, and I genuinely stumbled across making it work in my scenario. I'm not quite sure I would call it a solution, as there are plenty of unanswered questions.

Hopefully if someone comes here via google, they'll find this answer useful, and at least get their code running.

GC.

雪落纷纷 2024-12-17 18:42:25

我在 11gR2 上遇到了同样的问题,感谢这个论坛帮助我找到问题。使数据库链接在 SQL 和过程中工作的方法是遵循以下语法(仅将密码括在双引号内)。

create database link remote_link
connect to remote_user
identified by "remote_pass"
using 'REMOTE_DB';

I faced the same issue on 11gR2, and I'm thankful to this forum for helping me find the problem. The way to make the db link work in both SQL and procedure is to follow the below syntax (enclose only the password within double quotes).

create database link remote_link
connect to remote_user
identified by "remote_pass"
using 'REMOTE_DB';
分开我的手 2024-12-17 18:42:25

我想我在这里看到了一个问题。执行存储过程的用户是否与创建存储过程的用户相同?

您说,“创建数据库链接,作为稍后将执行存储过程的用户”。

如果创建数据库链接的用户与创建存储过程的用户不同,那可能是您的问题。

尝试以同一用户身份创建存储过程和数据库链接,或创建公共数据库链接。

然后,由于 Oracle 默认是定义者权限,因此您可以让任何人执行该存储过程(假设他们已被授予对该过程的执行权限)。

I think I see a problem here. Is the user who is executing the stored procedure the same user who created the stored procedure?

You said, "Create database link, as the user who will later be executing the stored procedure".

If the user creating the database link is different from the user creating the stored procedure, that may be your problem.

Try creating the stored procedure and database link as the same user, or creating a public database link.

Then, since Oracle default is definer rights, you can have anyone execute the stored procedure (assuming they have been granted execute privilege on the procedure).

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