What is stored procedure, Baidu Encyclopedia is so defined: the stored procedure (Stored Procedure) is in a large database system , a set of SQL statements in order to complete a specific function, stored in the database after the first compilation and re-call do 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.
The stored procedure syntax structure for PostgreSQL is as follows:
<pre name = "code" class = "sql" style = "color: rgb (51, 51, 51); line-height: 24px;"> <span style = "font-family: arial, Times New Roman, sans-serif ; "> CREATE OR REPLACE FUNCTION function name (parameter 1, [int4, integer_int4,…]) </ span>
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:
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;
<pre name="code" class="sql" style="color: rgb(51, 51, 51); line-height: 24px;"> sql := 'create table _' || id || '_employee(id int, name text)';
EXECUTE sql;
<pre name="code" class="sql" style="color: rgb(51, 51, 51); line-height: 24px;"> sql := 'create table _' || id || '_sale_bill(id int, name text)';
EXECUTE sql;
..... return num; END; $BODY $ LANGUAGE plpgsql VOLATILE
On the storage procedure of PostgreSQL