Table name as a PostgreSQL function parameter

前端 未结 8 1920
悲&欢浪女
悲&欢浪女 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:12

    If the question was to test if the table is empty or not (id=1), here is a simplified version of Erwin's stored proc :

    CREATE OR REPLACE FUNCTION isEmpty(tableName text, OUT zeroIfEmpty integer) AS
    $func$
    BEGIN
    EXECUTE format('SELECT COALESCE ((SELECT 1 FROM %s LIMIT 1),0)', tableName)
    INTO zeroIfEmpty;
    END
    $func$ LANGUAGE plpgsql;
    

提交回复
热议问题