简体   繁体   English

Postgresql存储过程中的结果集Llike Java

[英]Result set in postgresql stored procedure llike java

hi is there any way to do the following java code in postgresql stored procedure 嗨,有什么方法可以在PostgreSQL存储过程中执行以下Java代码

String sqlQuery = "SELECT uid, name, duration from EVENTS";

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
       String name = rs.getString(2);
     }

i need the same in the postgresql stored procedure? 我需要在postgresql存储过程中相同吗?

Per the PL/PgSQL documentation you use a LOOP over a query for that. 根据PL / PgSQL文档,您可以在查询中使用LOOP

DO
$$
DECLARE
    myresult record;
BEGIN
    FOR myresult IN SELECT uid, name, duration FROM events
    LOOP
        RAISE NOTICE 'Name is %',myresult.name;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

However, use of loops is very often a sign you're just not using SQL properly. 但是,使用循环通常是您没有正确使用SQL的信号。 It's highly likely that proper use of WITH queries (common table expressions) and a bit of thought will get you a pure SQL solution that'll generally perform a LOT better. 正确使用WITH查询(常见的表表达式)和一点点思考很可能会为您提供一个纯SQL解决方案,该解决方案通常可以更好地完成很多任务。

In future, please give your PostgreSQL version and explain a little about what you're actually trying to accomplish. 将来,请提供您的PostgreSQL版本,并解释一些您实际上要完成的工作。 The code you show is completely useless, it does nothing. 您显示的代码完全没有用,它什么也不做。 So it's obviously not what you really want to do, but you've told us nothing about that. 因此,这显然不是您真正想要做的,但是您什么也没有告诉我们。 Quite often the solution you think you need (looping, say) isn't the solution you really need. 通常,您认为您需要的解决方案(例如循环)并不是您真正需要的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM