我想在Postgres函数中传递一个表名作为参数.我试过这个密码:
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');
我得到了这个:
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 "."
下面是我改成select * 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...
quote_ident($1)
可能有效,因为没有where quote_ident($1).id=1
部分,我得到1
,这意味着有些东西被选中了.为什么前quote_ident($1)
个可以同时工作,而第二个不能同时工作?这怎么解决呢?