do-conditional creation function in PostgreSQL

Source: Internet
Author: User
Tags postgresql

Lang_name  Code

do executes an anonymous block of code, in other words, an anonymous function that executes once in a program language.

A block of code is considered a function body with no parameters, and the return value type is void. Its parsing and execution occur at the same time.

The optional attribute LANGUAGE can be written before a block of code, or it can be written behind a block of code.


Code

The program language code can be executed. The programming language must be specified as a string, just like the command CREATE FUNCTION, which is recommended as a dollar sign.

Lang_name

The name of the program language used to parse the code, if default, the default language is plpgsql


Application Scenarios:

For example, if a function is created when a database satisfies certain conditions, for example, it must be UTF-8 encoded, then the function can be wrapped in code.

Cases

Do $DO $ DECLARE coding varchar; Beginselect getdatabaseencoding () into coding;if coding = ' UTF8 ' thenexecute ' CREATE OR REPLACE FUNCTION pg_catalog.length  B (character) RETURNS integer as $$    SELECT case octet_length ($) When     0 then NULL    ELSE octet_length ($) end;$$ LANGUAGE SQL immutable STRICT; COMMENT on FUNCTION PG_CATALOG.LENGTHB (character) is ' Returns the length of the specified string, using bytes instead of Characters ";"; END IF; end$do$;


do-conditional creation function in PostgreSQL

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.