RE: for loop

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: Igor Neyman <ineyman(at)perceptron(dot)com>, Pepe TD Vo <pepevo(at)yahoo(dot)com>, Pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: RE: for loop
Date: 2018-11-07 19:31:10
Message-ID: DM5PR17MB15326F3D106F39140A34D844DAC40@DM5PR17MB1532.namprd17.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin


From: Igor Neyman [mailto:ineyman(at)perceptron(dot)com]
Sent: Wednesday, November 07, 2018 1:47 PM
To: Pepe TD Vo <pepevo(at)yahoo(dot)com>; Pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: RE: for loop

WARNING: This email originated from outside of Perceptron! Please be mindful of PHISHING and MALWARE risks.
From: Pepe TD Vo [mailto:pepevo(at)yahoo(dot)com]
Sent: Wednesday, November 07, 2018 1:34 PM
To: Pgsql-admin <pgsql-admin(at)postgresql(dot)org<mailto:pgsql-admin(at)postgresql(dot)org>>
Subject: for loop

I have a script migrating from oracle to Postgres. I have checked online and don't see anything wrong on for ... loop statement. Would you please tell me what is wrong where?

CREATE OR REPLACE FUNCTION "CIDRDBA"."CIDRDBA_CONSTRAINTS" ( val in varchar(4000) ) RETURNS VOID
as $$
begin
if val = 'DISABLE' then
raise notice '%', 'CIDRDBA Constarints are being disabled';
for c in ( select table_name, constraint_name from information_schema.table_constraints
where constraint_type = 'R' )
loop
raise notice '%', 'Processing table: ' || c.table_name || ' - constraint
' || c.constraint_name;
execute immediate 'alter table ' || c.table_name ||' ' || val || ' constraint
' ||
c.constraint_name ;
end loop;
for c in ( select table_name, constraint_name from information_schema.table_constraints
where constraint_type = 'P' )
loop
raise notice '%', 'Processing table: ' || c.table_name || ' - constraint
' || c.constraint_name;
execute immediate 'alter table ' || c.table_name || ' ' || val || ' constrain
t ' ||
c.constraint_name ;
end loop;
elsif val = 'ENABLE' then
raise notice '%', 'CIDRDBA Constarints are being enabled';
for c in ( select table_name, constraint_name from information_schema.table_constraints
where constraint_type = 'P' )
loop
raise notice '%', 'Processing table: ' || c.table_name || ' - constraint
' || c.constraint_name;
execute immediate 'alter table ' || c.table_name ||' ' || val || ' constraint
' ||
c.constraint_name ;
end loop;
for c in ( select table_name, constraint_name from information_schema.table_constraints
where constraint_type = 'R' )
loop
raise notice '%', 'Processing table: ' || c.table_name || ' - constraint
' || c.constraint_name;
execute immediate 'alter table ' || c.table_name || ' ' || val || ' constrain
t ' ||
c.constraint_name ;
end loop;
else
raise notice '%', 'CIDRDBA nothing to do';
end if;
end;
$$ LANGUAGE plpgsql;

ERROR: loop variable of loop over rows must be a record or row variable or list of scalar variables
LINE 6: for c in ( select table_name, constraint_nam...
^
SQL state: 42601
Character: 238

Bach-Nga

Exactly what it says in error message: you need to declare loop variable:

DECLARE c record;
BEGIN
………

Now, your next error will be about “execute immediate”. There is no such command in Postgres PlPgSQL. You just do “execute” for dynamic sql.

So, in short, you need to read Postgres docs to learn about the differences between Oracle’s PlSQL and Postgres PlPgSQL.

Regards,
Igor Neyman

Oops… Wrong about “execute immediate” – it is supported.
But, not wrong about reading documentation on PlPgSQL.

Igor N.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Ron 2018-11-07 22:22:19 Re: query can't merge into table of the other schema
Previous Message Igor Neyman 2018-11-07 18:46:51 RE: for loop