0
CREATE OR REPLACE FUNCTION test123(column_name VARCHAR(40)) RETURNS RECORD AS $$
DECLARE
rec RECORD ;
BEGIN
SELECT * INTO rec FROM my_table WHERE column_name=1 LIMIT 1;
RETURN rec;
END;
$$ LANGUAGE plpgsql;
如何將列名傳遞給函數,然後在查詢中使用該名稱?將列名傳遞給函數
上面的代碼不起作用:
error: operator does not exist: character varying = integer
您需要動態SQL的。有關示例,請參閱手冊:https://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN –