Third, enter the above code int the query tool and click the Execute button to create the get_film_count function. Second, open the query tool by selecting Tools > Query Tool. To execute the create function statement, you can use any PostgreSQL client tool including psql and pgAdmin 1) Creating a function using pgAdminįirst, launch the pgAdmin tool and connect to the dvdrental sample database. At the end of the block, use the return statement to return the film_count.
Second, the get_film_count() function accepts two parameters len_from and len_to with the integer datatype.First, the name of the function is get_film_count that follows the create function keywords.The function get_film_count has two main sections: header and body. $$ Code language: PostgreSQL SQL dialect and PL/pgSQL ( pgsql ) Where length between len_from and len_to The following statement creates a function that counts the films whose length between the len_from and len_to parameters:Ĭreate function get_film_count(len_from int, len_to int) We’ll use the film table from the dvdrental sample database. PostgreSQL Create Function statement examples Store notes about the view in the Comments field. If applicable, select the name of the schema in which the view will reside from the drop-down listbox in the Schema field. Use the drop-down listbox next to Owner to select the role that will own the view. Finally, place a block in the dollar-quoted string constant. The name will be displayed in the pgAdmin tree control.Note that PostgreSQL supports many procedural languages, not just plpgsql. After that, use the language plpgsql to specify the procedural language of the function.Next, specify the datatype of the returned value after the returns keyword.A function can have zero or many parameters. Then, specify the function parameter list surrounded by parentheses after the function name.If you want to replace the existing function, you can use the or replace keywords. First, specify the name of the function after the create function keywords.$$ declare - variable declaration begin - logic end about the CREATE FUNCTION command, see the PostgreSQL core documentation available at. Create function function_name(param_list) Use the Name field to add a descriptive name for the function. SELECT show_cities_multiple2 ( 'ca_cur', 'tx_cur' ) ĬOMMIT Processing a Result Set from a. Procedure that accepts cursor names as parameters CREATE OR REPLACE FUNCTION show_cities_multiple2 (ref1 refcursor, ref2 refcursor ) RETURNS SETOF refcursor AS $$īEGIN OPEN ref1 FOR SELECT city, state FROM cities WHERE state = 'CA' - Open the first cursor RETURN NEXT ref1 - Return the cursor to the caller OPEN ref2 FOR SELECT city, state FROM cities WHERE state = 'TX' - Open the second cursor RETURN NEXT ref2 - Return the cursor to the caller END You can also redesign the function, and pass all cursor names as parameters to get predefined cursor names: So to fetch data, you can use a separate FETCH statements for each cursor. drop in the following function which should work for simple tables (assumes. If you call a procedure that returns multiple result sets in PSQL tool, pgAdmin Query tool or another function, the query returns cursor names: You can try to trace in the PostgreSQL log file what pgdump -table table.