------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelegMwstKennzeichen')
BEGIN
CREATE TABLE [dbo].[ut_BelegMwstKennzeichen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[BuchGrp] char(10) DEFAULT ' '
  ,[KzMwst] char(3) DEFAULT ' '
  ,[AbYMD] char(8) DEFAULT ' '
  ,[Satz] float DEFAULT 0
  ,[DrkNull] tinyint DEFAULT 0
  ,[MWStKateg] char(3) DEFAULT ' '
  CONSTRAINT [Pk_BelegMwstKennzeichen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegMwstKennzeichen_KeyDel')
  DROP TRIGGER [dbo].[tr_BelegMwstKennzeichen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegMwstKennzeichen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelegMwstKennzeichen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelegMwstKennzeichen_Keys')
  DROP TABLE [dbo].[ut_BelegMwstKennzeichen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [PrjNr] char(8) 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_BelegMwstKennzeichen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [BelArt] 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_BelegMwstKennzeichen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [BelNr] char(8) 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_BelegMwstKennzeichen' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [BuchGrp] char(10) 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_BelegMwstKennzeichen' and sc.[name] = 'KzMwst')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [KzMwst] 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_BelegMwstKennzeichen' and sc.[name] = 'AbYMD')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [AbYMD] char(8) 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_BelegMwstKennzeichen' and sc.[name] = 'Satz')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [Satz] 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_BelegMwstKennzeichen' and sc.[name] = 'DrkNull')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [DrkNull] tinyint 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_BelegMwstKennzeichen' and sc.[name] = 'MWStKateg')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ADD [MWStKateg] char(3) NOT NULL DEFAULT ' '
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [BelNr] char(8)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [BuchGrp] char(10)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'KzMwst')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [KzMwst] char(3)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'AbYMD')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [AbYMD] char(8)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelegMwstKennzeichen' and sc.[name] = 'MWStKateg')
BEGIN
  ALTER TABLE [dbo].[ut_BelegMwstKennzeichen] ALTER COLUMN [MWStKateg] char(3)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelegMwstKennzeichen')
  DROP VIEW [dbo].[BelegMwstKennzeichen]
GO
CREATE VIEW [dbo].[BelegMwstKennzeichen] AS
  SELECT * FROM [dbo].[ut_BelegMwstKennzeichen]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Add_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Add_BelegMwstKennzeichen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelegMwstKennzeichen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegMwstKennzeichen] (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_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Upd_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelegMwstKennzeichen]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BuchGrp char(10)
 ,@Parm_KzMwst char(3)
 ,@Parm_AbYMD char(8)
 ,@Parm_Satz float
 ,@Parm_DrkNull tinyint
 ,@Parm_MWStKateg char(3)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelegMwstKennzeichen] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[BuchGrp] = RTRIM(@Parm_BuchGrp)
    ,[KzMwst] = RTRIM(@Parm_KzMwst)
    ,[AbYMD] = RTRIM(@Parm_AbYMD)
    ,[Satz] = @Parm_Satz
    ,[DrkNull] = @Parm_DrkNull
    ,[MWStKateg] = RTRIM(@Parm_MWStKateg)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Sel_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelegMwstKennzeichen]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[BuchGrp]
    ,[KzMwst]
    ,[AbYMD]
    ,[Satz]
    ,[DrkNull]
    ,[MWStKateg]
  FROM [dbo].[ut_BelegMwstKennzeichen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Del_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Del_BelegMwstKennzeichen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelegMwstKennzeichen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Reorg_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelegMwstKennzeichen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelegMwstKennzeichen] 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_BelegMwstKennzeichen] 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_BelegMwstKennzeichen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_Append_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_Append_BelegMwstKennzeichen]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_BuchGrp char(10)
 ,@Parm_KzMwst char(3)
 ,@Parm_AbYMD char(8)
 ,@Parm_Satz float
 ,@Parm_DrkNull tinyint
 ,@Parm_MWStKateg char(3)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelegMwstKennzeichen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelegMwstKennzeichen] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[BuchGrp]
   ,[KzMwst]
   ,[AbYMD]
   ,[Satz]
   ,[DrkNull]
   ,[MWStKateg]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_BuchGrp)
   ,RTRIM(@Parm_KzMwst)
   ,RTRIM(@Parm_AbYMD)
   ,@Parm_Satz
   ,@Parm_DrkNull
   ,RTRIM(@Parm_MWStKateg)
       )
  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_BelegMwstKennzeichen')
  DROP PROCEDURE [dbo].[up_TrSel_BelegMwstKennzeichen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelegMwstKennzeichen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([BuchGrp]))
      +LTRIM(RTRIM([KzMwst]))
      +LTRIM(RTRIM([AbYMD]))
      +LTRIM(RTRIM([MWStKateg]))
    AS Data
  FROM     [dbo].[ut_BelegMwstKennzeichen]
  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_BelegMwstKennzeichen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_BelegMwstKennzeichen]
(PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_First_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelegMwstKennzeichen_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegMwstKennzeichen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelegMwstKennzeichen_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus BelegMwstKennzeichen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_BelegMwstKennzeichen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen] ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BelegMwstKennzeichen in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BelegMwstKennzeichen_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BelegMwstKennzeichen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldKzMwst char(3)
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,KzMwst,BuchGrp FROM [dbo].[ut_BelegMwstKennzeichen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldKzMwst,@oldBuchGrp
  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_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst = @oldKzMwst AND BuchGrp = @oldBuchGrp AND RecId > @oldRecId
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst = @oldKzMwst AND BuchGrp > @oldBuchGrp
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst > @oldKzMwst
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr > @oldPrjNr
                   ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,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 PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BelegMwstKennzeichen_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldKzMwst char(3)
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,KzMwst,BuchGrp FROM [dbo].[ut_BelegMwstKennzeichen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldKzMwst,@oldBuchGrp
  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_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst = @oldKzMwst AND BuchGrp = @oldBuchGrp AND RecId < @oldRecId
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst = @oldKzMwst AND BuchGrp < @oldBuchGrp
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND KzMwst < @oldKzMwst
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr < @oldPrjNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp 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 PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BelegMwstKennzeichen_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(KzMwst,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3) AND SUBSTRING(BuchGrp,1,@xLen-21) = SUBSTRING(@xKey,22,@xLen-21)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3) AND BuchGrp = SUBSTRING(@xKey,22,10)
      ORDER BY PrjNr,BelArt,BelNr,KzMwst,BuchGrp,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_BelegMwstKennzeichen_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(KzMwst,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3) AND SUBSTRING(BuchGrp,1,@xLen-21) = SUBSTRING(@xKey,22,@xLen-21)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND KzMwst = SUBSTRING(@xKey,19,3) AND BuchGrp = SUBSTRING(@xKey,22,10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,KzMwst DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BelegMwstKennzeichen'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key01')
CREATE INDEX [Ik_Key01] ON [dbo].[ut_BelegMwstKennzeichen]
(PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_First_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_Next50_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelegMwstKennzeichen_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegMwstKennzeichen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelegMwstKennzeichen_Key01]
GO

------------------------------------------------------------
/* ersten Datensatz aus BelegMwstKennzeichen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_First_BelegMwstKennzeichen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen] ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BelegMwstKennzeichen in Folge Key01 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BelegMwstKennzeichen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BelegMwstKennzeichen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldMWStKateg char(3)
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,MWStKateg,BuchGrp FROM [dbo].[ut_BelegMwstKennzeichen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldMWStKateg,@oldBuchGrp
  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_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg = @oldMWStKateg AND BuchGrp = @oldBuchGrp AND RecId > @oldRecId
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg = @oldMWStKateg AND BuchGrp > @oldBuchGrp
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg > @oldMWStKateg
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr > @oldPrjNr
                   ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,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 PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BelegMwstKennzeichen_Key01] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldMWStKateg char(3)
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,MWStKateg,BuchGrp FROM [dbo].[ut_BelegMwstKennzeichen] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldMWStKateg,@oldBuchGrp
  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_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg = @oldMWStKateg AND BuchGrp = @oldBuchGrp AND RecId < @oldRecId
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg = @oldMWStKateg AND BuchGrp < @oldBuchGrp
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND MWStKateg < @oldMWStKateg
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr < @oldBelNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr = @oldPrjNr AND BelArt < @oldBelArt
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
                   WHERE PrjNr < @oldPrjNr
                   ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp 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 PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BelegMwstKennzeichen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(MWStKateg,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen < 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3) AND SUBSTRING(BuchGrp,1,@xLen-21) = SUBSTRING(@xKey,22,@xLen-21)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  IF @xLen = 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3) AND BuchGrp = SUBSTRING(@xKey,22,10)
      ORDER BY PrjNr,BelArt,BelNr,MWStKateg,BuchGrp,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_BelegMwstKennzeichen_Key01] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE SUBSTRING(PrjNr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND SUBSTRING(BelArt,1,@xLen-8) = SUBSTRING(@xKey,9,@xLen-8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND SUBSTRING(BelNr,1,@xLen-10) = SUBSTRING(@xKey,11,@xLen-10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND SUBSTRING(MWStKateg,1,@xLen-18) = SUBSTRING(@xKey,19,@xLen-18)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 21
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen < 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3) AND SUBSTRING(BuchGrp,1,@xLen-21) = SUBSTRING(@xKey,22,@xLen-21)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 31
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegMwstKennzeichen]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND MWStKateg = SUBSTRING(@xKey,19,3) AND BuchGrp = SUBSTRING(@xKey,22,10)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,MWStKateg DESC,BuchGrp DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

