how to get the next autoincrement value in sql

一个人想着一个人 提交于 2019-12-18 04:36:11

问题


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 and phone no. Id is auto increment and all other datatypes are varchar.

I am using this code to get the next auto increment value:

string s = "select max(id) as Id from Employee_Master";
SqlCommand cmd = new SqlCommand(s, obj.con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int i = Convert.ToInt16(dr["Id"].ToString());
txtId.Text = (i + 1).ToString();

I am displaying on a textBox.

But when last row from table is deleted, still I get that value which is recently deleted in textbox

How should I get the next autoincrement value?


回答1:


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.




回答2:


try this:

SELECT IDENT_CURRENT('tbl_name') + IDENT_INCR('tbl_name');



回答3:


If you are using Microsoft SQL Server. Use this statement to get current identity value of table. Then add your seed value which you have specified at time of designing table if you want to get next id.

SELECT IDENT_CURRENT(<TableName>)



回答4:


When you delete a row from the table the next number will stay the same as it doesnt decrement in any way.

So if you have 100 rows and you deleted row 100. You would have 99 rows but the next number is still going to be 101.




回答5:


the max(id) will get you maximum number in the list pf employee_master

e.g. id = 10, 20, 100 so max will get you 100

But when you delete the record it must have been not 100

So you still get 100 back

One important reason for me to say this might be the issue because you are not using order by id in your query




回答6:


select isnull((max(AddressID)+1),1) from AddressDetails



回答7:


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>



回答8:


Just a thought, if what you wanted was the last auto-number that you inserted on an already open connection try using:

SELECT @@IDENTITY FROM...

from that connection. That's the best way to keep track of what has just happened on a given connection and avoids race conditions w/ other connections. Getting the maximum identity is not generally feasible.




回答9:


 SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=databasename;User ID=sa;Password=123");
 con.Open();
 SqlCommand cmd = new SqlCommand("SELECT TOP(1) UID FROM InvoiceDetails ORDER BY 1 DESC", con);

 SqlDataReader reader = cmd.ExecuteReader();

 //won't need a while since it will only retrieve one row
 while (reader.Read())
 {
     string data = reader["UID"].ToString();
     //txtuniqueno.Text = data;
     //here is your data
     //cal();
     //txtuniqueno.Text = data.ToString();
     int i = Int32.Parse(data);
     i++;
     txtuid.Text = i.ToString();
  }


来源:https://stackoverflow.com/questions/11469062/how-to-get-the-next-autoincrement-value-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!