如何运行Oracle数据库中的Select语句重新调整的查询

发布于 2025-02-12 14:50:48 字数 673 浏览 0 评论 0原文

我正在使用Oracle数据库,并且有此查询,该查询会

(SELECT 'ALTER TABLE drop CONSTRAINT '
           ||constraint_name
           ||' ;'
FROM   user_constraints
WHERE  constraint_type = 'R'
AND    table_name = 'PRINCIPALS_ROLES'
AND    r_constraint_name =
    (
           SELECT constraint_name
           FROM   user_constraints
           WHERE  table_name = 'PRINCIPALS'
           AND    constraint_type= 'P' ))

在运行此查询时生成 Alter Table 语句,我将获得以下输出。

ALTER TABLE drop CONSTRAINT PRINCIPAL_ID_FKEY ;

现在,我想在生成此查询的同一SQL语句中运行上述查询。我们该怎么做?我不想使用声明(> gt; gt; gt; gt;开始 - >结束。 我希望我的查询是一个可执行的查询。

I am using Oracle database and I have this query which generates Alter Table statement

(SELECT 'ALTER TABLE drop CONSTRAINT '
           ||constraint_name
           ||' ;'
FROM   user_constraints
WHERE  constraint_type = 'R'
AND    table_name = 'PRINCIPALS_ROLES'
AND    r_constraint_name =
    (
           SELECT constraint_name
           FROM   user_constraints
           WHERE  table_name = 'PRINCIPALS'
           AND    constraint_type= 'P' ))

When I run this query I get below output.

ALTER TABLE drop CONSTRAINT PRINCIPAL_ID_FKEY ;

Now I want to run the above query in the same sql statement that generated this query. How can we do this? I Dont want to use stored procedure like Declare --> Begin --> End.
I want my query to be a single executable query.

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

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

发布评论

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

评论(2

哆兒滾 2025-02-19 14:50:48

我不想使用像声明一样存储过程 - >开始 - >结尾。我希望我的查询是一个可执行的查询。

不幸的是,并不是所有的梦想成真。恐怕这是其中之一。因为,您描述的是一个经典的动态SQL示例,需要某种PL/SQL。示例显示了一个存储过程,因此您只使用一个语句才能运行 can

样品表:

SQL> create table principals (id number constraint pk_princ primary key);

Table created.

SQL> create table principals_roles
  2    (id         number constraint pk_princ_role primary key,
  3     id_master  number constraint fk_prole_princ references principals (id));

Table created.

虚拟程序;您可能想重复使用它,因此您至少将表名称作为参数传递。

SQL> create or replace procedure p_drop as
  2  begin
  3    for cur_r in (select
  4                    'ALTER TABLE ' || table_name || ' drop CONSTRAINT ' ||
  5                     constraint_name as command
  6                  from user_constraints
  7                  where constraint_type = 'R'
  8                    and table_name = 'PRINCIPALS_ROLES'
  9                    and r_constraint_name = (select constraint_name
 10                                             from user_constraints
 11                                             where table_name = 'PRINCIPALS'
 12                                               and constraint_type = 'P'
 13                                            )
 14                 ) loop
 15      dbms_output.put_line(cur_r.command);
 16      execute immediate cur_r.command;
 17    end loop;
 18  end;
 19  /

Procedure created.

测试:

SQL> set serveroutput on;
SQL> exec p_drop;
ALTER TABLE PRINCIPALS_ROLES drop CONSTRAINT FK_PROLE_PRINC

PL/SQL procedure successfully completed.

如果一切顺利,principals_roles现在只能具有其主要密钥约束:

SQL> select constraint_name from user_constraints where table_name = 'PRINCIPALS_ROLES';

CONSTRAINT_NAME
--------------------------------------------------------------------------------
PK_PRINC_ROLE

SQL>

I Dont want to use stored procedure like Declare --> Begin --> End. I want my query to be a single executable query.

Unfortunately, not all dreams come true; I'm afraid this is one of these. Because, what you described is a classic dynamic SQL example which requires PL/SQL of some kind; example shows a stored procedure so you kind of can run it using just a single statement.

Sample tables:

SQL> create table principals (id number constraint pk_princ primary key);

Table created.

SQL> create table principals_roles
  2    (id         number constraint pk_princ_role primary key,
  3     id_master  number constraint fk_prole_princ references principals (id));

Table created.

Dummy procedure; you'd probably want to reuse it, so you'd then pass at least table name as a parameter.

SQL> create or replace procedure p_drop as
  2  begin
  3    for cur_r in (select
  4                    'ALTER TABLE ' || table_name || ' drop CONSTRAINT ' ||
  5                     constraint_name as command
  6                  from user_constraints
  7                  where constraint_type = 'R'
  8                    and table_name = 'PRINCIPALS_ROLES'
  9                    and r_constraint_name = (select constraint_name
 10                                             from user_constraints
 11                                             where table_name = 'PRINCIPALS'
 12                                               and constraint_type = 'P'
 13                                            )
 14                 ) loop
 15      dbms_output.put_line(cur_r.command);
 16      execute immediate cur_r.command;
 17    end loop;
 18  end;
 19  /

Procedure created.

Testing:

SQL> set serveroutput on;
SQL> exec p_drop;
ALTER TABLE PRINCIPALS_ROLES drop CONSTRAINT FK_PROLE_PRINC

PL/SQL procedure successfully completed.

If everything went OK, principals_roles should now have only its primary key constraint:

SQL> select constraint_name from user_constraints where table_name = 'PRINCIPALS_ROLES';

CONSTRAINT_NAME
--------------------------------------------------------------------------------
PK_PRINC_ROLE

SQL>
清风挽心 2025-02-19 14:50:48

您有一个ddl并执行它将需要即时的动态执行,不能在单个语句中完成。您将至少需要2个语句,并且执行即时需要开始结束块。

You have a DDL and executing it will require dynamic execute immediate, which can't be done in a single statement. You will need at least 2 statements and the execute immediate requires a BEGIN END block.

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