クエリ5
String query5 ="USE DBTwo\n" +
"DECLARE @temp_table table (column1 VARCHAR(60))\n" +
"insert into @temp_table (column1)\n" +
"select column1 from real_table (nolock)";
クエリ3
String query3 = "USE DBTwo\n" +
"select column1 from @temp_table";
接続;
ResultSet rs1;
Statement stmt;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.131.10;" +"databaseName=DBOne;" +"user=" + "exuser" + ";" + "password="+"userpass" + ";" + "allowMultiQueries=true" + ";";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
結果セットを取得しています。
try {
int rowcount = stmt.executeUpdate(query5);
System.out.println(rowcount);
rs1 = stmt.executeQuery(query3);
while (rs1.next()) {
System.out.println(rs1.getString(1));
}
rs1.close();
}
catch (SQLException sqlex){
sqlex.printStackTrace();
}
エラーの下で SQL 例外がスローされます。
com.microsoft.sqlserver.jdbc.SQLServerException: Must declare the table variable "@temp_table".`rowcount` return 7
だから私は正常に を埋めました@temp_table
、私は接続を閉じず、stmt
これから結果セットを取得しようとし@temp_table
ます。しかし、SQL は、このテーブルをまだ宣言していないと言っています。どのように可能ですか?
-- 解決済み --
クエリを 1 つだけ作成します。
String query5 ="USE DBTwo\n" +
"DECLARE @temp_table table (column1 VARCHAR(60))\n" +
"insert into @temp_table (column1)\n" +
"select column1 from real_table(nolock)\n" +
"select column1 from @temp_table";
以下のように複数の結果を取得します。
try {
boolean result = stmt.execute(query5);
while (true)
if(result){
rs1 = stmt.getResultSet();
while (rs1.next()) {
System.out.println(rs1.getString(1));
}
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1){
break;
}
result = stmt.getMoreResults();
}
catch (SQLException sqlex){
sqlex.printStackTrace();
}