Nazwa tabeli jako parametr funkcji PostgreSQL
Chcę przekazać nazwę tabeli jako parametr w funkcji Postgres. Próbowałem tego kodu:
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');
I mam to:
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 "."
A oto błąd, który dostałem po zmianie na toselect * 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...
Prawdopodobnie,quote_ident($1)
działa, bo bezwhere quote_ident($1).id=1
część dostaję1
, co oznacza, że coś jest wybrane. Dlaczego pierwszyquote_ident($1)
a drugi nie w tym samym czasie? A jak można to rozwiązać?