質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
SQL Server

SQL Serverはマイクロソフトのリレーショナルデータベース管理システムです。データマイニングや多次元解析など、ビジネスインテリジェンスのための機能が備わっています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

3回答

7928閲覧

ストアドプロシージャから結果を取得したい

satouyuuya

総合スコア15

SQL Server

SQL Serverはマイクロソフトのリレーショナルデータベース管理システムです。データマイニングや多次元解析など、ビジネスインテリジェンスのための機能が備わっています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/11/22 07:03

編集2018/11/26 03:43

前提・実現したいこと

Javaでシステム開発をしているのですが、SQLServerを使用しデータを取り出したいです。
いま問題となっているのはストアドプロシージャでの結果取得についてです。

私自身前任者から引き継いだばかりのプログラム初心者な上SQLのことはわかりませんので、
本当に申し訳ないですが状況説明すら間違えてる可能性があります。

やりたいことは、
SQLに名簿があり
ストアドプロシージャを実行すると条件にあった人物が表示されるというものです。

発生している問題・エラーメッセージ

SQLManagementStudio上でストアドプロシージャ実行し、条件にあった検索結果は出て、returnVolumeが0となっています。
上司はSQLは出来るのですがJavaは出来きず、私が未熟なので結果テーブルが取り出せません。

ストアド実行画面

該当のソースコード

SQL側

