Tag Info

New answers tagged

1

If you don't always want to pass all parameters, create a function with parameter defaults. The basic, simple form would be an SQL function without dynamic SQL: CREATE OR REPLACE FUNCTION func( _a int = NULL , _b text = NULL , _c text = NULL) RETURNS TABLE(...) AS $func$ SELECT ... FROM ... WHERE (speed = $1 OR $1 IS NULL) AND ...


1

You can use Dynamic queries. Here is a page about it. Check out about 2/3rds of the way down after "Example 40-1. Quoting Values In Dynamic Queries". But like it states, you need to be VERY careful. Doing this type of SQL creation on the fly can be a huge hole for SQL injection. http://www.postgresql.org/docs/current/static/plpgsql-statements.html


2

Either way, that's totally possible, given that all your parameters are of the same data type. EXECUTE ... USING happily takes an array, which is treated as a single argument. Access elements with array subscripts. create or replace function test_function(_filter1 text = null , _filter2 text = null ...



Top 50 recent answers are included