A preliminary study of PostgreSQL stored procedure

Source: Internet
Author: User
Tags volatile

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:

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:

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

    • This article is from: Linux Learning Tutorial Network

A preliminary study of PostgreSQL stored procedure

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.