如何使用dblink从远程数据库创建表

huangapple go评论67阅读模式
英文:

How to create a table from remote database using dblink

问题

创建使用dblink从远程数据库创建表格的方法

create table MYTABLE@DBLINK (name varchar2(20)));

> 原因: 尝试在远程数据库上执行DDL操作。
> 例如,"CREATE TABLE tablename@remotedbname ..."。

> 操作: 要更改远程数据库结构,必须使用适当的权限连接到
> 远程数据库。

这是权限错误还是使用dblink无法完成的操作?

英文:

How to create a table from remote database using dblink

create table MYTABLE@DBLINK (name varchar2(20)));

> *Cause: An attempt was made to use a DDL operation on a remote database.
> For example, "CREATE TABLE tablename@remotedbname ...".
>
> *Action: To alter the remote database structure, you must connect to the
> remote database with the appropriate privileges.

Is this a privilege error or it is not possible to do with dblink?

答案1

得分: 3

如果您成功创建了数据库链接,显然您拥有该用户的凭据,因此最简单的选项是实际上连接为远程用户,并直接在该模式中创建表。

如果您想要从您的模式中执行此操作,您无法使用该语法,但是有一种解决方法:dbms_utility.exec_ddl_statement

以下是一个示例。

首先创建数据库链接:

SQL> create database link dbl_mike connect to mike identified by lion using '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = my_server)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XEPDB1)))';

Database link created.

它是否有效?是的!

SQL> select * from dual@dbl_mike;

D
-
X

这是您尝试的操作,并且发现它不起作用:

SQL> create table test_dbl@dbl_mike (id number);
create table test_dbl@dbl_mike (id number)
                     *
ERROR at line 1:
ORA-02021: DDL operations are not allowed on a remote database

我提到的解决方法:

SQL> exec dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl (id number)');

PL/SQL procedure successfully completed.

测试(如果表存在,我可以通过数据库链接插入一行并从该表中进行选择):

SQL> insert into test_dbl@dbl_mike (id) values (100);

1 row created.

SQL> select * from test_dbl@dbl_mike;

        ID
----------
       100

SQL>

[编辑]

如果您有多个要运行的语句,可以使用exec逐个运行。请注意,它不会在每个地方都起作用;它在SQLPlus、SQL Developer和可能一些其他工具中起作用。但是,由于它对于BEGIN-END块来说很短*,您可以更好地:

SQL> begin
  2    dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl (id number)');
  3    dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl2 (id number)');
  4  end;
  5  /

PL/SQL procedure successfully completed.

SQL>
英文:

If you managed to create a database link, you obviously have that user's credentials so the simplest option is to actually connect as remote user and create table in that schema directly.

If you want to do it from your schema, you can't do it using that syntax, but - there's a workaround: dbms_utility.exec_ddl_statement

Here's an example.

Database link first:

SQL> create database link dbl_mike connect to mike identified by lion using '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = my_server)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XEPDB1)))';

Database link created.

Does it work? Yes!

SQL> select * from dual@dbl_mike;

D
-
X

This is what you tried, and found out that it won't work:

SQL> create table test_dbl@dbl_mike (id number);
create table test_dbl@dbl_mike (id number)
                     *
ERROR at line 1:
ORA-02021: DDL operations are not allowed on a remote database

Workaround I mentioned:

SQL> exec dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl (id number)');

PL/SQL procedure successfully completed.

Testing (if table exists, I can insert a row over database link and select from that table):

SQL> insert into test_dbl@dbl_mike (id) values (100);

1 row created.

SQL> select * from test_dbl@dbl_mike;

        ID
----------
       100

SQL>

[EDIT]

If you have several statements to run, you can do it one-by-one using exec. Note that it won't work everywhere; it works in SQL*Plus, SQL Developer and perhaps some other tools. But, as it is short for a BEGIN-END block, you could rather/better

SQL> begin
  2    dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl (id number)');
  3    dbms_utility.exec_ddl_statement@dbl_mike('create table test_dbl2 (id number)');
  4  end;
  5  /

PL/SQL procedure successfully completed.

SQL>

huangapple
  • 本文由 发表于 2023年6月8日 01:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定