I am not very much experienced in plpgsql so I am having 2 questions here.
- Is it possible in Postgres to use cursors the way we use in dynamic sql's for plsql? Basically I did not want to use procedure in plsql so I created a cursor which will hold the output of my dynamic query and then I use that value in a loop inside another query to get my output displayed on the screen. The same thing I am trying to with Postgres but not able to do so.
- Is it possible to in such situation to avoid creating a permanent function to accomplish this?
Here is my oracle script which runs fine:
DECLARE
CURSOR cur_tables IS
SELECT NAME,
'SELECT PROPERTY_VALUE FROM '
|| USERNAME
|| '.P_PROPERTY WHERE PROPERTY_NAME = ''VERSION'''
AS dsql
FROM CB_DATASOURCE
WHERE (UPPER(USERNAME) LIKE 'NAV_PS_%' or UPPER(USERNAME) LIKE 'CBPS_%' or UPPER(USERNAME) LIKE 'DEFAULTPS');
CURR_VERSION VARCHAR2(1000);
BEGIN
FOR r_tables IN cur_tables LOOP
begin
EXECUTE IMMEDIATE r_tables.dsql INTO CURR_VERSION;
DBMS_OUTPUT.put_line(r_tables.NAME || ': ' || CURR_VERSION);
exception
when others then
DBMS_OUTPUT.put_line(r_tables.NAME || ' no table');
end;
END LOOP;
END;
/
and here is my postgres function which I am not able to get it working but eventually would like to refrain from using a permanent function
create or replace function upgrade_version() returns setof record as $$
declare
r record;
loopy record;
isql text;
CURR_VERSION text;
begin
for r in SELECT 'SELECT PROPERTY_VALUE FROM '
|| USERNAME
|| '.P_PROPERTY WHERE PROPERTY_NAME = ''VERSION'''
AS dsql
FROM CB_DATASOURCE
WHERE (UPPER(USERNAME) LIKE 'NAV_PS_%' or UPPER(USERNAME) LIKE 'CBPS_%' or UPPER(USERNAME) LIKE 'DEFAULTPS') loop
isql := r.dsql;
EXECUTE isql INTO CURR_VERSION;
RETURN next loopy;
end loop;
return;
end;
$$ language 'plpgsql';
I would really appreciate any inputs on this.