Home > Blockchain >  How to run a query retuned by SELECT statement in Oracle database
How to run a query retuned by SELECT statement in Oracle database

Time:07-02

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.

CodePudding user response:

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>

CodePudding user response:

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.

  • Related