2012-02-12 7 views
1

run pl/sqlスクリプトにNamedParameterJdbcTemplateを使用しています。 しかし、私はどのようにアウト変数(:id_out)の値を得ることができるのか分かりません。 ありがとうございます。私はあなたがそのようなことは、上記のように無名PL/SQLブロックとNamedParameterJdbcTemplate(またはJdbcTemplateの他のサブクラス)を使用することができます信じていませんSpring JDBCテンプレート。 pl/sqlスクリプトの結果変数を取得する方法

String script = "declare 
        begin 
        if myFunc(:id_in) is null then 
         :id_out := 0; 
        else 
         :id_out := 1; 
        end if; 
        end;"; 
Map<String,Object> bindVars = new HashMap<String, Object>(); 
bindVars.put(id_in,1); 
bindVars.put(id_out,2); 


jdbcTmpl.execute(script, bindVars, new PreparedStatementCallback<Object>() { 
    @Override public Object doInPreparedStatement(PreparedStatement cs) 
     throws SQLException, DataAccessException { 
     cs.execute(); 
     return null; 
       } 
     } 
     ); 
+0

あなたは何を望みますか?あなたのSQLはクエリではなく(何も返されません)、とにかく 'return null'をします......? –

+0

変数の値にアクセスする必要があります(たとえば 'id_out') – user1204818

答えて

2

。匿名のPL/SQLブロックをストアド・プロシージャまたはファンクションにラップする必要があります。

スプリングはデータベース間で移植可能であることを意図しています。私の知る限り、MySQLとSQL Serverのどちらも、Oracleの匿名PL/SQLブロックに類似した概念を持っていません(ただし、この時点で間違っていることがわかりました)。この機能はデータベース間で移植可能ではないため、Springは単なるOracle用にはサポートしていません。

+0

SQL ServerはJDBCを介して「ブロック」を実行できます。 [jOOQ](http:// www)のブロック例については、[このブログの投稿](http://blog.jooq.org/2014/06/23/sql-server-alter-table-set-default/)を参照してください。 .jooq.org)は、 'ALTER TABLE tab MODIFY col DEFAULT val'ステートメントをエミュレートするときに実行されます。 –

5

それは、以下の例のように、プレーンJdbcTemplateで行われますが、OUT値は「との匿名PLSQLブロックで指定されなければならないような方法に注意してくださいすることができます:= 『何か』;使用

Javaパラメータを?以下:文字列のid =「12345」と文字列fixSql =

declare 
     p_id VARCHAR2(20) := null; 
     p_status_message VARCHAR2(32767) := null; 
    begin 
     p_id := ?; 
     p_status_message := ' Everything is possible: ' || p_id; 
     ? := 'Return text.' || p_status_message; 
    end; 

注上記の2つの疑問符 - 最初は効果的にINパラメータと第二OUTパラメータであるこのコードは、それを呼び出します:

public class Foo extends JdbcDaoSupport { 
    ... 
    public String doAnonymousPlSql(final String id, String fixSql) throws CustomerFixException { 
     String resultValue = getJdbcTemplate().execute(new CallableStatementCreator() { 
      @Override 
      public CallableStatement createCallableStatement(Connection connection) throws SQLException { 
       CallableStatement sql = connection.prepareCall(fixSql); 
       sql.setString(1, id); 
       sql.registerOutParameter(2, Types.VARCHAR); 
       return sql; 
      } 
     } 
     , new CallableStatementCallback<String>() { 
      @Override 
      public String doInCallableStatement(CallableStatement callablestatement) throws SQLException, 
        DataAccessException { 
       callablestatement.executeUpdate(); 
       return (String) callablestatement.getObject(2); 
      } 
     }); 
+1

CallableStatementで名前付きパラメータを使用することもできます - cstmt.registerOutParameter(" id_out "、Types.NUMERIC); – INeedMySpace

0
List<SqlParameter> params = new LinkedList<SqlParameter>(); 
params.add(new SqlParameter(Types.NUMERIC)); 
params.add(new SqlParameter(Types.NUMERIC)); 
params.add(new SqlOutParameter("out_param_1", Types.NUMERIC)); 
params.add(new SqlOutParameter("out_param_2", Types.NUMERIC)); 
Map<String, Object> ret = simpleJdbcTemplate.getJdbcOperations().call(
new CallableStatementCreator() { 

    @Override 
    public CallableStatement createCallableStatement(Connection con) throws SQLException { 
     CallableStatement cs = con.prepareCall("DECLARE ..." + 
               "BEGIN ... " + 
               "delete from table where table_field_1 = ? and table_field_2 = ? " + 
               "returning out_param_1, out_param_2 into ?, ?; " + 
               "END;" 
     cs.setInt(1, first_in_param_value); 
     cs.setInt(2, second_in_param_value); 
     cs.registerOutParameter(3, Types.NUMERIC); 
     cs.registerOutParameter(4, Types.NUMERIC); 
     return cs; 
    } 
}, params); 

ret.get("out_param_1"); 
ret.get("out_param_1"); 
関連する問題