2

I'm new to pgsql. I just want to use a table name as a function parameter in pgsql.

CREATE OR REPLACE FUNCTION user_test_table_name_as_input (table_name text)
RETURNS VOID 
LANGUAGE plpgsql
AS $$
DECLARE
    i int;
BEGIN
    FOR i IN EXECUTE 'SELECT DISTINCT category FROM' || quote_ident(table_name)
    LOOP
    RAISE NOTICE '%', i;
    END LOOP;
END
$$

When I try to use this function...

SELECT user_test_table_name_as_input (table_name);

...i get this error:

ERROR: column "table_name" does not exist
SQL state: 42703

I read the related threads, like Table name as a PostgreSQL function parameter, but the other proposed solutions (Concatenation, format() ), do not work for me, neither. Any idea?

Community
  • 1
  • 1
dijea
  • 23
  • 4

1 Answers1

0

'table_name' needs quotes as the argument to the call of your stored procedure - it's text.

David Thomas
  • 778
  • 5
  • 10