First understanding of PostgreSQL stored procedures, postgresql stored procedures

Source: Internet
Author: User

First understanding of PostgreSQL stored procedures, postgresql stored procedures

What is a Stored Procedure, which is defined by Baidu Encyclopedia: A Stored Procedure (Stored Procedure) is a set of SQL statements in a large database system to complete specific functions, after the first compilation, the stored procedure does not need to be re-compiled. You can specify the name of the stored procedure and provide parameters (if the stored procedure has parameters) to execute it. Stored procedures are an important object in databases. Any well-designed database application should use stored procedures.

The syntax structure of Postgresql stored procedure is as follows:
Copy codeThe Code is as follows:
Create or replace function name (parameter 1, [integer int4, integer array _ int4,…])

RETURNS return value type

$ BODY $

DECLARE

Variable Declaration

BEGIN

Function body

END;

$ BODY $

LANGUAGE 'plpgsql' VOLATILE;

The following example calls a stored procedure to automatically create a series of tables:
Copy codeThe Code is as follows:
Create or replace function create_table_for_client (id int)

RETURNS integer

$ 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.