【发布时间】:2020-05-08 01:43:19
【问题描述】:
我有这个函数,它接受一个 Varchar 作为输入,它抛出一个表作为输出。
create or replace function historial_reproductivo_vaca(hierro_v varchar) returns table(hierro_toro varchar, sexo varchar, fecha_nacimiento varchar, peso_nacimiento numeric, peso_destete numeric, clasificacion varchar, estado varchar)
as $$
declare
estado varchar;
begin
create temporary table temp_table AS
select hijos.hierro_padre as toro, hijos.sexo as s, hijos.fecha_nacimiento as nacimiento, hijos.hierro as hierro, pesos.peso_nacimiento, pesos.peso_12_meses, hijos.clasificacion FROM
((select animales.hierro, animales.sexo, animales.fecha_nacimiento, animales.hierro_madre, animales.hierro_padre, animales.clasificacion from animales)
union (select defuncion.hierro, defuncion.sexo, defuncion.fecha_nacimiento, defuncion.hierro_madre, defuncion.hierro_padre, defuncion.clasificacion from defuncion)
union (select venta_carne.hierro, venta_carne.sexo, venta_carne.fecha_nacimiento, venta_carne.hierro_madre, venta_carne.hierro_padre, venta_carne.clasificacion from venta_carne)
union (select venta_finca.hierro, venta_finca.sexo, venta_finca.fecha_nacimiento, venta_finca.hierro_madre, venta_finca.hierro_padre, venta_finca.clasificacion from venta_finca))as hijos
JOIN pesos ON pesos.hierro = hijos.hierro;
alter table temp_table add estado varchar;
--call update_temp_table(temp_table.hierro) from temp_table;
return QUERY SELECT * from temp_table;
end;
$$ language plpgsql;
但是没有问题,问题是当我执行Select historial_reproductivo_vaca('anything') 然后我得到这个消息:structure of query does not match function result type
我想知道是否有人可以帮助我。谢谢大家
【问题讨论】:
-
欢迎使用 stackoverflow。参观并获得您的第一个徽章-stackoverflow.com/tour
-
在 Postgres 中通常不需要在临时表中存储中间结果。无论您在“更新临时表”中做什么,都可以直接在 QUERY 本身中更有效地完成
-
与您的问题无关,但是:由于您的函数定义为
returns table,您应该像使用表格一样使用它并将其放入 FROM 子句中:select * from historial_reproductivo_vaca(..)
标签: postgresql plpgsql set-returning-functions