IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('GetUniqueSaleNumber')))
THEN Drop procedure GetUniqueSaleNumber
END IF

GO

create function DBA.GetUniqueSaleNumber( in @PrinterName varchar(60) ) 
returns varchar(21)
begin atomic
  declare C integer;
  declare SerialNum varchar(8);
  select FP_SERIAL_NOM,IsNull(FP_SALE_NOM,1) into SerialNum,C from Printers where Name = @PrinterName;
  update Printers set FP_SALE_NOM = C+1 where Name = @PrinterName;
  return(SerialNum || '-' || ("right"('0000000000' || bvrUserN,4)) || '-' || ("right"('0000000000' || C,7)))
end