2015-11-20 7 views

risposta

5

Beh, è ​​possibile utilizzare SEQUENCE istruzione introdotte in SQL Server 2012 porta il metodo di ID che generano

di utilizzarlo in istruzione di inserimento, è necessario creare prima sequenza come questo -

CREATE SEQUENCE dbo.Id_Sequence 
    AS INT 
    START WITH 1 
    INCREMENT BY 1 
    MINVALUE 0 
    NO MAXVALUE 

Ora lo usano nella sua dichiarazione inserto come questo -

INSERT INTO dbo.Test1 
     (orderid , 
      custid , 
      empid 
     ) 
     SELECT NEXT VALUE FOR dbo.Id_Sequence, 
       @custid , 
       @empid 

Questo è tutto.

2

provare a creare un TRIGGER

CREATE TRIGGER incrementValue 
ON Test 
FOR Insert 
AS 
    Update Test 
    set columnvalue = columnvalue +1 
    where id in (select id from inserted) 
GO 
1

È possibile caricare il valore massimo del tavolo e aggiungere +1

SELECT MAX(MyColumn)+1 FROM MyTable 

magari aggiungere ISNULL per prima esecuzione.

ISNULL((SELECT MAX(MyColumn)+1 FROM MyTable),0) 
+0

grazie. ma non posso usare aggregato qui – bmsqldev

Problemi correlati