最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL function返回多行的操作

 更新時(shí)間:2020年12月30日 09:11:48   作者:賤一白  
這篇文章主要介紹了PostgreSQL function返回多行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

1. 建表

postgres=# create table tb1(id integer,name character varying);
CREATE TABLE
postgres=# 
postgres=# insert into tb1 select generate_series(1,5),'aa';
INSERT 0 5 

2. 返回單字段的多行(returns setof datatype)

不指定out參數(shù),使用return next xx:

create or replace function func01()returns setof character varying as $$
declare
n character varying;
begin
 for i in 1..5 loop
 select name into n from tb1 where id=i;
 return next n;
 end loop;
end
$$ language plpgsql;

指定out參數(shù),使用return next:

create or replace function func02(out character varying)returns setof character varying as $$
begin
 for i in 1..5 loop
 select name into $1from tb1 where id=i;
 return next;
 end loop;
end
$$ language plpgsql;

使用return query:

create or replace function func03()returns setof character varying as $$
begin
 for i in 1..5 loop
 return query(select name from tb1 where id=i);
 end loop;
end
$$language plpgsql;

3. 返回多列的多行(returns setog record)

不指定out參數(shù),使用return next xx:

create or replace function func04()RETURNS SETOF RECORD as $$
declare
 r record;
begin
 for i in 1..5 loop
 select * into r from tb1 where id=i;
 return next r;
 end loop;
end;
$$language plpgsql;

在使用func04的時(shí)候注意,碰到問(wèn)題列下:

問(wèn)題一:

postgres=# select func04();
ERROR: set-valued function called in context that cannot accept a set
CONTEXT: PL/pgSQL function func04() line 7 at RETURN NEXT

解決:

If you call your set-returning function the wrong way (IOW the way you might normally call a function), you will get this error message: Set-valued function called in context that cannot accept a set. Incorrect: select sr_func(arg1, arg2, …); Correct: select * from sr_func(arg1, arg2, …);

問(wèn)題二:

postgres=# select * from func04();
ERROR: a column definition list is required for functions returning "record"
LINE 1: select * from func04();

解決:

postgres=# select * from func04() as t(id integer,name character varying);
 id | name 
----+------
 1 | aa
 2 | aa
 3 | aa
 4 | aa
 5 | aa
(5 rows)

這個(gè)問(wèn)題在func04如果指定out參數(shù)就不會(huì)有問(wèn)題,如下func05所示:

指定out參數(shù),使用return next:

create or replace function func05(out out_id integer,out out_name character varying)returns setof record as $$
declare
 r record;
begin
 for i in 1..5 loop
 select * into r from tb1 where id=i;
 out_id:=r.id;
 out_name:=r.name;
 return next;
 end loop;
end;
$$language plpgsql;
postgres=# select * from func05();
 id | name 
----+------
 1 | aa
 2 | aa
 3 | aa
 4 | aa
 5 | aa
(5 rows)

使用return query:

create or replace function func06()returns setof record as $$
begin
 for i in 1..5 loop
 return query(select id,name from tb1 where id=i);
 end loop;
end;
$$language plpgsql;
postgres=# select * from func06() as t(id integer,name character varying);
 id | name 
----+------
 1 | aa
 2 | aa
 3 | aa
 4 | aa
 5 | aa
(5 rows)

補(bǔ)充:Postgresql - plpgsql - 從Function中查詢并返回多行結(jié)果

通過(guò)plpgsql查詢表,并返回多行的結(jié)果。

關(guān)于創(chuàng)建實(shí)驗(yàn)表插入數(shù)據(jù)這里就不說(shuō)啦

返回查詢結(jié)果

mytest=# create or replace function test_0830_5() returns setof test
mytest-# as $$
mytest$# DECLARE
mytest$# r test%rowtype; -- 將
mytest$# BEGIN
mytest$# FOR r IN
mytest$# SELECT * FROM test WHERE id > 0
mytest$# LOOP
mytest$# RETURN NEXT r;
mytest$# END LOOP;
mytest$# RETURN;
mytest$# END
mytest$# $$ language plpgsql;
CREATE FUNCTION
 
