【问题标题】:Why can I invoke an HSQL function with SELECT but not CALL?为什么我可以使用 SELECT 而不是 CALL 调用 HSQL 函数?
【发布时间】:2012-09-05 15:50:55
【问题描述】:

根据HSQL documentation,只有SQL 过程需要CALL 语法。我正在编写一个 SQL 函数,但我不能从中 SELECT。我只能CALL它。谁能看到我错过的东西?这是我的代码

public static void main(String[] args) throws Exception {
    Class<?> driverClass = Class.forName("org.hsqldb.jdbcDriver");
    JDBCDriver driver = (JDBCDriver) driverClass.newInstance();
    Properties props = new Properties();
    props.setProperty("user", "sa");
    props.setProperty("password", "");
    Connection c = driver.connect("jdbc:hsqldb:mem:aname", props);
    execute(c, "CREATE TABLE T (i INT)");
    execute(c, "INSERT INTO T VALUES (1)");
    execute(c, "CREATE FUNCTION f() RETURNS TABLE (i INT) READS SQL DATA " +
            " RETURN TABLE (SELECT * FROM T)");
    System.out.println("Call returns the ResultSet:");
    execute(c, "{ CALL f() }");
    try {
        execute(c, "SELECT * FROM f()");
    } catch (Exception e) {
        System.out.println("Select throws the exception:");
        System.out.println(e);
    }
}

private static void execute(Connection c, String sql) throws SQLException {
    Statement s = c.createStatement();
    try {
        s.execute(sql);
        ResultSet rs = s.getResultSet();
        if (rs != null) {
            printResultSet(rs);
        }
    } finally {
        s.close();
    }
}

private static void printResultSet(ResultSet rs) throws SQLException {
    try {
        while (rs.next()) {
            int columnCount = rs.getMetaData().getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                System.out.println(rs.getObject(i));
            }
        }
    } finally {
        rs.close();
    }
}

我得到了输出:

Call returns the ResultSet:
1
Select throws the exception:
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: F

【问题讨论】:

    标签: jdbc hsqldb sql-function


    【解决方案1】:

    选择该函数返回的表的正确语法如下:

    SELECT * FROM TABLE(F())
    

    【讨论】:

      【解决方案2】:

      好像还没有实现。

      目前 HSQLDB FUNCTION 不能有 out 参数。它可以返回一个 单值结果或表,由 execute() 调用返回。

      http://sourceforge.net/tracker/index.php?func=detail&aid=3530755&group_id=23316&atid=378134

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-28
        • 1970-01-01
        • 2016-04-16
        • 2018-07-09
        相关资源
        最近更新 更多