![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I seem to have some troubles with the use of temporary tables inside function. This is a simplified version of my function. In reality it has several in parameters and returns a set of records being the records in the temp table. -CREATE OR REPLACE FUNCTION func_stock_forecast (...) AS- -$body$- -DECLARE- -my_rec record;- -all_rec refcursor;- -BEGIN- -CREATE TEMPORARY TABLE tbl_temp (field1...) ON COMMIT DROP;- -EXECUTE 'INSERT INTO tbl_temp (...) SELECT ...';- -OPEN all_rec FOR EXECUTE 'SELECT * FROM tbl_Prod';- -LOOP- -FETCH all_rec INTO my_rec;- -EXIT WHEN NOT FOUND;- -UPDATE tbl_temp SET ... WHERE field1 = my_rec.fieldx;- -END LOOP;- -CLOSE all_rec;- -END;- -$body$- -LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;- I use the function like this: SELECT * from func_stock_forecast(...) AS (...) If I call my function like this twice in a row, the first time gives me the expected result. The second time however results in an error message saying: -ERROR: relation with OID 52267707 does not exist- -CONTEXT: SQL statement "UPDATE tbl_temp SET ... WHERE ..."- -PL/pgSQL function "func_stock_forecast" line 11 at SQL statement- |
#3
| |||
| |||
|
| Originally Posted by Laurenz Albe The reason for this behaviour is that PL/pgSQL uses prepared SQL statements. That means that the second time an SQL statement is executed, it is not parsed and prepared again. |
| Originally Posted by Laurenz Albe So that's why it happens. But what can you do? First, you can try to avoid using a temporary table from PL/pgSQL. This is probably not what you want. |
| Originally Posted by Laurenz Albe Second, you can turn the offending SQL statement into dynamic SQL by using EXECUTE...You will notice that you already have a dynamic SQL statement in your function, this is the reason why there is no error in this line. |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
I keep having a problem with dynamic SQL commands. Just before the update command in my example I also need to do a select on my temporary table. When I try Code: -------------------- SELECT field1, field2 INTO v_f1, v_f2 FROM tbl_temp WHERE ...; -------------------- then I get a "relation with OID xxx does not exist" error. So, just like before I change it to dynamic SQL. According to the manual (http://tinyurl.com/nahkl) the syntax should be Code: -------------------- EXECUTE command-string [ INTO target ]; -------------------- So my statement becomes Code: -------------------- EXECUTE 'SELECT field1, field2 FROM tbl_temp WHERE field3 = ' || my_rec."StockID" || ' AND field4 IS NULL' INTO v_f1, v_f2; -------------------- This gives me a new error however Code: -------------------- ERROR: syntax error at or near "$2" at character 20 QUERY: SELECT $1 INTO $2 , $3 CONTEXT: PL/pgSQL function "func_stock_forecast" line 63 at execute statement -------------------- I suppose this has to do with the Code: -------------------- SELECT INTO is not currently supported within EXECUTE -------------------- remark in the PostgreSQL manual. What is the mentionned Code: -------------------- EXECUTE command-string [ INTO target ]; -------------------- good for then and most important how do I get around this limitation. |
#6
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |