|
|
Question : Converting Integer Identity Columns to Character columns
|
|
SQL Server 2000
We have a system with two main IDs which exist in many tables - BaseID and SecondaryID.
Right now, they are currently Integer values (IDENTITY in the main table). It has been requested that a distinction be made between the two, for example:
a BaseID of 2000 could be confused with a SecondaryID of 2000. So the first could be BAS2000, and the second SEC2000.
Is it appropriate to make this change at the database level ? It would require more administration to maintain character fields as opposed to IDENTITY Integer fields.
If so, how should it be done ?
Thanks, JK
|
Answer : Converting Integer Identity Columns to Character columns
|
|
Another possibility: You could use a computed column that added the string prior to display but did *not* store the string in the table. Within that approach, you have two methods; which is easier depends on how the code is currently written.
1) Make the computed column names the same as the existing column names; the column name will be the same, although the datatype will change (from int to varchar). Naturally that requires renaming the existing columns. 2) Make the computed column a new name that is used only for display.
1) -- rename the existing columns so that the new computed columns can have the same name as -- the previous ID columns, reducing the changes that must be made to application code EXEC sp_rename 'tableName.BaseID', 'BaseIDNumber', 'COLUMN' EXEC sp_rename 'tableName.SecondaryID', 'SecondaryIDNumber', 'COLUMN' -- add computed columns, with needed prefixes, that take the place of the original columns ALTER TABLE tableName ADD BaseID AS 'BAS' + CAST(BaseIDNumber AS VARCHAR(10)) ALTER TABLE tableName ADD SecondaryID AS 'SEC' + CAST(SecondaryIDNumber AS VARCHAR(10))
2) -- add new computed columns used for display only ALTER TABLE tableName ADD BaseIDDisplay AS 'BAS' + CAST(BaseID AS VARCHAR(10)) ALTER TABLE tableName ADD SecondaryIDDisplay AS 'SEC' + CAST(SecondaryID AS VARCHAR(10))
|
|
|
|
|