PostgreSQL函数 [英] Postgresql functions

查看:122
本文介绍了PostgreSQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是提供给我的类型定义

These are the type definitions provided to me

create type IR as (pattern_number integer, uoc_number integer);

我当前的进度是:

create or replace function q1(pattern text, uoc_threshold integer)
  returns setof IR
  as $$
  BEGIN
    RETURN QUERY
    select count(code) from temp where code like $1;
    RETURN QUERY 
    select count(code) from temp where code like $1 and uoc > $2;

  END;
$$ language plpgsql;

我的输出需要这样:
查询:-

My output needs to be like this : Query:-

select * 
from q1('ECO%', 6);

pattern_number  |   uoc_number 
80              |         5      

我收到一条错误消息:


错误:查询的结构与函数结果类型不匹配

详细信息:返回的类型bigint与列1中的预期类型整数不匹配。


语境:返回查询中的PL / pgSQL函数q1(text,integer)第3行

ERROR: structure of query does not match function result type
DETAIL: Returned type bigint does not match expected type integer in column 1.
CONTEXT: PL/pgSQL function q1(text,integer) line 3 at RETURN QUERY

我该如何解决?

推荐答案

是您想要的吗?..

create or replace function q1(pattern text, uoc_threshold integer)
  returns setof IR
  as $$
  BEGIN
    RETURN QUERY
    select (select count(code) from temp where code like $1)::integer
    ,(
    select count(code) from temp where code like $1 and uoc > $2)::integer;

  END;
$$ language plpgsql;

这篇关于PostgreSQL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