Postgre SQL   发布时间:2022-05-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了关于postgresql的returns table调用时的显示问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,查官网上的解释。




35.4.9. SQL Functions Returning TABLE

There is another way to declare a function as returning a set,which is to use the Syntax RETURNS table(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SetoF record (or SetoF a single output parameter's type,as appropriatE). This notation is specifIEd in recent versions of the sql standard,and thus may be more portable than using SetoF.

For example,the preceding sum-and-product example Could also be done this way:

create functION sum_n_product_with_tab (x int)
RETURNS table(sum int,product int) AS $$
    SELECT $1 + tab.y,$1 * tab.y FROM tab;
$$ LANGUAGE sql;

it is not allowed to use explicit OUT or INOUT parameters with the RETURNS table notation — you must put all the output columns in the table List.




写完function之后,一调用,发现显示如图

完全不是我想要的样子,我想要的是多行多列的。于是去百度使劲查,可是因为RETURNS table是新功能,百度搜不到。于是我去Google查,在stackoverflow里查到了一个类似的例子。http://stackoverflow.com/questions/18084936/pl-pgsql-functions-how-to-return-table-with-an-execute-statement


里面有一条回答:Are you calling function with SELECT my_function(123); or SELECT FROM my_function(123); ?


太妙了!我回去试了一下,第一次我调用的方式是:SELECT List_lasT_Bill('yhc95@outlook.com');这样返回的是单列模式,就是上图那样。

于是我换成以下调用方式:SELECT * from List_lasT_Bill('yhc95@outlook.com');成功了!!变成多行多列了!

我又试了如下调用方式,去掉*:SELECT from List_lasT_Bill('yhc95@outlook.com');只显示行数


随机又对表格“contract”进行了同样的实验——

SELEctfrom的时候,一样,只是返回行数,但是当直接SELEctcontract时,显示的是字段不存在。

对视图操作也一样如此:


说明RETURNS tableS胜似表格,却在直接SELEct时比表格多了一个功能——将多列压缩成一列输出。



当function返回的是table时,相当于重新创建了一个表格,此时就不能像以前调用function一样简单粗暴地直接使用SELEct+Function_name,而是要像调用表格中所有列一样,SELEct*fromFunction_name


特此铭记,折腾了我一个小时,找不出原因。





大佬总结

以上是大佬教程为你收集整理的关于postgresql的returns table调用时的显示问题全部内容,希望文章能够帮你解决关于postgresql的returns table调用时的显示问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:首先