回答編集履歴
3
推敲
test
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
ary text[]
|
40
40
|
|
41
|
-
, group
|
41
|
+
, groupid uuid
|
42
42
|
|
43
43
|
)
|
44
44
|
|
@@ -60,7 +60,7 @@
|
|
60
60
|
|
61
61
|
insert into user_group(user_id, group_id)
|
62
62
|
|
63
|
-
values (ary[i], group
|
63
|
+
values (ary[i], groupid)
|
64
64
|
|
65
65
|
;
|
66
66
|
|
@@ -72,11 +72,11 @@
|
|
72
72
|
|
73
73
|
-- 配列を行に展開してループ
|
74
74
|
|
75
|
-
FOR wr IN select * from unnest(ary) as w(user
|
75
|
+
FOR wr IN select * from unnest(ary) as w(userid) LOOP
|
76
76
|
|
77
77
|
insert into user_group(user_id, group_id)
|
78
78
|
|
79
|
-
values (wr.user
|
79
|
+
values (wr.userid, groupid)
|
80
80
|
|
81
81
|
;
|
82
82
|
|
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
insert into user_group(user_id, group_id)
|
90
90
|
|
91
|
-
select user
|
91
|
+
select userid, groupid from unnest(ary) as w(userid)
|
92
92
|
|
93
93
|
;
|
94
94
|
|
@@ -110,7 +110,7 @@
|
|
110
110
|
|
111
111
|
ary :=array['test01', 'test02', 'test03'] ::text[]
|
112
112
|
|
113
|
-
,group
|
113
|
+
,groupid :='64cca920-726a-4298-8cf9-66e93d8c7ba1' ::uuid
|
114
114
|
|
115
115
|
)
|
116
116
|
|
2
推敲
test
CHANGED
@@ -82,7 +82,7 @@
|
|
82
82
|
|
83
83
|
END LOOP;
|
84
84
|
|
85
|
-
/
|
85
|
+
*/
|
86
86
|
|
87
87
|
-- 配列を行に展開してそのままinsert
|
88
88
|
|
1
追記
test
CHANGED
@@ -5,3 +5,113 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
loopするよりSQLで直接insertなりを行う方が手間が掛からないと思います。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
追記
|
12
|
+
|
13
|
+
--
|
14
|
+
|
15
|
+
insert対象のテーブルの定義を以下とします
|
16
|
+
|
17
|
+
```SQL
|
18
|
+
|
19
|
+
create table user_group(
|
20
|
+
|
21
|
+
id serial
|
22
|
+
|
23
|
+
, user_id text
|
24
|
+
|
25
|
+
, group_id uuid
|
26
|
+
|
27
|
+
);
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
以下はストアドの定義です。(戻り値は無しです)
|
32
|
+
|
33
|
+
3パターン記述していますが、2つはコメントアウトしています。
|
34
|
+
|
35
|
+
```SQL
|
36
|
+
|
37
|
+
CREATE OR REPLACE FUNCTION get_list_users(
|
38
|
+
|
39
|
+
ary text[]
|
40
|
+
|
41
|
+
, group_id uuid
|
42
|
+
|
43
|
+
)
|
44
|
+
|
45
|
+
RETURNS void
|
46
|
+
|
47
|
+
AS $BODY$
|
48
|
+
|
49
|
+
DECLARE
|
50
|
+
|
51
|
+
wr record;
|
52
|
+
|
53
|
+
BEGIN
|
54
|
+
|
55
|
+
/*
|
56
|
+
|
57
|
+
-- 配列の数だけループ
|
58
|
+
|
59
|
+
FOR i IN 1..array_length(ary, 1) LOOP
|
60
|
+
|
61
|
+
insert into user_group(user_id, group_id)
|
62
|
+
|
63
|
+
values (ary[i], group_id)
|
64
|
+
|
65
|
+
;
|
66
|
+
|
67
|
+
END LOOP;
|
68
|
+
|
69
|
+
*/
|
70
|
+
|
71
|
+
/*
|
72
|
+
|
73
|
+
-- 配列を行に展開してループ
|
74
|
+
|
75
|
+
FOR wr IN select * from unnest(ary) as w(user_id) LOOP
|
76
|
+
|
77
|
+
insert into user_group(user_id, group_id)
|
78
|
+
|
79
|
+
values (wr.user_id, group_id)
|
80
|
+
|
81
|
+
;
|
82
|
+
|
83
|
+
END LOOP;
|
84
|
+
|
85
|
+
/*
|
86
|
+
|
87
|
+
-- 配列を行に展開してそのままinsert
|
88
|
+
|
89
|
+
insert into user_group(user_id, group_id)
|
90
|
+
|
91
|
+
select user_id, group_id from unnest(ary) as w(user_id)
|
92
|
+
|
93
|
+
;
|
94
|
+
|
95
|
+
return;
|
96
|
+
|
97
|
+
END;
|
98
|
+
|
99
|
+
$BODY$
|
100
|
+
|
101
|
+
LANGUAGE PLpgSQL;
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
以下のような呼び出しです。
|
106
|
+
|
107
|
+
```SQL
|
108
|
+
|
109
|
+
select get_list_users(
|
110
|
+
|
111
|
+
ary :=array['test01', 'test02', 'test03'] ::text[]
|
112
|
+
|
113
|
+
,group_id :='64cca920-726a-4298-8cf9-66e93d8c7ba1' ::uuid
|
114
|
+
|
115
|
+
)
|
116
|
+
|
117
|
+
```
|