VARIADIC

VARIADIC in SQL is a special parameter type that can accept an undefined number of arguments in a SQL function. It allows arguments of the same data type to be passed in arbitrary quantity when calling the function. This feature contributes to the flexibility and dynamic nature of SQL programming for managing databases.

Example

CREATE OR REPLACE FUNCTION total_sum(VARIADIC arr integer[]) RETURNS integer AS $$
BEGIN
RETURN array_sum(arr);
END; $$ LANGUAGE plpgsql;
SELECT total_sum(10, 20, 30, 40);

Output

100

Explanation

The function total_sum is declared with the VARIADIC keyword, which allows it to take an unspecified number of arguments. In the SELECT statement, four integers are provided as arguments. The result is the sum of these four integers, which is ‘100’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.