java - 为列的值创建一个序列,如果不存在值,则最小值为 1;如果存在值,则为比列上的最高值大 1

标签 java sql jpa sql-server-2012 sequence

我有表organization,它有一列customer_number

现在,我应该创建一个序列,每当创建一个新的组织时 - 就像新行中的内容一样 - customer_number列如果任何行的 customer_number 列中不存在任何值,则新行上的值将获取为 1。如果值确实存在,新行将在所述列上获得一个值,该值将是表中所述列上的最高值加一。

示例:如果 customer_number 列上没有行或只有没有值的行,则新添加的 organization 行将得到1 作为其 customer_number 列的值。例如,如果所述列上已经有两行值 - 比如说 100 和 200 - 新行将在该列上获得值 201。

我就是想不出实现这个目标的 SQL 咒语。

数据库是SQL Server 2012。

我已经有了 ID 序列。以下是在 SQL 脚本中创建表的方式:

CREATE SEQUENCE organisation_seq AS BIGINT START WITH 1 INCREMENT BY 1;
CREATE TABLE organisation
(
  id BIGINT NOT NULL,
  customer_number VARCHAR(50) UNIQUE,
... rest of the columns ...
);

在组织实体 bean 中,它是这样的:

@Entity
@Table(name = "organisation")
public class Organisation {

    static Logger logger = Logger.getLogger(Organisation.class.getName());
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="organisationSeq")
    @SequenceGenerator(name="organisationSeq", sequenceName="organisation_seq", allocationSize=1)
    private Long id;

    private String customerNumber;

   ... rest of the Entity properties ...

最佳答案

如果您只是希望能够从查询确定的值开始重新启动序列,则可以使用动态 sql 来实现,如下所示:

create procedure dbo.mysequence_restart as 
begin
set nocount, xact_abort on;
  declare @restartSequence nvarchar(256);
  declare @restartWith nvarchar(10); = convert(nvarchar(10),isnull((
    select max(id)+1 
    from organisation 
    where customer_number is not null
    ), 1));

  set @restartSequence = 'alter sequence dbo.mysequence restart with '+@restartWith+';'; 
  exec sp_executesql @restartSequence;
end;
go

这不一定是在过程中,它只是一个示例。

<小时/>

这并不完全是序列在Sql Server 中的工作方式。以下是序列身份如何对它们未生成的插入值使用react的快速比较。

测试设置:http://rextester.com/VDDF36095

/* ------------- using sequence ----------- */
create sequence organisation_seq as bigint 
  start with 1 increment by 1;
create table organisation
(
  id bigint not null default next value for organisation_seq,
  customer_number varchar(50) unique
);

insert into organisation values
 (next value for organisation_seq, 'a')
,(200, 'b')
,(next value for organisation_seq, 'c');

select * from organisation;

返回:

+-----+-----------------+
| id  | customer_number |
+-----+-----------------+
|   1 | a               |
| 200 | b               |
|   2 | c               |
+-----+-----------------+
<小时/>

如果您使用身份:

/* ------------- using identity ----------- */
create table organisation_identity
(
  id bigint not null identity (1,1),
  customer_number varchar(50) unique
);

insert into organisation_identity values
('a');

/* ------------- identity_insert on ----------- */
set identity_insert organisation_identity on;
insert into organisation_identity (id, customer_number) values
(200, 'b');
set identity_insert organisation_identity off;
/* ------------- identity_insert off ----------- */

insert into organisation_identity values
('c');

select * from organisation_identity;

返回:

+-----+-----------------+
| id  | customer_number |
+-----+-----------------+
|   1 | a               |
| 200 | b               |
| 201 | c               |
+-----+-----------------+
<小时/>

序列引用:

身份引用:

关于java - 为列的值创建一个序列,如果不存在值,则最小值为 1;如果存在值,则为比列上的最高值大 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42509836/

相关文章:

java - GAE JPA 2.0 Txn快速回滚

java - Play Framework 无法读取数据库配置并注入(inject) JPAApi

java - 在java中获得最大日期值的最佳方法?

java - Gradle 在目录中包含传递依赖项

java - 在 eclipse - Ubuntu 14.04 32 位上安装 OpenCV 3.0.0

java - 在 Java 中使用带有类似运算符和参数的选择查询

java - 根据出现次数获取子字符串,正则表达式java

php - SQL 查询失败

sql - Octopus Deploy - 执行脚本文件

java - JPA 查询.getResultList()?