要从表的列中获取最大数量后阻止用户插入行 [英] Want to block user to insert row after getting max number from column of table

查看:72
本文介绍了要从表的列中获取最大数量后阻止用户插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个表学生,并且有一个列ID(int),现在我想通过在表中添加ID + 1来插入行。 br />


当我插入一行时,我得到MAX(ID)+1的最大ID。当多个用户在执行相同的查询时获得相同的ID时发生问题同时向表中插入相同的ID。

Hi,

I have a table student and have a column ID (int ) , now i want to insert rows by adding ID+1 in table.

I got max id by MAX(ID)+1 when i insert one row and so on.Issue happen when multiple user get the same ID when they execute same query at same time and insert the same ID to table.

推荐答案

为防止出现这种情况,您应该为列设置INDEITY = true并在之后使用SCOPE_IDENTITY()获取值插入记录。这样就可以防止重复值插入。
To prevent this situation you should set INDEITY=true for your column and get the value by using SCOPE_IDENTITY() after insertion of record. This way you can prevent duplicate value insertion.


这篇关于要从表的列中获取最大数量后阻止用户插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