August 30, 2012 at 4:04 pm
Hey all,
I have a table with a varchar(10) primary key. This value is determined and input by the user via web page.
This field is no longer needed to be populated by the user and will be hidden on all web pages.
example values: A057, A174, A175, B298, R159, R999, etc...
The value doesn't matter, so I am wondering for an INSERT, how I can auto-increment the field.
How/Can I increment a varchar field? ie X001 -> X002, X003, X004, etc...
or can I convert the field to an int with identity and increment of 1?
I can't access the DB directly so anything will need to be done via scripts that our DBA executes.
Any help you can provide would be greatly appreciated.
THanks.
August 30, 2012 at 4:16 pm
These quotes are slightly re-arranged for pertinance and organization, so please bear with me.
Isaac Vallee (8/30/2012)
I have a table with a varchar(10) primary key. This value is determined and input by the user via web page.example values: A057, A174, A175, B298, R159, R999, etc...
This field is no longer needed to be populated by the user and will be hidden on all web pages. The value doesn't matter, so I am wondering for an INSERT, how I can auto-increment the field.
With great difficulty, however, I'm confused.
I can't access the DB directly so anything will need to be done via scripts that our DBA executes.
or can I convert the field to an int with identity and increment of 1?
This confused me at first. Anything you do will need to be done via script with your DBA, or you cannot make changes to the database itself?
If you can't make db changes, you're not going to be able to adjust this issue.
Automating incrementors like this are fraught with peril from concurrency concerns. There are tricks to deal with it but you'll need to make it pretty robust. Before you travel down that path, what are the concerns about adding a new column that's a normal surrogate key (TableID INT IDENTITY( 1,1)) and translating all your foreign keys to look at that instead of the original column? While it'll end up being a bit more work up front, it'll save you a few hundred headaches as time marches on.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
August 30, 2012 at 4:19 pm
There's no need to do that when SQL server can manage the ID itself like stated in the post above.
If you have permission to alter the table, add a new ID field and remove the old one or simply make it not required.
Mark
August 30, 2012 at 7:41 pm
Based on what you said...
The value doesn't matter,
... then I believe that Mark's solution above is the best way to go.
The problem may be if you've used that PK value anywhere else in any other tables. It's not difficult to do but can be a complex "discovery" process.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply