PostgreSQL syntax error when using EXECUTE in Function
I think your problem is the language you're using. EXECUTE in the SQL language:
EXECUTE
is used to execute a previously prepared statement. Since prepared statements only exist for the duration of a session, the prepared statement must have been created by aPREPARE
statement executed earlier in the current session.
isn't the same as EXECUTE in PL/pgSQL:
Oftentimes you will want to generate dynamic commands inside your PL/pgSQL functions, that is, commands that will involve different tables or different data types each time they are executed. PL/pgSQL's normal attempts to cache plans for commands (as discussed in Section 39.10.2) will not work in such scenarios. To handle this sort of problem, the
EXECUTE
statement is provided:
EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ] ];
You're using the SQL EXECUTE (which executes a prepared statement) when you want to be using the PL/pgSQL EXECUTE (which executes a string as SQL).
Try this:
CREATE OR REPLACE FUNCTION example() RETURNS void AS $$BEGIN EXECUTE 'INSERT INTO table1 (col1, col2, col3) SELECT col1, col2, col3 from temp_table';END;$$ LANGUAGE PLPGSQL;
Or, another example that seems closer to what you seem to be trying to do:
create or replace function example(tname text) returns void as $$begin execute 'insert into ' || tname || ' (name) values(''pancakes'')';end;$$ language plpgsql;
That will insert 'pancakes'
into the table that you pass in the tname
argument to the function.
EXECUTE
is used to execute prepared statements and only expects a prepared statement name as argument.
If you are trying to execute an SQL statement (as in your example) simply include it in the body of the function.
Check the manual for more info about "Query Language (SQL) Functions".
OTOH if you are trying to create a PL/pgSQL function (which is not what you've shown in your question), then you need to convert your function to be a PL/pgSQL function.
It is a example tested by me where I use EXECUTE to run a select and put its result in a cursor.
1. Create the table:
create table people ( nickname varchar(9), name varchar(12), second_name varchar(12), country varchar(30) );
2. Create the function:
CREATE OR REPLACE FUNCTION fun_find_people (col_name text, col_value varchar)RETURNS void AS$BODY$DECLARE local_cursor_p refcursor; row_from_people RECORD;BEGIN open local_cursor_p FOR EXECUTE 'select * from people where '|| col_name || ' LIKE ''' || col_value || '%'' '; raise notice 'col_name: %',col_name; raise notice 'col_value: %',col_value; LOOP FETCH local_cursor_p INTO row_from_people; EXIT WHEN NOT FOUND; raise notice 'row_from_people.nickname: %', row_from_people.nickname ; raise notice 'row_from_people.name: %', row_from_people.name ; raise notice 'row_from_people.country: %', row_from_people.country; END LOOP;END;$BODY$ LANGUAGE 'plpgsql'
3. Run the functionselect fun_find_people('name', 'Cristian');select fun_find_people('country', 'Chile');