本文介绍了行的PL / pgSQL的阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是以下几种可能?结果
我想有/这需要作为参数类似行集,我的意思是我需要传递的功能类似于字典结构用PL过程pgSQL的:
伪code:
函数名({键:VAL1,KEY2:val2中,KEY3:VAL3 [,...]})
解决方案
与现代的PostgreSQL可以简化这样的功能。结果
测试设置:
CREATE TABLE TBL1(ID INT,价值文本);
没有必要在这种情况下,明确创建一个类型(如果该类型是基于表行),它是为每个表创建含蓄。结果
功能:
CREATE FUNCTION f_insert_rows_into_tbl1(TBL 1 [])
返回void AS
$ BODY $
INSERT INTO TBL1(ID,值)
选择一个)。*
FROM(SELECT UNNEST AS A($ 1))×;
$ BODY $语言SQL;
电话:
SELECT f_insert_rows_into_tbl1({(1,富),(2条)}');
请注意输入语法行的一组!
Is the following possible?
I want to have a procedure written in PL/pgSQL that takes as parameter something like "collection of row", I mean I need to pass to function a dictionary-like structure:
Pseudocode:
function_name({key1:val1,key2:val2, key3:val3 [, ...] })
解决方案
With modern day PostgreSQL you can simplify such a function.
Test setup:
CREATE TABLE tbl1 (id int, value text);
No need to create a type explicitly in this case (if the type is based on a table row), it is created for every table implicitly.
Function:
CREATE FUNCTION f_insert_rows_into_tbl1(tbl1[])
RETURNS VOID AS
$BODY$
INSERT INTO tbl1 (id,value)
SELECT (a).*
FROM (SELECT unnest($1) AS a) x;
$BODY$ LANGUAGE sql;
Call:
SELECT f_insert_rows_into_tbl1('{"(1,foo)","(2,bar)"}');
Note the input syntax for an array of rows!
这篇关于行的PL / pgSQL的阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!