What is stored procedure, Baidu Encyclopedia is so defined: the stored procedure (Stored Procedure) is in the large database system, a set of SQL statements to complete a specific function, stored in the database after the first compilation after the second call does not need to compile again, The user executes it by specifying the name of the stored procedure and giving the parameter (if the stored procedure has parameters). Stored procedures are an important object in a database, and any well-designed database application should use stored procedures.
PostgreSQL's stored procedure syntax is structured as follows:
Copy Code code as follows:
CREATE OR REPLACE function name (parameter 1,[integral type int4, integer array _int4, ...])
RETURNS return value type as
$BODY $
DECLARE
Variable declaration
BEGIN
function body
End;
$BODY $
LANGUAGE ' Plpgsql ' VOLATILE;
The following example is to invoke a stored procedure to automatically create a corresponding series of tables:
Copy Code code as follows:
CREATE OR REPLACE FUNCTION create_table_for_client (id int)
RETURNS Integer AS
$BODY $
DECLARE
num INT4: = 0;
SQL "varchar";
BEGIN
sql: = ' CREATE table _ ' | | ID | | ' _company (id int, name text) ';
EXECUTE SQL;
sql: = ' CREATE table _ ' | | ID | | ' _employee (id int, name text) '; EXECUTE SQL;
sql: = ' CREATE table _ ' | | ID | | ' _sale_bill (id int, name text) '; EXECUTE SQL;
.......
return num;
End;
$BODY $ LANGUAGE Plpgsql VOLATILE