tech4science
Programmer
I want to write a loop using PL/pgSQL to perform an incrementing loop that will substitute the numbers in the query. PostGreSQL documentation shows writting PL/pgSQL code inside function definitions, but I have no need to create a function.
Can I write PL/pgSQL code directly alongside other queries?
If I have to create a function, how can I run a function by itself without placing it inside a query?
DECLARE
year INTEGER := 0;
BEGIN
LOOP
EXECUTE ''
EXECUTE ''
-- if source cost data is a comment or other invalid cost value, then copy data to destination comment field
UPDATE master
SET
fnote'' || year || '' = baseline.fyear'' || year || '',
fyear'' || year || '' = NULL
FROM baseline
WHERE
master.ckt_id = baseline.ckt_id AND
baseline.fyear'' || year || '' !~ ''''(^[0-9]+\.?[0-9]*$)|(^\.[0-9]+$)''''
'';
year := year + 1;
EXIT WHEN year = 10;
END LOOP;
END;
' LANGUAGE 'plpgsql';
Can I write PL/pgSQL code directly alongside other queries?
If I have to create a function, how can I run a function by itself without placing it inside a query?
DECLARE
year INTEGER := 0;
BEGIN
LOOP
EXECUTE ''
EXECUTE ''
-- if source cost data is a comment or other invalid cost value, then copy data to destination comment field
UPDATE master
SET
fnote'' || year || '' = baseline.fyear'' || year || '',
fyear'' || year || '' = NULL
FROM baseline
WHERE
master.ckt_id = baseline.ckt_id AND
baseline.fyear'' || year || '' !~ ''''(^[0-9]+\.?[0-9]*$)|(^\.[0-9]+$)''''
'';
year := year + 1;
EXIT WHEN year = 10;
END LOOP;
END;
' LANGUAGE 'plpgsql';