@@ -33,9 +33,12 @@ EXECUTE stmt;
33
33
id1 (SELECT id2 FROM t2 WHERE val2='b')
34
34
1 2
35
35
DEALLOCATE PREPARE stmt;
36
+ REPLACE INTO t1 (id1, val1) VALUES (1, 'g') RETURNING t1.*;
37
+ id1 val1
38
+ 1 g
36
39
SELECT * FROM t1;
37
40
id1 val1
38
- 1 f
41
+ 1 g
39
42
TRUNCATE TABLE t1;
40
43
#
41
44
# Multiple values in one replace statement...RETURNING
@@ -69,10 +72,14 @@ id1 (SELECT id2 FROM t2 WHERE val2='b')
69
72
1 2
70
73
2 2
71
74
DEALLOCATE PREPARE stmt;
75
+ REPLACE INTO t1 VALUES (1,'u'),(2,'v') RETURNING t1.*;
76
+ id1 val1
77
+ 1 u
78
+ 2 v
72
79
SELECT * FROM t1;
73
80
id1 val1
74
- 1 s
75
- 2 t
81
+ 1 u
82
+ 2 v
76
83
TRUNCATE TABLE t1;
77
84
#
78
85
# REPLACE...SET...RETURNING
@@ -101,9 +108,12 @@ EXECUTE stmt;
101
108
id1 (SELECT id2 FROM t2 WHERE val2='b')
102
109
3 2
103
110
DEALLOCATE PREPARE stmt;
111
+ REPLACE INTO t1 SET id1=1, val1 = 'o' RETURNING t1.*;
112
+ id1 val1
113
+ 1 o
104
114
SELECT * FROM t1;
105
115
id1 val1
106
- 1 i
116
+ 1 o
107
117
2 j
108
118
3 k
109
119
#
@@ -113,7 +123,7 @@ TRUNCATE TABLE t2;
113
123
REPLACE INTO t2(id2,val2) SELECT * FROM t1;
114
124
REPLACE INTO t2 SELECT * FROM t1 WHERE id1=1 RETURNING *;
115
125
id2 val2
116
- 1 i
126
+ 1 o
117
127
REPLACE INTO t2 SELECT * FROM t1 WHERE id1=2 RETURNING id2+id2 AS total,
118
128
id2&&id2, id2|id2,UPPER(val2),f(id2);
119
129
total id2&&id2 id2|id2 UPPER(val2) f(id2)
@@ -122,7 +132,7 @@ REPLACE INTO t2 SELECT * FROM t1 WHERE id1=3 RETURNING (SELECT
122
132
GROUP_CONCAT(val1) FROM t1 WHERE id1=1);
123
133
(SELECT
124
134
GROUP_CONCAT(val1) FROM t1 WHERE id1=1)
125
- i
135
+ o
126
136
REPLACE INTO t2 SELECT * FROM t1 WHERE id1=1 RETURNING (SELECT
127
137
GROUP_CONCAT(val1) FROM t1 GROUP BY id1 HAVING id1=id1+1);
128
138
(SELECT
@@ -144,11 +154,109 @@ FROM t2 WHERE id2=0);
144
154
(SELECT id1+id2
145
155
FROM t2 WHERE id2=0)
146
156
NULL
157
+ REPLACE INTO t2 SELECT * FROM t1 WHERE id1=2 RETURNING t2.*;
158
+ id2 val2
159
+ 2 j
147
160
SELECT * FROM t2;
148
161
id2 val2
149
- 1 i
162
+ 1 o
150
163
2 j
151
164
3 k
152
165
DROP TABLE t1;
153
166
DROP TABLE t2;
154
167
DROP FUNCTION f;
168
+ #
169
+ # checking errors
170
+ #
171
+ CREATE TABLE t1(id1 INT,val1 VARCHAR(1));
172
+ CREATE TABLE t2(id2 INT,val2 VARCHAR(1));
173
+ REPLACE INTO t1 VALUES(1,'a'),(2,'b'),(3,'c');
174
+ #
175
+ # SIMLPE REPLACE STATEMENT
176
+ #
177
+ REPLACE INTO t2(id2,val2) VALUES(1,'a') RETURNING id1;
178
+ ERROR 42S22: Unknown column 'id1' in 'field list'
179
+ REPLACE INTO t2(id2,val2) values(2,'b') RETURNING SUM(id2);
180
+ ERROR HY000: Invalid use of group function
181
+ REPLACE INTO t2(id2,val2) VALUES(3,'c') RETURNING (SELECT id1 FROM t1);
182
+ ERROR 21000: Subquery returns more than 1 row
183
+ REPLACE INTO t2(id2,val2) VALUES(4,'d') RETURNING (SELECT * FROM t1);
184
+ ERROR 21000: Operand should contain 1 column(s)
185
+ REPLACE INTO t2(id2,val2) VALUES(4,'d') RETURNING (SELECT * FROM t2);
186
+ ERROR 21000: Operand should contain 1 column(s)
187
+ REPLACE INTO t2(id2,val2) VALUES(5,'e') RETURNING id2, (SELECT id1+id2 FROM
188
+ t1 WHERE id1=1);
189
+ id2 (SELECT id1+id2 FROM
190
+ t1 WHERE id1=1)
191
+ 5 6
192
+ REPLACE INTO t2(id2,val2) VALUES(5,'f') RETURNING (SELECT id2 FROM t2);
193
+ ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
194
+ REPLACE INTO t2 (id2, val2) VALUES (6,'f') RETURNING t1.*;
195
+ ERROR 42S02: Unknown table 'test.t1'
196
+ #
197
+ # Multiple rows in single insert statement
198
+ #
199
+ REPLACE INTO t2 VALUES(1,'a'),(2,'b') RETURNING id1;
200
+ ERROR 42S22: Unknown column 'id1' in 'field list'
201
+ REPLACE INTO t2 VALUES(3,'c'),(4,'d') RETURNING MAX(id2);
202
+ ERROR HY000: Invalid use of group function
203
+ REPLACE INTO t2 VALUES(5,'c'),(6,'f') RETURNING (SELECT id1 FROM t1);
204
+ ERROR 21000: Subquery returns more than 1 row
205
+ REPLACE INTO t2 VALUES(7,'g'),(8,'h') RETURNING (SELECT * FROM t1);
206
+ ERROR 21000: Operand should contain 1 column(s)
207
+ REPLACE INTO t2 VALUES(9,'g'),(10,'h') RETURNING (SELECT * FROM t2);
208
+ ERROR 21000: Operand should contain 1 column(s)
209
+ REPLACE INTO t2 VALUES(11,'e'),(12,'f') RETURNING id2, (SELECT id1+id2 FROM
210
+ t1 WHERE id1=1);
211
+ id2 (SELECT id1+id2 FROM
212
+ t1 WHERE id1=1)
213
+ 11 12
214
+ 12 13
215
+ REPLACE INTO t2 VALUES(13,'f'),(14,'g') RETURNING (SELECT id2 FROM t2);
216
+ ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
217
+ REPLACE INTO t2 VALUES(13,'f'),(14,'g') RETURNING t1.*;
218
+ ERROR 42S02: Unknown table 'test.t1'
219
+ #
220
+ # REPLACE ... SET
221
+ #
222
+ REPLACE INTO t2 SET id2=1, val2='a' RETURNING id1;
223
+ ERROR 42S22: Unknown column 'id1' in 'field list'
224
+ REPLACE INTO t2 SET id2=2, val2='b' RETURNING COUNT(id2);
225
+ ERROR HY000: Invalid use of group function
226
+ REPLACE INTO t2 SET id2=3, val2='c' RETURNING (SELECT id1 FROM t1);
227
+ ERROR 21000: Subquery returns more than 1 row
228
+ REPLACE INTO t2 SET id2=4, val2='d' RETURNING (SELECT * FROM t1);
229
+ ERROR 21000: Operand should contain 1 column(s)
230
+ REPLACE INTO t2 SET id2=4, val2='d' RETURNING (SELECT * FROM t2);
231
+ ERROR 21000: Operand should contain 1 column(s)
232
+ REPLACE INTO t2 SET id2=5, val2='e' RETURNING id2, (SELECT id1+id2 FROM t1
233
+ WHERE id1=1);
234
+ id2 (SELECT id1+id2 FROM t1
235
+ WHERE id1=1)
236
+ 5 6
237
+ REPLACE INTO t2 SET id2=5, val2='f' RETURNING (SELECT id2 FROM t2);
238
+ ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
239
+ REPLACE INTO t2 SET id2=5, val2='f' RETURNING t1.*;
240
+ ERROR 42S02: Unknown table 'test.t1'
241
+ #
242
+ # REPLACE...SELECT
243
+ #
244
+ REPLACE INTO t2(id2, val2) SELECT * FROM t1 WHERE id1=1 RETURNING id1;
245
+ ERROR 42S22: Unknown column 'id1' in 'field list'
246
+ REPLACE INTO t2(id2, val2) SELECT * FROM t1 WHERE id1=2 RETURNING MAX(id2);
247
+ ERROR HY000: Invalid use of group function
248
+ REPLACE INTO t2(id2, val2) SELECT * FROM t1 WHERE id1=2 RETURNING (SELECT
249
+ id1 FROM t1);
250
+ ERROR 21000: Subquery returns more than 1 row
251
+ REPLACE INTO t2(id2, val2) SELECT * FROM t1 WHERE id1=2 RETURNING (SELECT
252
+ * FROM t1);
253
+ ERROR 21000: Operand should contain 1 column(s)
254
+ REPLACE INTO t2(id2, val2) SELECT * FROM t1 WHERE id1=2 RETURNING(SELECT
255
+ * FROM t2);
256
+ ERROR 21000: Operand should contain 1 column(s)
257
+ REPLACE INTO t2(id2,val2) SELECT * FROM t1 WHERE id1=2 RETURNING (SELECT
258
+ id2 FROM t2);
259
+ ERROR 21000: Subquery returns more than 1 row
260
+ REPLACE INTO t2(id2,val2) SELECT t1.* FROM t1 WHERE id1=2 RETURNING t1.*;
261
+ ERROR 42S02: Unknown table 'test.t1'
262
+ DROP TABLE t1,t2;
0 commit comments