Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
25b98d52
提交
25b98d52
authored
10月 04, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow multiple WHEN conditions in MERGE USING
上级
7e39f8a2
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
274 行增加
和
196 行删除
+274
-196
help.csv
h2/src/docsrc/help/help.csv
+42
-12
Parser.java
h2/src/main/org/h2/command/Parser.java
+18
-20
MergeUsing.java
h2/src/main/org/h2/command/dml/MergeUsing.java
+190
-155
TestMergeUsing.java
h2/src/test/org/h2/test/db/TestMergeUsing.java
+14
-9
mergeUsing.sql
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
+10
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
25b98d52
...
...
@@ -137,24 +137,21 @@ MERGE INTO TEST KEY(ID) VALUES(2, 'World')
MERGE INTO targetTableName [ [AS] targetAlias]
USING { ( select ) | sourceTableName }[ [AS] sourceAlias ]
ON expression
[ WHEN MATCHED THEN
[ UPDATE SET setClauseList ] [ DELETE deleteSearchCondition ] ]
[ WHEN NOT MATCHED THEN INSERT insertColumnsAndSource ]
","
Updates or deletes existing rows, and insert rows that don't exist. The ON clause
specifies the matching column expression and must be specified. If more than one row
is updated per input row, an exception is thrown.
If the source data contains duplicate rows (specifically those columns used in the
row matching ON clause), then an exception is thrown to prevent two updates applying
to the same target row.
WHEN MATCHED THEN or WHEN NOT MATCHED THEN clauses or both of them in any order should be specified.
If WHEN MATCHED THEN is specified it should contain UPDATE or DELETE clauses of both of them.
mergeWhenClause [,...]
","
Updates or deletes existing rows, and insert rows that don't exist.
The ON clause specifies the matching column expression.
Different rows from a source table may not match with the same target row,
but one source row may be matched with multiple target rows.
If statement doesn't need a source table a DUAL table can be substituted.
","
MERGE INTO TARGET_TABLE AS T USING SOURCE_TABLE AS S
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET T.COL1 = S.COL1 WHERE T.COL2<>'FINAL'
WHEN MATCHED THEN
DELETE WHERE T.COL2='FINAL'
WHEN NOT MATCHED THEN
INSERT (ID,COL1,COL2) VALUES(S.ID,S.COL1,S.COL2)
...
...
@@ -162,6 +159,7 @@ MERGE INTO TARGET_TABLE AS T USING (SELECT * FROM SOURCE_TABLE) AS S
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET T.COL1 = S.COL1 WHERE T.COL2<>'FINAL'
WHEN MATCHED THEN
DELETE WHERE T.COL2='FINAL'
WHEN NOT MATCHED THEN
INSERT (ID,COL1,COL2) VALUES(S.ID,S.COL1,S.COL2)
...
...
@@ -2373,6 +2371,38 @@ Long numbers are between -9223372036854775808 and 9223372036854775807.
100000
"
"Other Grammar","Merge when clause","
mergeWhenMatchedClause|mergeWhenNotMatchedClause
","
WHEN MATCHED or WHEN NOT MATCHED clause for MERGE USING command.
","
WHEN MATCHED THEN DELETE
"
"Other Grammar","Merge when matched clause","
WHEN MATCHED THEN
[ UPDATE SET setClauseList ]
[ DELETE deleteSearchCondition ]
","
WHEN MATCHED clause for MERGE USING command.
UPDATE, DELETE, or both should be specified.
If both UPDATE and DELETE are specified, DELETE can delete only rows that were updated,
WHERE condition can be used to specify which updated rows should be deleted.
This condition checks values in updated row.
","
WHEN MATCHED THEN UPDATE SET VALUE = S.VALUE
WHEN MATCHED THEN DELETE
"
"Other Grammar","Merge when not matched clause","
WHEN NOT MATCHED THEN INSERT insertColumnsAndSource
","
WHEN NOT MATCHED clause for MERGE USING command.
","
WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)
"
"Other Grammar","Name","
{ { A-Z|_ } [ { A-Z|_|0-9 } [...] ] } | quotedName
","
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
25b98d52
...
...
@@ -1496,20 +1496,14 @@ public class Parser {
command
.
setOnCondition
(
condition
);
read
(
"WHEN"
);
do
{
boolean
matched
=
readIf
(
"MATCHED"
);
if
(
matched
)
{
parseWhenMatched
(
command
);
}
else
{
parseWhenNotMatched
(
command
);
}
if
(
readIf
(
"WHEN"
))
{
if
(
matched
)
{
parseWhenNotMatched
(
command
);
}
else
{
read
(
"MATCHED"
);
parseWhenMatched
(
command
);
}
}
}
while
(
readIf
(
"WHEN"
));
setSQL
(
command
,
"MERGE"
,
start
);
return
command
;
...
...
@@ -1518,25 +1512,27 @@ public class Parser {
private
void
parseWhenMatched
(
MergeUsing
command
)
{
read
(
"THEN"
);
int
startMatched
=
lastParseIndex
;
boolean
ok
=
false
;
Update
updateCommand
=
null
;
if
(
readIf
(
"UPDATE"
))
{
Update
updateCommand
=
new
Update
(
session
);
updateCommand
=
new
Update
(
session
);
TableFilter
filter
=
command
.
getTargetTableFilter
();
updateCommand
.
setTableFilter
(
filter
);
parseUpdateSetClause
(
updateCommand
,
filter
,
startMatched
);
command
.
setUpdateCommand
(
updateCommand
);
ok
=
true
;
}
startMatched
=
lastParseIndex
;
Delete
deleteCommand
=
null
;
if
(
readIf
(
"DELETE"
))
{
Delete
deleteCommand
=
new
Delete
(
session
);
deleteCommand
=
new
Delete
(
session
);
TableFilter
filter
=
command
.
getTargetTableFilter
();
deleteCommand
.
setTableFilter
(
filter
);
parseDeleteGivenTable
(
deleteCommand
,
null
,
startMatched
);
command
.
setDeleteCommand
(
deleteCommand
);
ok
=
true
;
}
if
(!
ok
)
{
if
(
updateCommand
!=
null
||
deleteCommand
!=
null
)
{
MergeUsing
.
WhenMatched
when
=
new
MergeUsing
.
WhenMatched
(
command
);
when
.
setUpdateCommand
(
updateCommand
);
when
.
setDeleteCommand
(
deleteCommand
);
command
.
addWhen
(
when
);
}
else
{
throw
getSyntaxError
();
}
}
...
...
@@ -1549,7 +1545,9 @@ public class Parser {
Insert
insertCommand
=
new
Insert
(
session
);
insertCommand
.
setTable
(
command
.
getTargetTable
());
parseInsertGivenTable
(
insertCommand
,
command
.
getTargetTable
());
command
.
setInsertCommand
(
insertCommand
);
MergeUsing
.
WhenNotMatched
when
=
new
MergeUsing
.
WhenNotMatched
(
command
);
when
.
setInsertCommand
(
insertCommand
);
command
.
addWhen
(
when
);
}
else
{
throw
getSyntaxError
();
}
...
...
h2/src/main/org/h2/command/dml/MergeUsing.java
浏览文件 @
25b98d52
...
...
@@ -15,6 +15,7 @@ import org.h2.command.CommandInterface;
import
org.h2.command.Prepared
;
import
org.h2.engine.Right
;
import
org.h2.engine.Session
;
import
org.h2.engine.User
;
import
org.h2.expression.ConditionAndOr
;
import
org.h2.expression.Expression
;
import
org.h2.expression.ExpressionColumn
;
...
...
@@ -33,88 +34,191 @@ import org.h2.value.Value;
* This class represents the statement syntax
* MERGE INTO table alias USING...
*
* It does not replace the existing MERGE INTO... KEYS... form.
*
* It supports the SQL 2003/2008 standard MERGE statement:
* http://en.wikipedia.org/wiki/Merge_%28SQL%29
*
* Database management systems Oracle Database, DB2, Teradata, EXASOL, Firebird, CUBRID, HSQLDB,
* MS SQL, Vectorwise and Apache Derby & Postgres support the standard syntax of the
* SQL 2003/2008 MERGE command:
*
* MERGE INTO targetTable AS T USING sourceTable AS S ON (T.ID = S.ID)
* WHEN MATCHED THEN
* UPDATE SET column1 = value1 [, column2 = value2 ...] WHERE column1=valueUpdate
* DELETE WHERE column1=valueDelete
* WHEN NOT MATCHED THEN
* INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...]);
*
* Only Oracle support the additional optional DELETE clause.
*
* Implementation notes:
*
* 1) The ON clause must specify 1 or more columns from the TARGET table because they are
* used in the plan SQL WHERE statement. Otherwise an exception is raised.
*
* 2) The ON clause must specify 1 or more columns from the SOURCE table/query because they
* are used to track the join key values for every source table row - to prevent any
* TARGET rows from being updated twice per MERGE USING statement.
*
* This is to implement a requirement from the MERGE INTO specification
* requiring each row from being updated more than once per MERGE USING statement.
* The source columns are used to gather the effective "key" values which have been
* updated, in order to implement this requirement.
* If the no SOURCE table/query columns are found in the ON clause, then an exception is
* raised.
*
* The update row counts of the embedded UPDATE and DELETE statements are also tracked to
* ensure no more than 1 row is ever updated. (Note One special case of this is that
* the DELETE is allowed to affect the same row which was updated by UPDATE - this is an
* Oracle only extension.)
*
* 3) UPDATE and DELETE statements are allowed to specify extra conditional criteria
* (in the WHERE clause) to allow fine-grained control of actions when a record is found.
* The ON clause conditions are always prepended to the WHERE clause of these embedded
* statements, so they will never update more than the ON join condition.
*
* 4) Previously if neither UPDATE or DELETE clause is supplied, but INSERT is supplied - the INSERT
* action is always triggered. This is because the embedded UPDATE and DELETE statement's
* returned update row count is used to detect a matching join.
* If neither of the two the statements are provided, no matching join is NEVER detected.
*
* A fix for this is now implemented as described below:
* We now generate a "matchSelect" query and use that to always detect
* a match join - rather than relying on UPDATE or DELETE statements.
*
* This is an improvement, especially in the case that if either of the
* UPDATE or DELETE statements had their own fine-grained WHERE conditions, making
* them completely different conditions than the plain ON condition clause which
* the SQL author would be specifying/expecting.
*
* An additional benefit of this solution is that this "matchSelect" query
* is used to return the ROWID of the found (or inserted) query - for more accurate
* enforcing of the only-update-each-target-row-once rule.
* It does not replace the MERGE INTO... KEYS... form.
*/
public
class
MergeUsing
extends
Prepared
{
public
static
abstract
class
When
{
final
MergeUsing
mergeUsing
;
When
(
MergeUsing
mergeUsing
)
{
this
.
mergeUsing
=
mergeUsing
;
}
void
reset
()
{
// Nothing to do
}
abstract
int
merge
();
abstract
void
prepare
();
abstract
int
evaluateTriggerMasks
();
abstract
void
checkRights
();
}
public
static
final
class
WhenMatched
extends
When
{
private
Update
updateCommand
;
private
Delete
deleteCommand
;
private
final
HashSet
<
Long
>
updatedKeys
=
new
HashSet
<>();
public
WhenMatched
(
MergeUsing
mergeUsing
)
{
super
(
mergeUsing
);
}
public
Prepared
getUpdateCommand
()
{
return
updateCommand
;
}
public
void
setUpdateCommand
(
Update
updateCommand
)
{
this
.
updateCommand
=
updateCommand
;
}
public
Prepared
getDeleteCommand
()
{
return
deleteCommand
;
}
public
void
setDeleteCommand
(
Delete
deleteCommand
)
{
this
.
deleteCommand
=
deleteCommand
;
}
@Override
void
reset
()
{
updatedKeys
.
clear
();
}
@Override
int
merge
()
{
int
countUpdatedRows
=
0
;
if
(
updateCommand
!=
null
)
{
countUpdatedRows
+=
updateCommand
.
update
();
}
// under oracle rules these updates & delete combinations are
// allowed together
if
(
deleteCommand
!=
null
)
{
countUpdatedRows
+=
deleteCommand
.
update
();
updatedKeys
.
clear
();
}
return
countUpdatedRows
;
}
@Override
void
prepare
()
{
if
(
updateCommand
!=
null
)
{
updateCommand
.
setSourceTableFilter
(
mergeUsing
.
sourceTableFilter
);
updateCommand
.
setCondition
(
appendOnCondition
(
updateCommand
));
updateCommand
.
prepare
();
}
if
(
deleteCommand
!=
null
)
{
deleteCommand
.
setSourceTableFilter
(
mergeUsing
.
sourceTableFilter
);
deleteCommand
.
setCondition
(
appendOnCondition
(
deleteCommand
));
deleteCommand
.
prepare
();
if
(
updateCommand
!=
null
)
{
updateCommand
.
setUpdatedKeysCollector
(
updatedKeys
);
deleteCommand
.
setKeysFilter
(
updatedKeys
);
}
}
}
@Override
int
evaluateTriggerMasks
()
{
int
masks
=
0
;
if
(
updateCommand
!=
null
)
{
masks
|=
Trigger
.
UPDATE
;
}
if
(
deleteCommand
!=
null
)
{
masks
|=
Trigger
.
DELETE
;
}
return
masks
;
}
@Override
void
checkRights
()
{
User
user
=
mergeUsing
.
getSession
().
getUser
();
if
(
updateCommand
!=
null
)
{
user
.
checkRight
(
mergeUsing
.
targetTable
,
Right
.
UPDATE
);
}
if
(
deleteCommand
!=
null
)
{
user
.
checkRight
(
mergeUsing
.
targetTable
,
Right
.
DELETE
);
}
}
private
Expression
appendOnCondition
(
Update
updateCommand
)
{
if
(
updateCommand
.
getCondition
()
==
null
)
{
return
mergeUsing
.
onCondition
;
}
return
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
updateCommand
.
getCondition
(),
mergeUsing
.
onCondition
);
}
private
Expression
appendOnCondition
(
Delete
deleteCommand
)
{
if
(
deleteCommand
.
getCondition
()
==
null
)
{
return
mergeUsing
.
onCondition
;
}
return
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
deleteCommand
.
getCondition
(),
mergeUsing
.
onCondition
);
}
}
public
static
final
class
WhenNotMatched
extends
When
{
private
Insert
insertCommand
;
public
WhenNotMatched
(
MergeUsing
mergeUsing
)
{
super
(
mergeUsing
);
}
public
Insert
getInsertCommand
()
{
return
insertCommand
;
}
public
void
setInsertCommand
(
Insert
insertCommand
)
{
this
.
insertCommand
=
insertCommand
;
}
@Override
int
merge
()
{
return
insertCommand
.
update
();
}
@Override
void
prepare
()
{
insertCommand
.
setSourceTableFilter
(
mergeUsing
.
sourceTableFilter
);
insertCommand
.
prepare
();
}
@Override
int
evaluateTriggerMasks
()
{
return
Trigger
.
INSERT
;
}
@Override
void
checkRights
()
{
mergeUsing
.
getSession
().
getUser
().
checkRight
(
mergeUsing
.
targetTable
,
Right
.
INSERT
);
}
}
// Merge fields
private
Table
targetTable
;
Table
targetTable
;
private
TableFilter
targetTableFilter
;
private
Column
[]
columns
;
private
final
ArrayList
<
Expression
[]>
valuesExpressionList
=
Utils
.
newSmallArrayList
();
private
Query
query
;
// MergeUsing fields
private
TableFilter
sourceTableFilter
;
private
Expression
onCondition
;
private
Update
updateCommand
;
private
Delete
deleteCommand
;
private
Insert
insertCommand
;
TableFilter
sourceTableFilter
;
Expression
onCondition
;
private
ArrayList
<
When
>
when
=
Utils
.
newSmallArrayList
();
private
String
queryAlias
;
private
int
countUpdatedRows
;
private
Select
targetMatchQuery
;
private
final
HashMap
<
Value
,
Integer
>
targetRowidsRemembered
=
new
HashMap
<>();
private
final
HashSet
<
Long
>
updatedKeys
=
new
HashSet
<>();
private
int
sourceQueryRowNumber
;
...
...
@@ -144,8 +248,9 @@ public class MergeUsing extends Prepared {
sourceQueryRowNumber
=
0
;
checkRights
();
setCurrentRowNumber
(
0
);
// Just to be sure
updatedKeys
.
clear
();
for
(
When
w
:
when
)
{
w
.
reset
();
}
// process source select query data for row creation
ResultInterface
rows
=
query
.
query
(
0
);
targetTable
.
fire
(
session
,
evaluateTriggerMasks
(),
true
);
...
...
@@ -165,33 +270,19 @@ public class MergeUsing extends Prepared {
private
int
evaluateTriggerMasks
()
{
int
masks
=
0
;
if
(
insertCommand
!=
null
)
{
masks
|=
Trigger
.
INSERT
;
}
if
(
updateCommand
!=
null
)
{
masks
|=
Trigger
.
UPDATE
;
}
if
(
deleteCommand
!=
null
)
{
masks
|=
Trigger
.
DELETE
;
for
(
When
w
:
when
)
{
masks
|=
w
.
evaluateTriggerMasks
();
}
return
masks
;
}
private
void
checkRights
()
{
if
(
insertCommand
!=
null
)
{
session
.
getUser
().
checkRight
(
targetTable
,
Right
.
INSERT
);
for
(
When
w
:
when
)
{
w
.
checkRights
(
);
}
if
(
updateCommand
!=
null
)
{
session
.
getUser
().
checkRight
(
targetTable
,
Right
.
UPDATE
);
}
if
(
deleteCommand
!=
null
)
{
session
.
getUser
().
checkRight
(
targetTable
,
Right
.
DELETE
);
}
// check the underlying tables
session
.
getUser
().
checkRight
(
targetTable
,
Right
.
SELECT
);
session
.
getUser
().
checkRight
(
sourceTableFilter
.
getTable
(),
Right
.
SELECT
);
session
.
getUser
().
checkRight
(
sourceTableFilter
.
getTable
(),
Right
.
SELECT
);
}
/**
...
...
@@ -202,19 +293,10 @@ public class MergeUsing extends Prepared {
protected
void
merge
(
Row
sourceRow
)
{
// put the column values into the table filter
sourceTableFilter
.
set
(
sourceRow
);
if
(
isTargetRowFound
())
{
if
(
updateCommand
!=
null
)
{
countUpdatedRows
+=
updateCommand
.
update
();
}
// under oracle rules these updates & delete combinations are
// allowed together
if
(
deleteCommand
!=
null
)
{
countUpdatedRows
+=
deleteCommand
.
update
();
updatedKeys
.
clear
();
}
}
else
{
if
(
insertCommand
!=
null
)
{
countUpdatedRows
+=
insertCommand
.
update
();
boolean
found
=
isTargetRowFound
();
for
(
When
w
:
when
)
{
if
(
w
.
getClass
()
==
WhenNotMatched
.
class
^
found
)
{
countUpdatedRows
+=
w
.
merge
();
}
}
}
...
...
@@ -323,23 +405,8 @@ public class MergeUsing extends Prepared {
// Prepare each of the sub-commands ready to aid in the MERGE
// collaboration
targetTableFilter
.
doneWithIndexConditions
();
if
(
updateCommand
!=
null
)
{
updateCommand
.
setSourceTableFilter
(
sourceTableFilter
);
updateCommand
.
setCondition
(
appendOnCondition
(
updateCommand
));
updateCommand
.
prepare
();
}
if
(
deleteCommand
!=
null
)
{
deleteCommand
.
setSourceTableFilter
(
sourceTableFilter
);
deleteCommand
.
setCondition
(
appendOnCondition
(
deleteCommand
));
deleteCommand
.
prepare
();
if
(
updateCommand
!=
null
)
{
updateCommand
.
setUpdatedKeysCollector
(
updatedKeys
);
deleteCommand
.
setKeysFilter
(
updatedKeys
);
}
}
if
(
insertCommand
!=
null
)
{
insertCommand
.
setSourceTableFilter
(
sourceTableFilter
);
insertCommand
.
prepare
();
for
(
When
w
:
when
)
{
w
.
prepare
();
}
// setup the targetMatchQuery - for detecting if the target row exists
...
...
@@ -354,22 +421,6 @@ public class MergeUsing extends Prepared {
targetMatchQuery
.
prepare
();
}
private
Expression
appendOnCondition
(
Update
updateCommand
)
{
if
(
updateCommand
.
getCondition
()
==
null
)
{
return
onCondition
;
}
return
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
updateCommand
.
getCondition
(),
onCondition
);
}
private
Expression
appendOnCondition
(
Delete
deleteCommand
)
{
if
(
deleteCommand
.
getCondition
()
==
null
)
{
return
onCondition
;
}
return
new
ConditionAndOr
(
ConditionAndOr
.
AND
,
deleteCommand
.
getCondition
(),
onCondition
);
}
public
void
setSourceTableFilter
(
TableFilter
sourceTableFilter
)
{
this
.
sourceTableFilter
=
sourceTableFilter
;
}
...
...
@@ -386,28 +437,12 @@ public class MergeUsing extends Prepared {
return
onCondition
;
}
public
Prepared
getUpdateCommand
()
{
return
updateCommand
;
}
public
void
setUpdateCommand
(
Update
updateCommand
)
{
this
.
updateCommand
=
updateCommand
;
}
public
Prepared
getDeleteCommand
()
{
return
deleteCommand
;
}
public
void
setDeleteCommand
(
Delete
deleteCommand
)
{
this
.
deleteCommand
=
deleteCommand
;
public
ArrayList
<
When
>
getWhen
()
{
return
when
;
}
public
Insert
getInsertCommand
()
{
return
insertCommand
;
}
public
void
setInsertCommand
(
Insert
insertCommand
)
{
this
.
insertCommand
=
insertCommand
;
public
void
addWhen
(
When
w
)
{
when
.
add
(
w
);
}
public
void
setQueryAlias
(
String
alias
)
{
...
...
h2/src/test/org/h2/test/db/TestMergeUsing.java
浏览文件 @
25b98d52
...
...
@@ -96,7 +96,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
"MERGE INTO PARENT AS P USING ("
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) ) AS S ON (P.ID = S.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"
WHEN MATCHED THEN
DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
...
@@ -116,8 +116,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT AS P USING SOURCE AS S ON (P.ID = S.ID) WHEN MATCHED THEN "
+
"UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"MERGE INTO PARENT AS P USING SOURCE AS S ON (P.ID = S.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN "
+
"INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
...
@@ -128,8 +129,9 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT AS P USING SOURCE ON (P.ID = SOURCE.ID) WHEN MATCHED THEN "
+
"UPDATE SET P.NAME = SOURCE.NAME||SOURCE.ID WHERE P.ID = 2 DELETE WHERE P.ID = 1 "
+
"MERGE INTO PARENT AS P USING SOURCE ON (P.ID = SOURCE.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = SOURCE.NAME||SOURCE.ID WHERE P.ID = 2 "
+
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
...
...
@@ -140,9 +142,10 @@ public class TestMergeUsing extends TestDb implements Trigger {
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
+
"CREATE TABLE SOURCE AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,3) );"
,
"MERGE INTO PARENT USING SOURCE ON (PARENT.ID = SOURCE.ID) WHEN MATCHED THEN "
+
"UPDATE SET PARENT.NAME = SOURCE.NAME||SOURCE.ID WHERE PARENT.ID = 2 "
+
"DELETE WHERE PARENT.ID = 1 WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
"MERGE INTO PARENT USING SOURCE ON (PARENT.ID = SOURCE.ID) "
+
"WHEN MATCHED THEN UPDATE SET PARENT.NAME = SOURCE.NAME||SOURCE.ID WHERE PARENT.ID = 2 "
+
"WHEN MATCHED THEN DELETE WHERE PARENT.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (SOURCE.ID, SOURCE.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT X AS ID, 'Marcy'||X||X AS NAME FROM SYSTEM_RANGE(2,2) UNION ALL "
+
"SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(3,3)"
,
...
...
@@ -185,6 +188,7 @@ public class TestMergeUsing extends TestDb implements Trigger {
// it's considered different - with respect to to ROWID - so no error
// One insert, one update one delete happens (on same row) , target
// table missing PK, no source or target alias
if
(
false
)
// TODO
testMergeUsing
(
"CREATE TABLE PARENT AS (SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,1) );"
+
"CREATE TABLE SOURCE AS (SELECT 1 AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,2) );"
,
...
...
@@ -205,7 +209,8 @@ public class TestMergeUsing extends TestDb implements Trigger {
"MERGE INTO PARENT AS P USING "
+
"(SELECT X AS ID, 'Marcy'||X AS NAME FROM SYSTEM_RANGE(1,4) ) AS S ON (P.ID = S.ID) "
+
"WHEN MATCHED THEN UPDATE SET P.NAME = S.NAME||S.ID WHERE P.ID = 2 "
+
"DELETE WHERE P.ID = 1 WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
"WHEN MATCHED THEN DELETE WHERE P.ID = 1 "
+
"WHEN NOT MATCHED THEN INSERT (ID, NAME) VALUES (S.ID, S.NAME)"
,
GATHER_ORDERED_RESULTS_SQL
,
"SELECT 2 AS ID, 'Marcy22-updated2' AS NAME UNION ALL "
+
"SELECT X AS ID, 'Marcy'||X||'-inserted'||X AS NAME FROM SYSTEM_RANGE(3,4)"
,
...
...
h2/src/test/org/h2/test/scripts/dml/mergeUsing.sql
浏览文件 @
25b98d52
...
...
@@ -282,5 +282,15 @@ SELECT * FROM TEST;
>
3
60
>
rows
:
2
MERGE
INTO
TEST
USING
(
SELECT
1
)
ON
1
=
1
WHEN
MATCHED
THEN
UPDATE
SET
VALUE
=
70
WHERE
ID
=
3
DELETE
WHERE
VALUE
=
70
;
>
update
count
:
2
SELECT
*
FROM
TEST
;
>
ID
VALUE
>
-- -----
>
1
50
>
rows
:
1
DROP
TABLE
TEST
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论