Can a parameter pass not one, but several values?
select myfunc((123,234,344), var) or condition?
select myfunc('where id = 123 and db = 2') Can a parameter pass not one, but several values?
select myfunc((123,234,344), var) or condition?
select myfunc('where id = 123 and db = 2') Same parameters? Use an array
CREATE FUNCTION my_method(INT[]) RETURNS VOID AS $$ ... END $$ LANGUAGE plpgsql; SELECT my_method(ARRAY[123,234,344]) You can also transfer a piece of sql as text, but because of this you will not be able to substitute the condition as a parameter, just build the entire query, parse and execute, which is slower than the parameter substitution in the ready query that is parsed at the creation stage.
CREATE OR REPLACE FUNCTION myfunc ( s_where text ) RETURNS integer AS $BODY$ DECLARE i_id integer; BEGIN EXECUTE 'SELECT id FROM tablename ' || s_where INTO i_id; RETURN i_id; -- or, RETURNS SETOF, RETURN QUERY EXECUTE ... END $BODY$ LANGUAGE plpgsql; But, again, it is better to think about how to do without it.
Source: https://ru.stackoverflow.com/questions/566795/
All Articles