
CREATE PROCEDURE dbo.CSAPP_KFZ_GET_NEXT_MVANUMBER
@result varchar(10) OUTPUT
AS
  DECLARE @weiter BIT
  
  SELECT @weiter = 0
  
  IF ((SELECT wert FROM Params WHERE param = 'BAUTOS_MVANUMMER_LISTE')=1)
    BEGIN
      WHILE (@weiter = 0)
        BEGIN
          select top 1 @result = isnull(mvanummer,'') from dbo.mvanummern order by mvanummer ASC
          IF (@result<>'')
            DELETE FROM dbo.MVANummern WHERE mvanummer = @result
          
          IF NOT(EXISTS(SELECT mvanummer FROM autos WHERE mvanummer = @result))
            SELECT @weiter = 1
        END
    END
  ELSE
    begin
      BEGIN TRAN
        update dbo.next_pks set pk = pk+1 where tablename='AUTOS'
        SELECT @result = pk FROM dbo.next_pks WHERE Tablename = 'AUTOS'
      COMMIT TRAN
    END
	return
 SELECT @result
