2016-11-29 9 views
0

パッケージを作成してからストアドプロシージャを作成しています。次のようにパッケージと手順は以下のとおりです。無効な識別子の固定エラー

create or replace package test_system is 
    type ref_cursor is ref cursor; 
procedure demo_show_students(p_gpa IN STUDENTS.gpa%TYPE,p_ref_cursor OUT ref_cursor); 
end test_system; 
/

create or replace package body test_system is 
procedure demo_show_students(p_gpa IN STUDENTS.gpa%TYPE,p_ref_cursor OUT ref_cursor) 
is 
v_sql_str varchar2(4000); 
begin 
v_sql_str:='select sid,firstname,lastname,status,gpa,email from students where gpa = p_gpa'; 
open p_ref_cursor for v_sql_str; 
end demo_show_students; 
end test_system; 

次の両方をリンクするためのJavaコードすなわち、PL/SQLコードですが、それは私に問題を与えている:どのように

import java.sql.*; 

import oracle.jdbc.OracleTypes; 

public class ShowStudents { 

public void showStudents() { 

    try { 

     // Connection to Oracle server 
     OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource(); 
     ds.setURL("jdbc:oracle:thin:@localhost:1521:xe"); 
     Connection conn = ds.getConnection("*****", "*****"); 

     String val = '4'+""; 
     CallableStatement cs = conn.prepareCall("{call   
     test_system.demo_show_students(?,?)}"); 
     cs.setString(1, val); 

     cs.registerOutParameter(2, OracleTypes.CURSOR); 
     cs.execute(); 
     ResultSet rs = (ResultSet)cs.getObject(2); 

     while (rs.next()) { 
      System.out.println(rs.getString(1) + "\t" + 
       rs.getString(2) + "\t" + rs.getString(3) + 
       rs.getString(4) + 
       "\t" + rs.getDouble(5) + "\t" + 
       rs.getString(6)); 
     } 


     cs.close(); 

    } catch (SQLException ex) { 
     System.out.println("\n*** SQLException caught ***\n"); 
     ex.printStackTrace(); 
    } catch (Exception e) { 
     System.out.println("\n*** other Exception caught ***\n"); 
    } 
} 
} 



Exception : 

*** SQLException caught *** 
ORA-00904: "P_GPA": invalid identifier 
ORA-06512: at "PROJECT2.TEST_SYSTEM", line 7 
ORA-06512: at line 1 

誰も教えてもらえますこのエラーを修正してください。

答えて

1

オラクルは、可変性を識別することができませんp_gpa。あなたは、2通りの方法でそれを行うことができます:興のソリューションの@Akshayどちらが正しいですが、好ましい解決策

open p_ref_cursor for 
select sid,firstname,lastname,status,gpa,email 
from students where gpa = p_gpa; 
+1

:直接書き込み)プレースホルダ

v_sql_str:='select sid,firstname,lastname,status,gpa,email from students where gpa = :p_gpa'; open p_ref_cursor for v_sql_str using p_gpa; 

2を置く

1)として照会a)動的SQLを使用する必要はなく、b)動的SQLはコンパイル時ではなく実行時にしか解析できないため、静的SQLバージョン(2番目のソリューション)です。つまり、SQL文でエラーが発生した場合、実際にSQL文を実行するまでは、そのことを知ることはできません。また、オブジェクトがドロップされた場合、パッケージは無効化されず、user_dependenciesなどにも表示されません。可能であれば静的SQLに固執し、必要があれば動的SQLに戻ってください。 – Boneist

関連する問題