1USE [Dcat] 2GO 3/****** Object: StoredProcedure [dbo].[getExamineeRs] Script Date: 2018/11/22 15:30:16 ******/ 4SET ANSI_NULLS ON 5GO 6SET QUOTED_IDENTIFIER ON 7GO 8 9-- ============================================= 10-- Author: <Author,,Name> 11-- Create date: <Create Date,,> 12-- Description: <Description,,> 13-- ============================================= 14ALTER PROCEDURE [dbo].[getExamineeRs] 15 -- Add the parameters for the stored procedure here 16 @p_name as nvarchar(50) = null, 17 @p_kananame as nvarchar(50) = null, 18 @p_birth as date = null 19 20AS 21BEGIN 22 -- SET NOCOUNT ON added to prevent extra result sets from 23 -- interfering with SELECT statements. 24 SET NOCOUNT ON; 25 26 -- Insert statements for procedure here 27 28 declare @return int 29 set @return = 0 30 31 select @p_name = ltrim(rtrim(@p_name)) 32 33 if (@p_name is null) and (@p_kananame is null) and (@p_birth is null) begin 34 --SELECT 35 -- [ExamineeId] 36 -- ,[ImportGroupId] 37 -- ,[ResidentNumber] 38 -- ,[ApoDate] 39 -- ,[TestDate] 40 -- ,[Name] 41 -- ,[KanaName] 42 -- ,[Gender] 43 -- ,[Birth] 44 -- ,[Age] 45 -- ,[RankId] 46 -- ,[LastTestId] 47 -- ,[Status] 48 -- ,[DeleteFlag] 49 -- ,[CreateDate] 50 -- ,[CreateUser] 51 -- ,[UpdateDate] 52 -- ,[UpdateUser] 53 -- FROM [dbo].[MExaminee] 54 55 set @return = 200 56 57 end else begin 58 if (@p_name is not null) and (@p_kananame is not null) and (@p_birth is not null) begin 59 select 60 [ExamineeId] 61 ,[ImportGroupId] 62 ,[ResidentNumber] 63 ,[ApoDate] 64 ,[TestDate] 65 ,[Name] = LTRIM(RTRIM([Name])) 66 ,[KanaName] = LTRIM(RTRIM([KanaName])) 67 ,[Gender] 68 ,[Birth] 69 ,[Age] 70 ,[RankId] 71 ,[LastTestId] 72 ,[Status] 73 ,[DeleteFlag] 74 ,[CreateDate] 75 ,[CreateUser] 76 ,[UpdateDate] 77 ,[UpdateUser] 78 FROM [dbo].[MExaminee] 79 where 80 [Name] = @p_name 81 and [KanaName] = @p_kananame 82 and [Birth] = @p_birth 83 end else begin 84 if (@p_name is not null) and (@p_kananame is not null) begin 85 select 86 [ExamineeId] 87 ,[ImportGroupId] 88 ,[ResidentNumber] 89 ,[ApoDate] 90 ,[TestDate] 91 ,[Name] = LTRIM(RTRIM([Name])) 92 ,[KanaName] = LTRIM(RTRIM([KanaName])) 93 ,[Gender] 94 ,[Birth] 95 ,[Age] 96 ,[RankId] 97 ,[LastTestId] 98 ,[Status] 99 ,[DeleteFlag] 100 ,[CreateDate] 101 ,[CreateUser] 102 ,[UpdateDate] 103 ,[UpdateUser] 104 FROM [dbo].[MExaminee] 105 where 106 [Name] = @p_name 107 and [KanaName] = @p_kananame 108 -- and [Birth] = @p_birth 109 end else begin 110 if (@p_name is not null) and (@p_birth is not null) begin 111 select 112 [ExamineeId] 113 ,[ImportGroupId] 114 ,[ResidentNumber] 115 ,[ApoDate] 116 ,[TestDate] 117 ,[Name] = LTRIM(RTRIM([Name])) 118 ,[KanaName] = LTRIM(RTRIM([KanaName])) 119 ,[Gender] 120 ,[Birth] 121 ,[Age] 122 ,[RankId] 123 ,[LastTestId] 124 ,[Status] 125 ,[DeleteFlag] 126 ,[CreateDate] 127 ,[CreateUser] 128 ,[UpdateDate] 129 ,[UpdateUser] 130 FROM [dbo].[MExaminee] 131 where 132 [Name] = @p_name 133 -- and [KanaName] = @p_kananame 134 and [Birth] = @p_birth 135 end else begin 136 if (@p_kananame is not null) and (@p_birth is not null) begin 137 select 138 [ExamineeId] 139 ,[ImportGroupId] 140 ,[ResidentNumber] 141 ,[ApoDate] 142 ,[TestDate] 143 ,[Name] = LTRIM(RTRIM([Name])) 144 ,[KanaName] = LTRIM(RTRIM([KanaName])) 145 ,[Gender] 146 ,[Birth] 147 ,[Age] 148 ,[RankId] 149 ,[LastTestId] 150 ,[Status] 151 ,[DeleteFlag] 152 ,[CreateDate] 153 ,[CreateUser] 154 ,[UpdateDate] 155 ,[UpdateUser] 156 FROM [dbo].[MExaminee] 157 where 158 -- [Name] = @p_name 159 [KanaName] = @p_kananame 160 and [Birth] = @p_birth 161 end else begin 162 if (@p_name is not null) begin 163 select 164 [ExamineeId] 165 ,[ImportGroupId] 166 ,[ResidentNumber] 167 ,[ApoDate] 168 ,[TestDate] 169 ,[Name] = LTRIM(RTRIM([Name])) 170 ,[KanaName] = LTRIM(RTRIM([KanaName])) 171 ,[Gender] 172 ,[Birth] 173 ,[Age] 174 ,[RankId] 175 ,[LastTestId] 176 ,[Status] 177 ,[DeleteFlag] 178 ,[CreateDate] 179 ,[CreateUser] 180 ,[UpdateDate] 181 ,[UpdateUser] 182 FROM [dbo].[MExaminee] 183 where 184 [Name] = @p_name 185 -- [KanaName] = @p_kananame 186 -- and [Birth] = @p_birth 187 end else begin 188 if (@p_kananame is not null) begin 189 select 190 [ExamineeId] 191 ,[ImportGroupId] 192 ,[ResidentNumber] 193 ,[ApoDate] 194 ,[TestDate] 195 ,[Name] = LTRIM(RTRIM([Name])) 196 ,[KanaName] = LTRIM(RTRIM([KanaName])) 197 ,[Gender] 198 ,[Birth] 199 ,[Age] 200 ,[RankId] 201 ,[LastTestId] 202 ,[Status] 203 ,[DeleteFlag] 204 ,[CreateDate] 205 ,[CreateUser] 206 ,[UpdateDate] 207 ,[UpdateUser] 208 FROM [dbo].[MExaminee] 209 where 210 -- [Name] = @p_name 211 [KanaName] = @p_kananame 212 -- and [Birth] = @p_birth 213 end else begin 214 if (@p_birth is not null) begin 215 select 216 [ExamineeId] 217 ,[ImportGroupId] 218 ,[ResidentNumber] 219 ,[ApoDate] 220 ,[TestDate] 221 ,[Name] = LTRIM(RTRIM([Name])) 222 ,[KanaName] = LTRIM(RTRIM([KanaName])) 223 ,[Gender] 224 ,[Birth] 225 ,[Age] 226 ,[RankId] 227 ,[LastTestId] 228 ,[Status] 229 ,[DeleteFlag] 230 ,[CreateDate] 231 ,[CreateUser] 232 ,[UpdateDate] 233 ,[UpdateUser] 234 FROM [dbo].[MExaminee] 235 where 236 -- [Name] = @p_name 237 -- [KanaName] = @p_kananame 238 [Birth] = @p_birth 239 end 240 end 241 end 242 end 243 end 244 end 245 end 246 end 247 248 return (@return) 249END 250 251

