PL / pgSQL abort entry from (4.1) - function returns a common data types - a combination of data types

This paper consists @ Little Liu Xiansen original, please indicate the source.

PL / pgSQL series, small partners to facilitate learning.

PL / pgSQL from entry to discard (1) - Getting
PL / pgSQL from entry to discard (2) - variable definitions and data type
PL / pgSQL from entry to discard (3) - function
PL / pgSQL from entry to discard (4.1) - function returns a common data types - the combined data type
PL / pgSQL abort entry from (4.2) - function returns a common data types - the type of data line
PL / pgSQL abort entry from (4.3) - function returns a common data types - recording data type
PL / pgSQL abort entry from (4.4) - function returns a common data types - type copying data
PL / pgSQL abort entry from (5) - conditional statements and loop statements
PL / pgSQL from entry to discard (6) -cursor


Return to the basic data types, such as int, varchar, json, jsonbthese data types is not the function returns and the use of the process of what is special, but in the process of writing the function will return rows of demand, these articles describes how to return rowtype, record, 组合数据类型, 复制数据类型.

Composite data type

Data type is a combination of a plurality of types of data combined into a data type, then the function returns can be used, for example, a write function analog HTTP request return result, i.e., while the return status and body (status int, body varchar). Then you can use a combination of the data type returned classes, for example:

  • Create Composite Data Types
create type http_result as (
	http_status int,
	body        varchar
)
-- drop TYPE if EXISTS http_result 
  • Function Example
create or replace function http(param int) returns http_result as $$
	declare
		code   int;
		msg    varchar;
	begin
		if param < 100 then
			code = 200;
			msg = '请求成功';
			
		else
			code = 500;
			msg = '请求失败';
		end if;
		return (code, msg);
	end;
$$ language plpgsql;
  • Function runs
    Successful outcome
    Fail results

Since the return result of the function depends on the type of data, you want to remove the need to remove the pre-function data type

The next line describes the return data type

About the Author

What we need to introduce part or some of the ideas are welcome to contact me.

GISer
QQ: 1016817543
E-mail: [email protected]
GitHub: https://github.com/MrSmallLiu (welcome star)

Related Links

Here are some I participate in the development of libraries, welcome to Star, Issues, PR

Published 11 original articles · won praise 3 · Views 3574

Guess you like

Origin blog.csdn.net/qq_35241223/article/details/105054959