2012-09-05 3 views
2

HSQL documentation에 따르면 SQL 프로 시저에만 CALL 구문이 필요합니다. SQL 함수를 작성 중이지만, SELECT을 사용할 수 없습니다. 나는 단지 CALL 일 수있다. 아무도 내가 놓친 것을 볼 수 있습니까?왜 CALL이 아닌 SELECT를 사용하여 HSQL 함수를 호출 할 수 있습니까?

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

답변

2

이 함수에 의해 반환되는 테이블을 선택하는 올바른 구문은 다음과 같습니다 : : 여기에 내 코드

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(); 
    } 
} 

입니다 I 출력 얻을 것 같다

SELECT * FROM TABLE(F())