2017-02-28 71 views
1

我有表organisation並且它有一列。如果不存在值,則爲列值創建最小值爲1的序列,如果存在值,則創建1列大於列上最大值的序列

現在,我應該創建一個序列,每創建一個新的organisation(如在一個新行中持久化到表) - 新行上的列將獲得值1如果沒有值存在任何行上的列。如果IF值存在,新行將在所述列上得到一個值,這將是表中所述列的最高值加1。

例子:如果將沒有行或者只有該行對列中沒有值,新添加的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 ... 
); 

在組織實體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

如果你只是希望能夠重新啓動序列開始在通過查詢確定的值,你可以做到這一點與動態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 '[email protected]+';'; 
    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    | 
+-----+-----------------+ 

序列序號:

標識引用:

+0

大,徹底的答案,但不解決問題。我需要更改customer_number列。我認爲你不能通過IDENTITY來改變表格並添加自動增量。 –

+1

@SteveWaters用另一種替代方法更新。 – SqlZim

相關問題