Java側

1 2 try { 3 ResultSet rs = Sql.exec(true, "getExamineeRs", params.name, params.kanaName, params.birth); 4 DataTable tbl = new DataTable(rs); 5 6// DataTable tbl = new DataTable("MExaminee"); 7 tbl.setLabelMap( 8 "Name", "名前", 9 "KanaName", "フリガナ", 10 "Birth", "生年月日", 11 "Gender", "性別", 12 "ExamineeId", "ID" 13 ); 14 tbl.addHtml(0, "<td><input type=\"button\" value=\"選択\" onclick=\"rowClick(this);\"></td>"); 15 out.write(tbl.getTable()); 16 17 } catch (SQLException e) { 18 throw new UserRuntimeException(e); 19 }

試したこと

上司は出来るということなので、
色々調べてたのですが、returnVolume以外を取り出す方法がわかりませんでした。

追記
ここで質問したあと3日ほど調べていたら上司の説明通り
「SELECT結果とReturn値の両方を返すストアドからの値取得」が出来るみたいなので頑張っていました。
http://new-pill.hatenablog.com/entry/2017/01/27/174113
しかし、いくらやってもやはり出てきませんでした。(自分が下手なせいかもしれませんが

補足情報(FW/ツールのバージョンなど)

Java1.8
SQLServer2017

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

下記のリンク先で説明されている通りoutパラメータを使用するとreturnとは別に検索結果を返すことができると思いますが、
まずは上司の方が『出来る』と言っている根拠がoutパラメータのことを指しているのか確認された方がいいと思います。

出力パラメーターがあるストアド プロシージャの使用

以上ご参考まで。

投稿2018/11/26 10:47

rubytomato

総合スコア1752

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

satouyuuya

2018/11/27 04:51

情報ありがとうございます。 上司曰く、そのoutパラメータのことらしいです。
guest

0

ベストアンサー

少なくとも、ストアドプロシージャが返している値が returnValue だけなので、検索結果は取得できません。

投稿2018/11/26 01:17

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

satouyuuya

2018/11/26 03:41

そういうアドバイス、ありがとうございます。 今本当に苦戦していて私は出来ないことを証明するのに時間を取られてます。 具体的に何故出来ないとかわかりますでしょうか? 私自身、ここで質問したあと3日ほど調べていたら上司の説明通り 「SELECT結果とReturn値の両方を返すストアドからの値取得」が出来るみたいなので今頑張っていました。 http://new-pill.hatenablog.com/entry/2017/01/27/174113 しかし、いくらやってもやはり出てきませんでした。(自分が下手なせいかもしれませんが
A-pZ

2018/11/26 07:26

この質問に書かれていないSqlクラス?の実装次第なところもありますが、CallableStatementの結果が得られれば検索結果が取得できるのかもしれません。
satouyuuya

2018/11/27 05:06

色々考えていただきありがとうございます。 私もその辺りを考えてやってみて出来なかったので上司とワイワイ言い合った結果全然違うストアドプロシージャを渡され解決?しました。(根本的に何が悪いのかわかりませんが無事プログラムは正常に出力だれました)
guest

0

解決方法・事故解決と言って良いのかわかりませんが、上司と言い争った結果ストアドプロシージャ自体を変更して問題なく動作しました。
不要かと思いますが、変更したストアドプロシージャを載せておきます。

結果画面

SQL

1 2USE [Dcat] 3GO 4/****** Object: StoredProcedure [dbo].[getExamineeRs] Script Date: 2018/11/27 14:09:23 ******/ 5SET ANSI_NULLS ON 6GO 7SET QUOTED_IDENTIFIER ON 8GO 9 10-- ============================================= 11-- Author: <Author,,Name> 12-- Create date: <Create Date,,> 13-- Description: <Description,,> 14-- ============================================= 15ALTER PROCEDURE [dbo].[getExamineeRs] 16 -- Add the parameters for the stored procedure here 17 @p_name as nvarchar(50) = null, 18 @p_kananame as nvarchar(50) = null, 19 @p_birth as date = null 20 21AS 22BEGIN 23 -- SET NOCOUNT ON added to prevent extra result sets from 24 -- interfering with SELECT statements. 25 SET NOCOUNT ON; 26 27 -- Insert statements for procedure here 28 29 declare @return int 30 set @return = 0 31 32 select @p_name = ltrim(rtrim(@p_name)) 33 select @p_kananame = ltrim(rtrim(@p_kananame)) 34 35 if len(@p_name) <= 2 begin set @p_name = Null end 36 if len(@p_kananame) < 1 begin set @p_kananame = Null end 37 if @p_birth <= '1900/1/1' begin set @p_birth = Null end 38 39 --select @p_name,@p_kananame,@p_birth 40 41 if (@p_name is null) and (@p_kananame is null) and (@p_birth is null) begin 42 --SELECT 43 -- [ExamineeId] 44 -- ,[ImportGroupId] 45 -- ,[ResidentNumber] 46 -- ,[ApoDate] 47 -- ,[TestDate] 48 -- ,[Name] 49 -- ,[KanaName] 50 -- ,[Gender] 51 -- ,[Birth] 52 -- ,[Age] 53 -- ,[RankId] 54 -- ,[LastTestId] 55 -- ,[Status] 56 -- ,[DeleteFlag] 57 -- ,[CreateDate] 58 -- ,[CreateUser] 59 -- ,[UpdateDate] 60 -- ,[UpdateUser] 61 -- FROM [dbo].[MExaminee] 62 63 set @return = 0 64 65 end else begin 66 if (@p_name is not null) and (@p_kananame is not null) and (@p_birth is not null) begin 67 select 68 [ExamineeId] 69 ,[ImportGroupId] 70 ,[ResidentNumber] 71 ,[ApoDate] 72 ,[TestDate] 73 ,[Name] = LTRIM(RTRIM([Name])) 74 ,[KanaName] = LTRIM(RTRIM([KanaName])) 75 ,[Gender] 76 ,[Birth] 77 ,[Age] 78 ,[RankId] 79 ,[RankName] -- ★ 80 ,[TestTime] -- ★ 81 ,[LastTestId] 82 ,[Status] 83 ,[DeleteFlag] 84 ,[CreateDate] 85 ,[CreateUser] 86 ,[UpdateDate] 87 ,[UpdateUser] 88 FROM [dbo].[MExaminee] 89 where 90 [Name] = @p_name 91 and [KanaName] = @p_kananame 92 and [Birth] = @p_birth 93 end else begin 94 if (@p_name is not null) and (@p_kananame is not null) begin 95 select 96 [ExamineeId] 97 ,[ImportGroupId] 98 ,[ResidentNumber] 99 ,[ApoDate] 100 ,[TestDate] 101 ,[Name] = LTRIM(RTRIM([Name])) 102 ,[KanaName] = LTRIM(RTRIM([KanaName])) 103 ,[Gender] 104 ,[Birth] 105 ,[Age] 106 ,[RankId] 107 ,[RankName] -- ★ 108 ,[TestTime] -- ★ 109 ,[LastTestId] 110 ,[Status] 111 ,[DeleteFlag] 112 ,[CreateDate] 113 ,[CreateUser] 114 ,[UpdateDate] 115 ,[UpdateUser] 116 FROM [dbo].[MExaminee] 117 where 118 [Name] = @p_name 119 and [KanaName] = @p_kananame 120 -- and [Birth] = @p_birth 121 end else begin 122 if (@p_name is not null) and (@p_birth is not null) begin 123 select 124 [ExamineeId] 125 ,[ImportGroupId] 126 ,[ResidentNumber] 127 ,[ApoDate] 128 ,[TestDate] 129 ,[Name] = LTRIM(RTRIM([Name])) 130 ,[KanaName] = LTRIM(RTRIM([KanaName])) 131 ,[Gender] 132 ,[Birth] 133 ,[Age] 134 ,[RankId] 135 ,[RankName] -- ★ 136 ,[TestTime] -- ★ 137 ,[LastTestId] 138 ,[Status] 139 ,[DeleteFlag] 140 ,[CreateDate] 141 ,[CreateUser] 142 ,[UpdateDate] 143 ,[UpdateUser] 144 FROM [dbo].[MExaminee] 145 where 146 [Name] = @p_name 147 -- and [KanaName] = @p_kananame 148 and [Birth] = @p_birth 149 end else begin 150 if (@p_kananame is not null) and (@p_birth is not null) begin 151 select 152 [ExamineeId] 153 ,[ImportGroupId] 154 ,[ResidentNumber] 155 ,[ApoDate] 156 ,[TestDate] 157 ,[Name] = LTRIM(RTRIM([Name])) 158 ,[KanaName] = LTRIM(RTRIM([KanaName])) 159 ,[Gender] 160 ,[Birth] 161 ,[Age] 162 ,[RankId] 163 ,[RankName] -- ★ 164 ,[TestTime] -- ★ 165 ,[LastTestId] 166 ,[Status] 167 ,[DeleteFlag] 168 ,[CreateDate] 169 ,[CreateUser] 170 ,[UpdateDate] 171 ,[UpdateUser] 172 FROM [dbo].[MExaminee] 173 where 174 -- [Name] = @p_name 175 [KanaName] = @p_kananame 176 and [Birth] = @p_birth 177 end else begin 178 if (@p_name is not null) begin 179 select 180 [ExamineeId] 181 ,[ImportGroupId] 182 ,[ResidentNumber] 183 ,[ApoDate] 184 ,[TestDate] 185 ,[Name] = LTRIM(RTRIM([Name])) 186 ,[KanaName] = LTRIM(RTRIM([KanaName])) 187 ,[Gender] 188 ,[Birth] 189 ,[Age] 190 ,[RankId] 191 ,[RankName] -- ★ 192 ,[TestTime] -- ★ 193 ,[LastTestId] 194 ,[Status] 195 ,[DeleteFlag] 196 ,[CreateDate] 197 ,[CreateUser] 198 ,[UpdateDate] 199 ,[UpdateUser] 200 FROM [dbo].[MExaminee] 201 where 202 [Name] = @p_name 203 -- [KanaName] = @p_kananame 204 -- and [Birth] = @p_birth 205 end else begin 206 if (@p_kananame is not null) begin 207 select 208 [ExamineeId] 209 ,[ImportGroupId] 210 ,[ResidentNumber] 211 ,[ApoDate] 212 ,[TestDate] 213 ,[Name] = LTRIM(RTRIM([Name])) 214 ,[KanaName] = LTRIM(RTRIM([KanaName])) 215 ,[Gender] 216 ,[Birth] 217 ,[Age] 218 ,[RankId] 219 ,[RankName] -- ★ 220 ,[TestTime] -- ★ 221 ,[LastTestId] 222 ,[Status] 223 ,[DeleteFlag] 224 ,[CreateDate] 225 ,[CreateUser] 226 ,[UpdateDate] 227 ,[UpdateUser] 228 FROM [dbo].[MExaminee] 229 where 230 -- [Name] = @p_name 231 [KanaName] = @p_kananame 232 -- and [Birth] = @p_birth 233 end else begin 234 if (@p_birth is not null) begin 235 select 236 [ExamineeId] 237 ,[ImportGroupId] 238 ,[ResidentNumber] 239 ,[ApoDate] 240 ,[TestDate] 241 ,[Name] = LTRIM(RTRIM([Name])) 242 ,[KanaName] = LTRIM(RTRIM([KanaName])) 243 ,[Gender] 244 ,[Birth] 245 ,[Age] 246 ,[RankId] 247 ,[RankName] -- ★ 248 ,[TestTime] -- ★ 249 ,[LastTestId] 250 ,[Status] 251 ,[DeleteFlag] 252 ,[CreateDate] 253 ,[CreateUser] 254 ,[UpdateDate] 255 ,[UpdateUser] 256 FROM [dbo].[MExaminee] 257 where 258 -- [Name] = @p_name 259 -- [KanaName] = @p_kananame 260 [Birth] = @p_birth 261 end 262 end 263 end 264 end 265 end 266 end 267 end 268 end 269 270 return (@return) 271END 272 273 274

投稿2018/11/27 05:13

satouyuuya

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問