Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
6aa21ffb
提交
6aa21ffb
authored
4月 18, 2009
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
The MERGE statement now returns 0 as the generated key if the row was updated.
上级
198d7b24
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
29 行增加
和
2 行删除
+29
-2
Merge.java
h2/src/main/org/h2/command/dml/Merge.java
+2
-0
help.csv
h2/src/main/org/h2/res/help.csv
+3
-2
TestStatement.java
h2/src/test/org/h2/test/jdbc/TestStatement.java
+24
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Merge.java
浏览文件 @
6aa21ffb
...
...
@@ -24,6 +24,7 @@ import org.h2.table.Column;
import
org.h2.table.Table
;
import
org.h2.util.ObjectArray
;
import
org.h2.value.Value
;
import
org.h2.value.ValueLong
;
/**
* This class represents the statement
...
...
@@ -106,6 +107,7 @@ public class Merge extends Prepared {
String
sql
=
buff
.
toString
();
update
=
session
.
prepare
(
sql
);
setCurrentRowNumber
(
0
);
session
.
setLastIdentity
(
ValueLong
.
get
(
0
));
if
(
list
.
size
()
>
0
)
{
count
=
0
;
for
(
int
x
=
0
;
x
<
list
.
size
();
x
++)
{
...
...
h2/src/main/org/h2/res/help.csv
浏览文件 @
6aa21ffb
...
...
@@ -82,8 +82,9 @@ MERGE INTO tableName [(columnName [,...])] [KEY(columnName [,...])]
Updates the row if it exists, and if the row does not exist, inserts a new row.
If the key columns are not specified, the primary key columns are used to find the row.
This command is sometimes called 'UPSERT' as it updates a row if it exists,
or inserts the row if it does not yet exist.
If more than one row per new row is affected, an exception is thrown.
or inserts the row if it does not yet exist. If more than one row per new row is affected,
an exception is thrown. If the table contains an auto-incremented key or identity column, a
nd the row was updated, the generated key is set to 0; otherwise it is set to the new key.
","
MERGE INTO TEST KEY(ID) VALUES(2, 'World')
"
...
...
h2/src/test/org/h2/test/jdbc/TestStatement.java
浏览文件 @
6aa21ffb
...
...
@@ -43,6 +43,7 @@ public class TestStatement extends TestBase {
testConnectionRollback
();
testStatement
();
if
(
config
.
jdk14
)
{
testIdentityMerge
();
testIdentity
();
}
conn
.
close
();
...
...
@@ -318,6 +319,29 @@ public class TestStatement extends TestBase {
stat
.
close
();
}
private
void
testIdentityMerge
()
throws
SQLException
{
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"drop table if exists test1"
);
stat
.
execute
(
"create table test1(id identity, x int)"
);
stat
.
execute
(
"drop table if exists test2"
);
stat
.
execute
(
"create table test2(id identity, x int)"
);
stat
.
execute
(
"merge into test1(x) key(x) values(5)"
);
ResultSet
keys
;
keys
=
stat
.
getGeneratedKeys
();
keys
.
next
();
assertEquals
(
1
,
keys
.
getInt
(
1
));
stat
.
execute
(
"insert into test2(x) values(10), (11), (12)"
);
stat
.
execute
(
"merge into test1(x) key(x) values(5)"
);
keys
=
stat
.
getGeneratedKeys
();
keys
.
next
();
assertEquals
(
0
,
keys
.
getInt
(
1
));
stat
.
execute
(
"merge into test1(x) key(x) values(6)"
);
keys
=
stat
.
getGeneratedKeys
();
keys
.
next
();
assertEquals
(
2
,
keys
.
getInt
(
1
));
stat
.
execute
(
"drop table test1, test2"
);
}
private
void
testIdentity
()
throws
SQLException
{
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"CREATE SEQUENCE SEQ"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论