Posted by: U?Me June 6, 2008
SQL Automatic incremental generator
Login in to Rate this Post:     0       ?        
This is what I did:

create function NextCustomerNumber()
returns char(7)
as
begin
    declare @lastval char(7)
    set @lastval = (select max(CustomerNumber) from absences)
    if @lastval is null set @lastval = 'C000001'
    declare @i int
    set @i = right(@lastval,4) + 1
    return 'C' + right('000000' + convert(varchar(10),@i),4)
end

THEN,
insert into TableA(CustomerNumber)
SELECT NextCustomerNumber()  as CustomerNumber

BUT this gives me error

Incorrect syntax near the keyword 'SELECT'.

'NextCustomerNumber' is not a recognized function name.


Read Full Discussion Thread for this article