how to get the next autoincrement value in sql

后端 未结 10 946
时光取名叫无心
时光取名叫无心 2020-12-16 13:21

I am creating a winform application in c#.and using sql database.

I have one table, employee_master, which has columns like Id, name, address

相关标签:
10条回答
  • 2020-12-16 13:51

    try this:

    SELECT IDENT_CURRENT('tbl_name') + IDENT_INCR('tbl_name');
    
    0 讨论(0)
  • 2020-12-16 13:52

    To get the next auto-increment value from SQLServer :

    This will fetch the present auto-increment value.

    SELECT IDENT_CURRENT('table_name');
    

    Next auto-increment value.

    SELECT IDENT_CURRENT('table_name')+1; 
    

    ------> This will work even if you add a row and then delete it because IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

    0 讨论(0)
  • 2020-12-16 13:55

    For MS SQL 2005 and greater:

    Select Cast(IsNULL(last_value,seed_value) As Int) + Cast(increment_value As Int) As NextID
    From sys.identity_columns
    WHERE NAME = <Table_Name>
    
    0 讨论(0)
  • 2020-12-16 13:57

    As for me, the best answer is:

    dbcc checkident(table_name)
    

    You will see two values (probably same) current identity value , current column value

    0 讨论(0)
提交回复
热议问题