Get the following auto-increment value for a data column?

I have a datatable with an AutoIncrement column. How can I get the next increment (-1, -2, -3, ...) for a column without adding a new row?

+3
source share
7 answers

If this matches your design, you can always create a new row (datatable.newrow), and it already assigned nextId. Now it is lost, but the row is not bound to the table until you AcceptChanges.

+6
source

I got a solution to the problem (not working):

To get the following auto-increment value from SQLServer:

SELECT IDENT_CURRENT('table_name'); -- This will fetch the present auto-increment value.

Thus,

SELECT IDENT_CURRENT('table_name')+1; -- Next auto-increment value.

This will not be violated if the rows are deleted from the table with the identifier, and the key is not consistent.

, .

+7

MAX "" .

- ( ):

DataRow row = dt.Select("MAX(id)")[0];
int nextId = row["id"] + 1;

MSDN , . DataColumn..::.

+1

datatable AutoIncrement.

AI SQL AI? SQL db , , DataTable . , @@IDENTITY .

+1

SQL Server, :

SELECT cast(last_value as integer)+cast(increment_value as integer) 
FROM sys.identity_columns 
WHERE object_id=(SELECT id FROM sys.sysobjects WHERE name='PACLogErros')
AND name='ERRCod'
0

It’s best to use @@ IDENTITY, as Andrew mentioned, because we cannot deduce an algorithm in which SQL computes the following identity, and if you think that several people are updating the database, then the identity value that you calculated will not same when trying to save a row in the database

0
source

since the auto-increment value is always the largest value in this column, you can simply get it with max (column name) +1.

-2
source

Source: https://habr.com/ru/post/1710791/


All Articles