I generating a string for my Primarykey column similar identity function. but its behavior is unexpected.
it Removing all 0 by String and storing only a single character. Correct me Where am i wrong..
Alter FUNCTION [dbo].[UDF_GenerateBatchNumber] (@BatchNumber as Varchar(10))
Returns Varchar
AS
BEGIN
-- Declare the return variable here
-- Add the T-SQL statements to compute the return value here
Select Top(1) @BatchNumber=[BatchNo] From TBL_iBATCH Order by [BatchNo] desc
IF @BatchNumber is Null
BEGIN
Set @BatchNumber ='B000001'
END
Else
BEGIN
Set @BatchNumber='B'+Convert(Varchar(7),(Cast(SUBSTRING(@BatchNumber,2,7) as Integer)+1))
END
RETURN @BatchNumber
END
The usual way is to have a computed column using your IDENTITY column.
This UDF will fail under load and give 2 keys the same