------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_UStVA')
BEGIN
CREATE TABLE [dbo].[ut_UStVA]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Jahr] char(4) DEFAULT ' '
  ,[Monat] char(2) DEFAULT ' '
  ,[Korrektur] char(1) DEFAULT ' '
  ,[UstSeite] char(1) DEFAULT ' '
  ,[UstSpalte] char(3) DEFAULT ' '
  ,[SaldoAlt] float DEFAULT 0
  ,[SaldoNeu] float DEFAULT 0
  CONSTRAINT [Pk_UStVA]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UStVA_KeyDel')
  DROP TRIGGER [dbo].[tr_UStVA_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UStVA_KeyUpd')
  DROP TRIGGER [dbo].[tr_UStVA_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_UStVA_Keys')
  DROP TABLE [dbo].[ut_UStVA_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [Jahr] char(4) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [Monat] char(2) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Korrektur')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [Korrektur] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'UstSeite')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [UstSeite] char(1) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [UstSpalte] char(3) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'SaldoAlt')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [SaldoAlt] float NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'SaldoNeu')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ADD [SaldoNeu] float NOT NULL DEFAULT 0
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ALTER COLUMN [Jahr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ALTER COLUMN [Monat] char(2)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'Korrektur')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ALTER COLUMN [Korrektur] char(1)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'UstSeite')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ALTER COLUMN [UstSeite] char(1)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UStVA' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_UStVA] ALTER COLUMN [UstSpalte] char(3)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'UStVA')
  DROP VIEW [dbo].[UStVA]
GO
CREATE VIEW [dbo].[UStVA] AS
  SELECT * FROM [dbo].[ut_UStVA]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_UStVA')
  DROP PROCEDURE [dbo].[up_Add_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Add_UStVA] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_UStVA])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UStVA] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_UStVA')
  DROP PROCEDURE [dbo].[up_Upd_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Upd_UStVA]
  @RI integer
 ,@Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Korrektur char(1)
 ,@Parm_UstSeite char(1)
 ,@Parm_UstSpalte char(3)
 ,@Parm_SaldoAlt float
 ,@Parm_SaldoNeu float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_UStVA] SET
     [Jahr] = RTRIM(@Parm_Jahr)
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[Korrektur] = RTRIM(@Parm_Korrektur)
    ,[UstSeite] = RTRIM(@Parm_UstSeite)
    ,[UstSpalte] = RTRIM(@Parm_UstSpalte)
    ,[SaldoAlt] = @Parm_SaldoAlt
    ,[SaldoNeu] = @Parm_SaldoNeu
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_UStVA')
  DROP PROCEDURE [dbo].[up_Sel_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Sel_UStVA]
  @RI integer
AS
SELECT
     [Jahr]
    ,[Monat]
    ,[Korrektur]
    ,[UstSeite]
    ,[UstSpalte]
    ,[SaldoAlt]
    ,[SaldoNeu]
  FROM [dbo].[ut_UStVA]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_UStVA')
  DROP PROCEDURE [dbo].[up_Del_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Del_UStVA]
  @RI integer
AS
  DELETE FROM [dbo].[ut_UStVA] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_UStVA')
  DROP PROCEDURE [dbo].[up_Reorg_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Reorg_UStVA]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_UStVA] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_UStVA] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_UStVA] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_UStVA')
  DROP PROCEDURE [dbo].[up_Append_UStVA]
GO
CREATE PROCEDURE [dbo].[up_Append_UStVA]
  @Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Korrektur char(1)
 ,@Parm_UstSeite char(1)
 ,@Parm_UstSpalte char(3)
 ,@Parm_SaldoAlt float
 ,@Parm_SaldoNeu float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_UStVA])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UStVA] (
    RecId
   ,[Jahr]
   ,[Monat]
   ,[Korrektur]
   ,[UstSeite]
   ,[UstSpalte]
   ,[SaldoAlt]
   ,[SaldoNeu]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_Monat)
   ,RTRIM(@Parm_Korrektur)
   ,RTRIM(@Parm_UstSeite)
   ,RTRIM(@Parm_UstSpalte)
   ,@Parm_SaldoAlt
   ,@Parm_SaldoNeu
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_UStVA')
  DROP PROCEDURE [dbo].[up_TrSel_UStVA]
GO
CREATE PROCEDURE [dbo].[up_TrSel_UStVA]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([Monat]))
      +LTRIM(RTRIM([Korrektur]))
      +LTRIM(RTRIM([UstSeite]))
      +LTRIM(RTRIM([UstSpalte]))
    AS Data
  FROM     [dbo].[ut_UStVA]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_UStVA'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_UStVA]
(Jahr,Monat,Korrektur,UstSpalte,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_First_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_Last_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_Prior_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_Next_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_Next50_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_FindL_UStVA_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UStVA_Key00')
  DROP PROCEDURE [dbo].[up_FindF_UStVA_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus UStVA in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_UStVA_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UStVA] ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UStVA in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UStVA_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UStVA] ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UStVA_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldKorrektur char(1)
  DECLARE @oldUstSpalte char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Korrektur,UstSpalte FROM [dbo].[ut_UStVA] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldKorrektur,@oldUstSpalte
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur = @oldKorrektur AND UstSpalte = @oldUstSpalte AND RecId > @oldRecId
                   ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur = @oldKorrektur AND UstSpalte > @oldUstSpalte
                   ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur > @oldKorrektur
                   ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat > @oldMonat
                   ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr > @oldJahr
                   ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_UStVA_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldMonat char(2)
  DECLARE @oldKorrektur char(1)
  DECLARE @oldUstSpalte char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,Monat,Korrektur,UstSpalte FROM [dbo].[ut_UStVA] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldMonat,@oldKorrektur,@oldUstSpalte
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur = @oldKorrektur AND UstSpalte = @oldUstSpalte AND RecId < @oldRecId
                   ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur = @oldKorrektur AND UstSpalte < @oldUstSpalte
                   ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat = @oldMonat AND Korrektur < @oldKorrektur
                   ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr = @oldJahr AND Monat < @oldMonat
                   ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
                   WHERE Jahr < @oldJahr
                   ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_UStVA_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1) AND SUBSTRING(UstSpalte,1,@xLen-7) = SUBSTRING(@xKey,8,@xLen-7)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1) AND UstSpalte = SUBSTRING(@xKey,8,3)
      ORDER BY Jahr,Monat,Korrektur,UstSpalte,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_UStVA_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen < 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(Monat,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 6
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1) AND SUBSTRING(UstSpalte,1,@xLen-7) = SUBSTRING(@xKey,8,@xLen-7)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UStVA]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND Monat = SUBSTRING(@xKey,5,2) AND Korrektur = SUBSTRING(@xKey,7,1) AND UstSpalte = SUBSTRING(@xKey,8,3)
      ORDER BY Jahr DESC,Monat DESC,Korrektur DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

