我如何从 plpgsql 中的存储过程中打印出多列
我正在尝试在存储过程中打印多列...任何人都可以为我提供一个使用查询的示例。谢谢。
例如,我有一个电影数据库,我想找到自 1960 年以来所有电影中盈利电影的百分比。我有执行此操作的查询,我在 pgADMIN 上运行它,它运行得很好;但是,当我尝试创建存储过程时,我知道我必须使用创建类型持有者作为(yr INTEGER,prnct FLOAT)。
因此,现在我需要创建一个存储过程,该存储过程将返回两列,其中一列是年份,另一列是百分比,但是我如何才能使这一列是年份,下一列是 prnct。
I am trying to print multiple columns in a stored procedure...can anyone please provide me with an example that uses a query. Thank you.
K for example, I have a movie database and I want to find the percentage of the profitable movies of all movies since the year 1960. I have the queires that do that and I ran it on pgADMIN and it works perfectly; however when i try creating a stored procedure, I know I have to use create a type holder as ( yr INTEGER, prnct FLOAT).
So now with that I need to create a stored procedure that would return the two columns one of the year and one of the percent, however how do i that this column is the yr and the next column is prnct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想返回具有多列的单行,则可以使用
record
或some_table
作为类型。如果你有一个像
movie
这样的表,那么你可以创建一个像这样的函数:如果你想返回某种任意类型,那么你必须这样做:
如果你想返回更多超过 1 行,您必须使用
SETOF
修饰符,如下所示:您可以创建一个如下所示的函数:
并从中选择结果,如下所示:
使用
setof
是相同的但当然是多行:)If you want to return a single row with multiple columns than you can use
record
orsome_table
as the type.If you have a table like
movie
than you can create a function like this:If you want to return some arbitrary type, than you'll have to do something like this:
And if you want to return more than 1 row, you have to use the
SETOF
modifier like this:You can create a function like this:
And select results from it like this:
With a
setof
it's the same but multiple rows ofcourse :)