Table name as a PostgreSQL function parameter

前端 未结 8 1891
悲&欢浪女
悲&欢浪女 2020-11-21 05:37

I want to pass a table name as a parameter in a Postgres function. I tried this code:

CREATE OR REPLACE FUNCTION some_f(param character varying) RETURNS inte         


        
8条回答
  •  独厮守ぢ
    2020-11-21 06:28

    The first doesn't actually "work" in the sense that you mean, it works only in so far as it does not generate an error.

    Try SELECT * FROM quote_ident('table_that_does_not_exist');, and you will see why your function returns 1: the select is returning a table with one column (named quote_ident) with one row (the variable $1 or in this particular case table_that_does_not_exist).

    What you want to do will require dynamic SQL, which is actually the place that the quote_* functions are meant to be used.

提交回复
热议问题