Sample Header Ad - 728x90

Loop through schemas and execute dml on the same table in each schema

0 votes
1 answer
244 views
I have a table "active_t" in a schema called "test", having the following structure:
instance      active
          IN          1
          SI          0
The ask is to loop through this table and for each active schema(i.e., having active=1), the code should query another table "company_t" having the following structure:
code    deldate
 1234     "some date"
 9876     null
 7321     null
 9097     "some date"
The code needs to retrieve those company codes from company_t which have deldate as null. Once picked up, the code should further run an insert statement and insert some data in the another table "rec_t" in the test schema. The basic code that I tried was this:
SET SERVEROUTPUT ON;
declare
v_sql varchar2(200);
v_cntry active_t.instancename%type;
cursor c1 is
select instancename, active from active_t
where active = '1';
begin
for rec in c1
loop
execute IMMEDIATE 'select code from '||rec.instancename||'.company_t where deldate is null' into v_sql;
dbms_output.put_line(v_sql);
end loop;
end;
/
But I get an error as below:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 10
ORA-06512: at line 10
01422. 00000 -  "exact fetch returns more than requested number of rows"
*Cause:    The number specified in exact fetch is less than the rows returned.
*Action:   Rewrite the query or change number of rows requested
Please help what needs to be fixed. If this simple code works then I will try to implement the insert statement. Thanks in advance.
Asked by Yuvraj (1 rep)
Sep 19, 2022, 05:07 AM
Last activity: May 30, 2025, 10:09 AM