我如何动态地将值添加到自动增量字段? [英] How can i dynamically add value to the auto increment field?

查看:78
本文介绍了我如何动态地将值添加到自动增量字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何动态地向自动递增字段添加值?


我已经使用查询列出了表中的列(字段)
使用此列是否可以列出此自动增量字段?

How can i dynamically add value to the auto increment field?
or

I have list the columns(fields) in the table using query
with this columns is it possible to list is this auto increment field ?

推荐答案

您不能将自己的值添加到自动增量数据库字段中-整个想法是它是由数据库分配和维护的.任何尝试将值写入自动递增字段的尝试都会引发异常,而不是执行插入或更新操作.原因很简单:在执行插入操作以创建新记录之前,您不知道下一个可用字段是什么,数据库也不知道.您可能会认为确实如此,但是请记住,数据库通常是多用户的,并且在准备好记录时,另一个用户可以轻松地写他的记录...
You can''t add your own value to an autoincrement database field - the whole idea is that it is allocated and maintained by the database. Any attempt to write a value to an autoincrement field will throw an exception instead of the insert or update operation being performed. The reason is simple: you do not know what the next available field is, and neither does the database until the insert operation is performed to create the new record. You may think you do, but remember that databases are generally multiuser and another user can easily write his record(s) while you are getting yours ready...


如果您使用的是SQL Server您可能要尝试SET IDENTITY_INSERT ON | OFF.参见此处: http://www.sqlteam .com/article/how-to-insert-values-into-an-identity-column-in-sql-server [ OriginalGriff [ http://stackoverflow.com/questions/87747/how-do-you-determine-what-sql-tables-have-an-identity-column-programmatically [
If you are on SQL Server you might want to try SET IDENTITY_INSERT ON | OFF. See here: http://www.sqlteam.com/article/how-to-insert-values-into-an-identity-column-in-sql-server[^].

As OriginalGriff[^] already pointed out it''s generally not a good idea. I''ve had a couple of cases where this was needed, but I''d rather not do it if it can be avoided.

Cheers!


See here for how to find indentity columns in SQL Server: http://stackoverflow.com/questions/87747/how-do-you-determine-what-sql-tables-have-an-identity-column-programatically[^].
[/Edit]

--MRB


这篇关于我如何动态地将值添加到自动增量字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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