Nome da tabela como um parâmetro de função do PostgreSQL
Eu quero passar um nome de tabela como um parâmetro em uma função Postgres. Eu tentei este código:
CREATE OR REPLACE FUNCTION some_f(param character varying) RETURNS integer
AS $
BEGIN
IF EXISTS (select * from quote_ident($1) where quote_ident($1).id=1) THEN
return 1;
END IF;
return 0;
END;
$ LANGUAGE plpgsql;
select some_f('table_name');
E eu tenho isso:
ERROR: syntax error at or near "."
LINE 4: ...elect * from quote_ident($1) where quote_ident($1).id=1)...
^
********** Error **********
ERROR: syntax error at or near "."
E aqui está o erro que recebi quando mudou para esteselect * from quote_ident($1) tab where tab.id=1
:
ERROR: column tab.id does not exist
LINE 1: ...T EXISTS (select * from quote_ident($1) tab where tab.id...
Provavelmente,quote_ident($1)
funciona, porque sem owhere quote_ident($1).id=1
parte eu recebo1
, o que significa que algo está selecionado. Por que a primeiraquote_ident($1)
trabalho e o segundo não ao mesmo tempo? E como isso poderia ser resolvido?