回答編集履歴

2

調整

2022/03/23 09:21

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,3 +1,4 @@
1
+ # testデータ作成
1
2
  ```SQL
2
3
  create table tbl (id int primary key,val int);
3
4
  insert into tbl values(1,1),(2,2),(3,5),(4,4),(5,2);
@@ -24,5 +25,9 @@
24
25
  ※調整:数字の羅列以外はNGとする
25
26
  # procedure実行
26
27
  ```SQL
27
- call proc('2,3,4');
28
+ call proc('2,3,4'); /*出力OK*/
29
+ call proc('2'); /*出力OK*/
30
+ call proc('6'); /*ヒットするデータはないがOK*/
31
+ call proc('x'); /*数値じゃないので戻り値がない*/
32
+
28
33
  ```

1

調整

2022/03/23 09:17

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -9,13 +9,19 @@
9
9
  delimiter //
10
10
  create procedure proc(IN args varchar(50))
11
11
  begin
12
+ set @sql='select * from tbl where 1 ';
13
+ if args regexp "^(\\d+,)*\\d+$" then
12
- set @sql:=concat('select * from tbl where val in (',args,')');
14
+ set @sql:=concat(@sql,'and val in (',args,')');
15
+ else
16
+ set @sql:=concat(@sql,'and 0');
17
+ end if;
13
18
  prepare stmt from @sql;
14
19
  execute stmt;
15
20
  end;
16
21
  //
17
22
  delimiter ;
18
23
  ```
24
+ ※調整:数字の羅列以外はNGとする
19
25
  # procedure実行
20
26
  ```SQL
21
27
  call proc('2,3,4');