我如何从 plpgsql 中的存储过程中打印出多列

发布于 2024-11-29 06:44:00 字数 268 浏览 1 评论 0原文

我正在尝试在存储过程中打印多列...任何人都可以为我提供一个使用查询的示例。谢谢。

例如,我有一个电影数据库,我想找到自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-12-06 06:44:01

如果您想返回具有多列的单行,则可以使用 recordsome_table 作为类型。

如果你有一个像 movie 这样的表,那么你可以创建一个像这样的函数:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS movie AS

如果你想返回某种任意类型,那么你必须这样做:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS record AS

如果你想返回更多超过 1 行,您必须使用 SETOF 修饰符,如下所示:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS SETOF record AS

您可以创建一个如下所示的函数:

CREATE OR REPLACE FUNCTION multicolumn_thing() RETURNS record AS $
DECLARE
    r record;
BEGIN
    SELECT 1, 2, 3 INTO r;
    RETURN r;
END
$ LANGUAGE 'plpgsql';

并从中选择结果,如下所示:

SELECT
    columns.a,
    columns.b,
    columns.c
FROM multicolumn_thing() AS columns(a int, b int, c int);

使用 setof 是相同的但当然是多行:)

If you want to return a single row with multiple columns than you can use record or some_table as the type.

If you have a table like movie than you can create a function like this:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS movie AS

If you want to return some arbitrary type, than you'll have to do something like this:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS record AS

And if you want to return more than 1 row, you have to use the SETOF modifier like this:

CREATE OR REPLACE FUNCTION get_profitable_movie() RETURNS SETOF record AS

You can create a function like this:

CREATE OR REPLACE FUNCTION multicolumn_thing() RETURNS record AS $
DECLARE
    r record;
BEGIN
    SELECT 1, 2, 3 INTO r;
    RETURN r;
END
$ LANGUAGE 'plpgsql';

And select results from it like this:

SELECT
    columns.a,
    columns.b,
    columns.c
FROM multicolumn_thing() AS columns(a int, b int, c int);

With a setof it's the same but multiple rows ofcourse :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文