Oracle 数据库链接。检查是否存在或覆盖?

发布于 2025-01-03 13:48:34 字数 364 浏览 0 评论 0原文

我需要在创建数据库链接之前检查数据库链接是否已存在。我怎样才能做到这一点?

我正在编写一个以此开头的 SQL 脚本:

DROP DATABASE LINK mydblink

然后创建一个:

CREATE DATABASE LINK mydblink
CONNECT TO testuser
IDENTIFIED BY mypswd
USING 'mypersonaldb'

如果数据库链接不存在,我当然会在第一步中收到错误。如果我省略第一步并继续创建数据库链接,我将再次收到错误消息,指出它已存在同名。

我可以做什么来检查数据库链接是否已经存在?

I need to check if a database link already exists before I create one. How can I do that?

I am writing an SQL script that starts with this:

DROP DATABASE LINK mydblink

then I create one:

CREATE DATABASE LINK mydblink
CONNECT TO testuser
IDENTIFIED BY mypswd
USING 'mypersonaldb'

I will of course get an error in the first step if the database link doesn't exists. And if I omit the first step and just go ahead and create a db link, I will again get an error saying that it already exists with the same name.

What can I do in order to check if the the database link already exists?

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

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

发布评论

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

评论(2

身边 2025-01-10 13:48:34
SELECT COUNT(1)
  FROM dba_objects -- user_objects
 WHERE object_type = 'DATABASE LINK'
   AND object_name = 'ARGUS51P';

例如(未经测试):

declare
  l_link_cnt pls_integer := 0;
  l_sql varchar2(32767);
begin
  -- link creation sql (fill in details of how you want this created)
  l_sql := 'create public database link ...';

  select count(1)
  into l_link_cnt
  from dba_objects
  where object_type = 'DATABASE LINK'
  and object_name = 'SOME_LINK';

  -- create link if it doesn't exist yet
  if (l_link_cnt = 0) then
    -- create link 
    execute immediate l_sql;

  end if;

end;
SELECT COUNT(1)
  FROM dba_objects -- user_objects
 WHERE object_type = 'DATABASE LINK'
   AND object_name = 'ARGUS51P';

For example (untested):

declare
  l_link_cnt pls_integer := 0;
  l_sql varchar2(32767);
begin
  -- link creation sql (fill in details of how you want this created)
  l_sql := 'create public database link ...';

  select count(1)
  into l_link_cnt
  from dba_objects
  where object_type = 'DATABASE LINK'
  and object_name = 'SOME_LINK';

  -- create link if it doesn't exist yet
  if (l_link_cnt = 0) then
    -- create link 
    execute immediate l_sql;

  end if;

end;
噩梦成真你也成魔 2025-01-10 13:48:34

Oracle 无法在 DROP 或 CREATE 之前测试是否存在。 (好吧,您可以编写一些 PL/SQL,但是,这可能比其价值更麻烦。)在 Oracle 脚本编写中,在脚本中简单地执行 DROP 和 CREATE 是相当标准的。如果 DROP 出错,那就这样吧。它不会影响脚本的执行。

-标记

Oracle has no way to test for existence before a DROP or CREATE. (Well, ok, you could write some PL/SQL, but, that's probably more trouble than it's worth.) In Oracle scripting, it's pretty standard to simply do both the DROP and the CREATE in a script. If the DROP errors out, so be it. It won't affect execution of the script.

-Mark

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