Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writting PL/pgSQL in the main body

Status
Not open for further replies.

tech4science

Programmer
Feb 27, 2003
2
US
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';
 
Hi tech4science,

You might take a look at the PostgreSQL cookbook pages. There are a number of functions that may give you some ideas. The PostgreSQL cookbook pages can be found at the following link:


Also, you might consider placing you incrementing date loop inside a front-end programming language like perl, php, python, C/C++, VBA, FoxPro, etc and execute it programmically from within an application.

LelandJ Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top