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