Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Auto Generated Auto Incremented Alphanumeric Sequential Code in SQL SERVER

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.65k
    Comment on it

    Create a table tbEmployeeMaster by following query :-

     

    1. CREATE TABLE tbEmployeeMaster
    2. (
    3. EmpId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    4. EmpName VARCHAR(100),
    5. EmpCode VARCHAR(15)
    6. )

     

    Create a user defined function 'fnEmpCode' to get new Employee code of specified length and specified prefix using following query :-

     

    1. CREATE FUNCTION fnEmpCode
    2. (
    3. @EmpPrefix VARCHAR(10),
    4. @EmpLength INT
    5. )
    6. RETURNS VARCHAR(20)
    7. AS
    8. BEGIN
    9. --Get maximum Emp id from table i.e. the last generated Empid in the table. (initially 0 if table has no data).
    10.  
    11. DECLARE @MaxEmpId INT;
    12. SET @MaxEmpId= ISNULL((SELECT MAX(EmpId) FROM tbEmployeeMaster),0)
    13.  
    14. --Increment maxempid by 1 to get next emp id.
    15. SET @MaxEmpId+=1
    16.  
    17. DECLARE @EmpCode VARCHAR(20),@i INT=1;
    18.  
    19. WHILE(@i=1)
    20. BEGIN
    21. --Generate new emp code of specified code length prefixed by specified prefix passed as parameters.
    22.  
    23. SET @EmpCode=@EmpPrefix +RIGHT(REPLICATE('0', @EmpLength-1) + CONVERT(VARCHAR(20),@MaxEmpId),@EmpLength)
    24.  
    25. --Check generated emp code. If already exists then get next emp code untill we get fresh emp code.
    26. IF EXISTS(SELECT 1 FROM tbEmployeeMaster WHERE EmpCode=@EmpCode)
    27. BEGIN
    28. SET @MaxEmpId +=1
    29. END
    30. ELSE
    31. BEGIN
    32. SET @i=0
    33. END
    34. END
    35. --Return newly generated emp code
    36. RETURN @EmpCode
    37. END
    38.  

     

    In above function line which actually generates the alphanumeric employee code is "SET @EmpCode=@EmpPrefix +RIGHT(REPLICATE('0', @EmpLength-1) + CONVERT(VARCHAR(20),@MaxEmpId),@EmpLength)" where @EmpCode is a variable defined in function and @EmpPrefix,@EmpLength-1 are passed variables to function. @EmpPrefix variable is concatenated with RIGHT function having two parameters, 1st parameter is REPLICATE function concatenated with CONVERT function and 2nd parameter is @EmpLength. REPLICATE function within RIGHT function will repeat the string '0' with specified number of times i.e  '@EmpLength-1' times concatenate with CONVERT function converting type of  @MaxEmpId to varchar. Finally RIGHT function will generate a string from rightmost side and will get appended to @EmpPrefix from rightmost side.  

     

    Finally create a stored procedure ‘spEmpDetails_Save’ where 'fnEmpCode' function is called to get auto generated alphanumeric employee code to save in table with other employee details using
    following query :-

     

    1. CREATE PROC spEmpDetails_Save
    2. (
    3. @EmpName VARCHAR(100)
    4. )
    5. AS
    6. BEGIN
    7. SET NOCOUNT ON;
    8. --Get New Emp Code by calling fnEmpCode function.
    9. DECLARE @EmpCode VARCHAR(20)=(SELECT dbo.fnEmpCode('EMP-',5))
    10.  
    11. --Insert employee detail including generated emp code in table
    12. INSERT INTO tbEmployeeMaster (EmpName, EmpCode)
    13. VALUES(@EmpName,@EmpCode);
    14. END

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: