Click here to Skip to main content
15,886,066 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
SLNS001 11-Jun-13 5:13am    
I think it's not a good practice. First of all, check with making ID as primary key satisfies with your requirement. Because it is auto increment, so we can make it primary key. If at all it is mandatory to have EmployeeID as primary key, try to implement composite primary key.
gvprabu 11-Jun-13 5:41am    
Already posted the answer in your Question...Check the link
Problem with autogenerating employee id in sql server[^]
Ahmed Bensaid 11-Jun-13 5:57am    
Hello,
What is the version of your SQL Server ?

1 solution

Please, refer this link: Using varchar for primary key value is ok?[^]

Personally i'm not using varchar field as a primary key. In my opinion it's a bad idea ;(
 
Share this answer
 
Comments
Espen Harlinn 30-Jun-13 17:47pm    
Well answered :-D
Maciej Los 30-Jun-13 17:53pm    
Thank you, Espen ;)

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