[英]Fire Sequence.nextval query using jdbctemplate in spring
Database : Oracle
数据库: Oracle
I have table in which there are 10 columns and i want sequence next value when insert row and also use that sequence number which inserted.我有一个表,其中有 10 列,我想在插入行时对下一个值进行排序,并使用插入的序列号。
Now i have searched and find that KeyHolder of spring is useful but restrict for only less than 8 field so i can't use that.现在我搜索并发现 spring 的 KeyHolder 很有用,但仅限于少于 8 个字段,所以我不能使用它。
How can i fire "select MySequence.nextval from dual"
query and get sequence using jdbctemplate(NamedParameterJDBCTemplate)
?如何使用jdbctemplate(NamedParameterJDBCTemplate)
触发"select MySequence.nextval from dual"
查询并获取序列?
Is other way to achieve for get inserted sequence value ?.是否有其他方法可以实现获取插入的序列值?。
Using a jdbctemplate
you can just mention the sequence generator as a value, eg使用jdbctemplate
你可以只提到序列生成器作为一个值,例如
jdbcTemplate.update("INSERT INTO TABLE (id, data) VALUES (MySequence.nextval, ?)", new Object[] { data });
A note regarding the sequence generation: for versions before Oracle 12c you should have a trigger which will increment the sequence for you.关于序列生成的注意事项:对于 Oracle 12c 之前的版本,您应该有一个触发器,它将为您增加序列。 From 12c you can use the auto-increment feature.从 12c 开始,您可以使用自动增量功能。
You can achieve this by using JdbcTemplate like this :您可以像这样使用 JdbcTemplate 来实现这一点:
final SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(NEXT_VALUE_QUERY);
sqlRowSet.next();// mandatory to move the cursor
sqlRowSet.getLong(1);// the value of the nextval
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.