Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?
Posted

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...
 
Share this answer
 
Comments
Manfred Rudolf Bihy 22-Jun-11 5:23am    
In SQL Server you can insert values into an Identity column. See my answer for more details.
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!

[Edit]
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
 
Share this answer
 
v2
Comments
suryaswathi 22-Jun-11 2:07am    
very thanks really superb..its works good..but i able to insert now in identity field..for that i want to check or want to list the identity fields using query..

is there any query for listing the identity fields alone
Manfred Rudolf Bihy 22-Jun-11 5:25am    
See my update for on how to find all columns in all tables of a schema.
If my answer helped you please accept it and don't forget to vote for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900