Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
i am auto generating an employee id in sql server, but i didnt just wana use identity to generate it in the 1,2,3.... Sequence i want it in a format like this Emp00001 so decided to manipulate my code
here is my code to create the table
Create table employee ( ID int identity, EmployeeID as (('Emp') + replicate ('0', 8 - Len(ID)) + Cast (ID as Varchar)) persisted Primary Key, FirstName Varchar(50) not null, LastName Varchar(50) not null)

but the problem am having with this code is that after creating the table, sql server pops up two warnings that got me realy worried
here are the warnings:
Warning! The maximum key length is 900 bytes. The index 'PK_Employee_1A89E4E1' has maximum length of 800 bytes. For some combination of large values, the insert/update operation will fail.
Warning: The maximum length of the row exceeds the permissible limit of 8060 bytes. For some combination of large values, the insert/update operation will fail.

is the code i used good, or is not a good practice please i need sugestions
thanks
Posted
Comments
gvprabu 11-Jun-13 5:34am    
Hi why u need this values to store in table... u can use while show in front screen.
better store only Integer values

Hi,
For Best pratise use INT datatype for IDENTITY Column,Store only Integer values as EmaployeeID Column... while show to front end...try like this.
SQL
SELECT 'Emp'+RIGHT('00000'+CAST(Column_Name AS VARCHAR(10)),5)
FROM Your_Table

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
CHill60 11-Jun-13 12:33pm    
Did you mean CAST(1 AS VARCHAR(10)) or did you mean CAST(ID AS VARCHAR(10))
gvprabu 12-Jun-13 4:30am    
Yes I means , CAST(ID AS VARCHAR(10))
As long as your column EmployeeID does not exceed 800 bytes, there is no problem.
 
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