如何自动递增ID值 [英] How do I auto increment the ID value
本文介绍了如何自动递增ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个问题,但我的问题仍然存在。
Hi,
I have this question but my problem is still exists.
public void AutoIncrementTaskID()
{
DatabaseConnection conn = this._dbcEH;
string strComm =
string.Format(@"IF EXISTS(Select * from [DB].[Table] where ID Is NULL and [TempID] = @TempID)
Begin
UPDATE [DB].[Table] SET [ID]= (Select MAX(CAST(ISNULL(ID,0)as int))+1 from [DATABASENAME].[TABLE]) WHERE [TempID] =@TempID
End
");
我明白错误即将来临(选择MAX) (CAST(ISNULL(ID,0)为int))+ 1。任何人都可以尝试解决此问题并告诉我吗?感谢您提前帮助。
我尝试了什么:
我尝试通过更改强制转换代码来调整代码,但是所有bt它都没有用。
I understood that the error is coming near (Select MAX(CAST(ISNULL(ID,0)as int))+1. Can anyone try to resolve this and let me know? thanks for you help in advance.
What I have tried:
I tried tweaking the code by changing cast, max and all bt it didn't worked.
推荐答案
不要这样做。
而是将您的ID字段设置为IDENTITY字段,SQL将为您处理它。
你方法的问题在于我t将在生产中失败:只要两个用户同时尝试插入新行,它们就会失败,因为您的TempID无法复制!或者如果它可以,那会使情况变得更糟,因为具有该值的所有行都将被更改...
SQL自动增加字段 [ ^ ]
这篇关于如何自动递增ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文