mytest=# select test_0830_5(1);
test_0830_5
------------------------------------------
(2,abcabc,"2018-08-30 09:26:14.392187")
......
(11,abcabc,"2018-08-30 09:26:14.392187")
(10 rows)
 
mytest=# select * from test_0830_5();
id | col1 | col2
----+--------+----------------------------
2 | abcabc | 2018-08-30 09:26:14.392187
......
11 | abcabc | 2018-08-30 09:26:14.392187
(10 rows)

返回某列

mytest=# CREATE OR REPLACE FUNCTION test_0830_6(date) RETURNS SETOF integer AS $$
mytest$# BEGIN
mytest$# RETURN QUERY SELECT id
mytest$# FROM test
mytest$# WHERE col2 >= $1
mytest$# AND col2 < ($1 + 1);
mytest$# IF NOT FOUND THEN
mytest$# RAISE EXCEPTION 'No id at %.', $1;
mytest$# END IF;
mytest$# RETURN;
mytest$# END
mytest$# $$
mytest-# LANGUAGE plpgsql;
CREATE FUNCTION
mytest=# select test_0830_6('2018-08-30');
test_0830_6
-------------
2
......
11
(10 rows)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 基于PostgreSQL和mysql數(shù)據(jù)類型對(duì)比兼容

    基于PostgreSQL和mysql數(shù)據(jù)類型對(duì)比兼容

    這篇文章主要介紹了基于PostgreSQL和mysql數(shù)據(jù)類型對(duì)比兼容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法

    Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法

    Postgresql里面內(nèi)置了很多的實(shí)用函數(shù),下面這篇文章主要給大家介紹了關(guān)于Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • postgreSQL 非count方法算記錄數(shù)操作

    postgreSQL 非count方法算記錄數(shù)操作

    這篇文章主要介紹了postgreSQL 非count方法算記錄數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • postgres 實(shí)現(xiàn)查詢某條數(shù)據(jù)的排名

    postgres 實(shí)現(xiàn)查詢某條數(shù)據(jù)的排名

    這篇文章主要介紹了postgres 實(shí)現(xiàn)查詢某條數(shù)據(jù)的排名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • PostgreSQL長(zhǎng)事務(wù)概念解析

    PostgreSQL長(zhǎng)事務(wù)概念解析

    pg中的長(zhǎng)事務(wù)會(huì)影響表中垃圾回收,導(dǎo)致表的年齡增長(zhǎng)無(wú)法freeze。能消耗事務(wù)的只有當(dāng)執(zhí)行了一些DML或者DDL操作后才能算是我們通常說(shuō)的長(zhǎng)事務(wù)。否則只能算是我們常說(shuō)的長(zhǎng)連接,當(dāng)然長(zhǎng)連接也有很多弊端,例如占用內(nèi)存、cpu等資源
    2022-09-09
  • postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程

    postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程

    PostgreSQL是一種開(kāi)源的關(guān)系型數(shù)據(jù)庫(kù),它提供了多種管理工具來(lái)操作數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent)

    PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent)

    這篇文章主要介紹了PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • GP如何查詢并刪除重復(fù)數(shù)據(jù)

    GP如何查詢并刪除重復(fù)數(shù)據(jù)

    這篇文章主要介紹了GP如何查詢并刪除重復(fù)數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • PostgreSQL 默認(rèn)權(quán)限查看方式

    PostgreSQL 默認(rèn)權(quán)限查看方式

    這篇文章主要介紹了PostgreSQL 默認(rèn)權(quán)限查看方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)

    PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)

    這篇文章主要介紹了PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10

最新評(píng)論

桂东县| 黑水县| 崇州市| 肇庆市| 垣曲县| 建瓯市| 城固县| 鄂伦春自治旗| SHOW| 舒兰市| 鹤峰县| 平安县| 绩溪县| 潮州市| 濮阳市| 封丘县| 龙游县| 成都市| 孟津县| 翁牛特旗| 德州市| 定安县| 左贡县| 麻阳| 桑日县| 灌阳县| 南川市| 丹棱县| 顺昌县| 察哈| 绵阳市| 敖汉旗| 西藏| 苗栗市| 茶陵县| 牡丹江市| 金山区| 渝北区| 涞水县| 周口市| 麻栗坡县|