如果没有值,则为列值创建最小值为1的序列,如果值存在,则为列上的最大值1

时间:2017-02-28 13:18:30

标签: java sql jpa sql-server-2012 sequence

我有表organisation,它有一列customer_number

现在,我应该创建一个序列,每当创建一个新的organisation时 - 就像在新行中持久保存到表中一样 - 新行上的customer_number列将会得到值1如果任何行的customer_number列中没有值。如果存在IF值,则新行将在所述列上获得一个值,该值将是表中所述列的最高值加一。

示例:如果customer_number列上没有行或只有行没有值,则新添加的organisation行将获得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 ...
);

在Organization Entity 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 ...

1 个答案:

答案 0 :(得分:1)

如果您只想重新启动从查询确定的值开始的序列,您可以使用动态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

这不一定是程序,只是一个例子。

这不完全是sequence在Sql Server中的工作原理。以下是对sequenceidentity如何对未生成的插入值做出反应的快速比较。

测试设置: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               |
+-----+-----------------+

如果您改为使用identity

/* ------------- 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               |
+-----+-----------------+

序列参考:

身份参考: