Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXECUTE...USING statement in PL/pgSQL doesn't work with record type?

I'm trying to write a function in PL/PgSQL that have to work with a table it receives as a parameter.

I use EXECUTE..INTO..USING statements within the function definition to build dynamic queries (it's the only way I know to do this) but ... I encountered a problem with RECORD data types.

Let's consider the follow (extremely simplified) example.

 -- A table with some values.
 DROP TABLE IF EXISTS table1;
 CREATE TABLE table1 (
     code INT,
     descr TEXT
 );

INSERT INTO table1 VALUES ('1','a');
INSERT INTO table1 VALUES ('2','b');


-- The function code. 
DROP FUNCTION IF EXISTS foo (TEXT);
CREATE FUNCTION foo (tbl_name TEXT) RETURNS VOID AS $$
DECLARE 
    r RECORD;
    d TEXT;
BEGIN
    FOR r IN
    EXECUTE 'SELECT * FROM ' || tbl_name
    LOOP
    --SELECT r.descr INTO d; --IT WORK
    EXECUTE 'SELECT ($1)' || '.descr' INTO d USING r; --IT DOES NOT WORK
    RAISE NOTICE '%', d;
END LOOP;

END;
$$ LANGUAGE plpgsql STRICT;

-- Call foo function on table1
SELECT foo('table1');

It output the following error:

ERROR: could not identify column "descr" in record data type

although the syntax I used seems valid to me. I can't use the static select (commented in the example) because I want to dinamically refer the columns names.

So..someone know what's wrong with the above code?

like image 813
Hobbes Avatar asked Jan 20 '10 20:01

Hobbes


People also ask

Which function is used to execute query in PostgreSQL?

To do this in PL/pgSQL, use the PERFORM statement: PERFORM query ; This executes query and discards the result. Write the query the same way you would write an SQL SELECT command, but replace the initial keyword SELECT with PERFORM .

How do I record type in PostgreSQL?

We can declare a record type variable by simply using a variable name followed by the record keyword. Syntax: variable_name record; We can use the dot notation (.) to access any field from the record type variable.

Can we run PL SQL in PostgreSQL?

PL/pgSQL comes with PostgreSQL by default. The user-defined functions and stored procedures developed in PL/pgSQL can be used like any built-in functions and stored procedures. PL/pgSQL inherits all user-defined types, functions, and operators.

What is execute in PostgreSQL?

The PostgreSQL EXECUTE command prepares and runs commands dynamically. The EXECUTE command can also run DDL statements and retrieve data using SQL commands. Similar to SQL Server, you can use the PostgreSQL EXECUTE command with bind variables.


2 Answers

It's true. You cannot to use type record outside PL/pgSQL space.

RECORD value is valid only in plpgsql.

you can do

EXECUTE 'SELECT $1.descr' INTO d USING r::text::xx;
like image 86
Pavel Stehule Avatar answered Oct 13 '22 17:10

Pavel Stehule


$1 should be inside the || ,like || $1 || and give spaces properly then it will work.

BEGIN

EXECUTE ' delete from  ' ||  quote_ident($1)  || ' where condition ';

END;
like image 2
Chinmay R Ullur Avatar answered Oct 13 '22 19:10

Chinmay R Ullur