/usr/share/doc/firebird/sql.extensions
----------- Subroutines ----------- Author: Adriano dos Santos Fernandes <adrianosf at gmail.com> Description: Support for PSQL subroutines (functions and procedures) inside functions, procedures, triggers and EXECUTE BLOCK. Subroutines are declared in the main routine and may be used from there or others subroutines. Syntax: <declaration item> ::= DECLARE [VARIABLE] <variable name> <data type> [ := <value> ]; | DECLARE [VARIABLE] CURSOR <cursor name> FOR (<query>); | <subroutine declaration> | <subroutine implementation> <subroutine declaration> ::= DECLARE FUNCTION <function name> [ (<input parameters>) ] RETURNS <data type> [ [ NOT ] DETERMINISTIC ] ; | DECLARE PROCEDURE <procedure name> [ (<input parameters>) ] [ RETURNS (<output parameters>) ] ; <subroutine implementation> ::= DECLARE FUNCTION <function name> [ (<input parameters>) ] RETURNS <data type> [ [ NOT ] DETERMINISTIC ] AS ... BEGIN ... END | DECLARE PROCEDURE <procedure name> [ (<input parameters>) ] [ RETURNS (<output parameters>) ] AS ... BEGIN ... END Limitations: 1) Subroutines may not be nested in another subroutine. They are only supported in the main routine. 2) Currently, a subroutine may not directly access or use variables or cursors of the main statements. This may be allowed in the future. Notes: 1) Starting in FB 4, subroutines may be recursive or call others subroutines. Examples: set term !; -- 1) Sub-procedures in execute block. execute block returns (name varchar(31)) as declare procedure get_tables returns (table_name varchar(31)) as begin for select rdb$relation_name from rdb$relations where rdb$view_blr is null into table_name do suspend; end declare procedure get_views returns (view_name varchar(31)) as begin for select rdb$relation_name from rdb$relations where rdb$view_blr is not null into view_name do suspend; end begin for select table_name from get_tables union all select view_name from get_views into name do suspend; end! -- 2) Sub-function in a stored function. create or alter function func1 (n1 integer, n2 integer) returns integer as declare function subfunc (n1 integer, n2 integer) returns integer as begin return n1 + n2; end begin return subfunc(n1, n2); end! select func1(5, 6) from rdb$database! -- 3) Recursive sub-function in EXECUTE BLOCK. execute block returns (i integer, o integer) as -- Recursive function without forward declaration. declare function fibonacci(n integer) returns integer as begin if (n = 0 or n = 1) then return n; else return fibonacci(n - 1) + fibonacci(n - 2); end begin i = 0; while (i < 10) do begin o = fibonacci(i); suspend; i = i + 1; end end! -- 4) Example with forward declaration and parameter with default values. execute block returns (o integer) as -- Forward declaration of P1. declare procedure p1(i integer = 1) returns (o integer); -- Forward declaration of P2. declare procedure p2(i integer) returns (o integer); -- Implementation of P1 should not re-declare parameter default value. declare procedure p1(i integer) returns (o integer) as begin execute procedure p2(i) returning_values o; end declare procedure p2(i integer) returns (o integer) as begin o = i; end begin execute procedure p1 returning_values o; suspend; end!
.
Edit
..
Edit
README.PSQL_stack_trace.txt
Edit
README.aggregate_filter.md
Edit
README.aggregate_tracking
Edit
README.alternate_string_quoting.txt
Edit
README.autonomous_transactions.txt
Edit
README.blob_append.md
Edit
README.builtin_functions.txt
Edit
README.case
Edit
README.coalesce
Edit
README.column_type_psql.txt
Edit
README.common_table_expressions
Edit
README.context_variables
Edit
README.context_variables2
Edit
README.cumulative_roles.txt
Edit
README.current_time
Edit
README.cursor_variables.txt
Edit
README.cursors
Edit
README.data_type_results_of_aggregations.txt
Edit
README.data_types
Edit
README.db_triggers.txt
Edit
README.ddl.txt
Edit
README.ddl_access.txt
Edit
README.ddl_triggers.txt
Edit
README.default_parameters
Edit
README.derived_tables.txt
Edit
README.distinct
Edit
README.domains_psql.txt
Edit
README.exception_handling
Edit
README.execute_block
Edit
README.execute_statement
Edit
README.execute_statement2
Edit
README.explicit_locks
Edit
README.expression_indices
Edit
README.external_connections_pool
Edit
README.floating_point_types.md
Edit
README.global_temporary_tables
Edit
README.hex_literals.txt
Edit
README.identity_columns.txt
Edit
README.iif
Edit
README.isc_info_xxx
Edit
README.joins.txt
Edit
README.keywords
Edit
README.leave_labels
Edit
README.length
Edit
README.linger
Edit
README.list
Edit
README.management_statements_psql.md
Edit
README.mapping.html
Edit
README.merge.txt
Edit
README.null_value
Edit
README.nullif
Edit
README.offset_fetch.txt
Edit
README.order_by_expressions_nulls
Edit
README.packages.txt
Edit
README.plan
Edit
README.regr_functions.txt
Edit
README.returning
Edit
README.rows
Edit
README.savepoints
Edit
README.scrollable_cursors.txt
Edit
README.select_expressions
Edit
README.sequence_generators
Edit
README.set_bind.md
Edit
README.set_role
Edit
README.set_transaction.txt
Edit
README.similar_to.txt
Edit
README.sql_security.txt
Edit
README.statistical_functions.txt
Edit
README.subroutines.txt
Edit
README.substring_similar.txt
Edit
README.time_zone.md
Edit
README.trim
Edit
README.universal_triggers
Edit
README.update_or_insert
Edit
README.user_management
Edit
README.view_updates
Edit
README.window_functions.md
Edit