Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Sir,
I want to increment my Field Value in databaselike CUS01,cus02..
Posted

OK, well, what have you tried ? Your values are strings ? They can't auto increment, you'd have to write code to do it, like perhaps a trigger ? You've failed to tell us much of anything, so it's hard to know what you want.
 
Share this answer
 
Typically you shouldn't think of doing this in this way. What you might want to consider, as an alternative, is to just have an auto incrementing value, and then you can just add this to the prefix string after you've retrieved it.
 
Share this answer
 
It's not that simple: strings cannot be autoincremented by the database only integers. Plus there are other problems.

I assume that you want to create customer codes such as CUS01, CUS02, ANC01, ANC02, so you will have to work out what happens when you already have CUS99 and want the next one... You also have to be very careful about multiuser problems.

There are two safe ways to do it: moving your INSERTs to a stored procedure, and working out the new value there, or create a stored procedure which takes the "CUS" part and returns the next available name. To support this, I would have a separate table containing a string for the CUS part, and the last value used for that string. The stored procedure reads the row, updates the value, and writes it back befoire returnign teh new string complete.

Me? I would go the second route.
 
Share this answer
 

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