------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelTransaction')
BEGIN
CREATE TABLE [dbo].[ut_BelTransaction]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[TransNo] char(8) DEFAULT ' '
  ,[Status] char(1) DEFAULT ' '
  CONSTRAINT [Pk_BelTransaction]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelTransaction_KeyDel')
  DROP TRIGGER [dbo].[tr_BelTransaction_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelTransaction_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelTransaction_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelTransaction_Keys')
  DROP TABLE [dbo].[ut_BelTransaction_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelTransaction' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] 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_BelTransaction' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] 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_BelTransaction' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] 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_BelTransaction' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] ADD [TransNo] 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_BelTransaction' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] ADD [Status] char(1) 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_BelTransaction' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] 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_BelTransaction' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] 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_BelTransaction' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] ALTER COLUMN [BelNr] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelTransaction' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] ALTER COLUMN [TransNo] char(8)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelTransaction' and sc.[name] = 'Status')
BEGIN
  ALTER TABLE [dbo].[ut_BelTransaction] ALTER COLUMN [Status] char(1)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelTransaction')
  DROP VIEW [dbo].[BelTransaction]
GO
CREATE VIEW [dbo].[BelTransaction] AS
  SELECT * FROM [dbo].[ut_BelTransaction]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelTransaction')
  DROP PROCEDURE [dbo].[up_Add_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Add_BelTransaction] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelTransaction])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelTransaction] (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_BelTransaction')
  DROP PROCEDURE [dbo].[up_Upd_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelTransaction]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_Status char(1)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelTransaction] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[TransNo] = RTRIM(@Parm_TransNo)
    ,[Status] = RTRIM(@Parm_Status)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelTransaction')
  DROP PROCEDURE [dbo].[up_Sel_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelTransaction]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[TransNo]
    ,[Status]
  FROM [dbo].[ut_BelTransaction]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelTransaction')
  DROP PROCEDURE [dbo].[up_Del_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Del_BelTransaction]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelTransaction] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelTransaction')
  DROP PROCEDURE [dbo].[up_Reorg_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelTransaction]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelTransaction] 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_BelTransaction] 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_BelTransaction] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelTransaction')
  DROP PROCEDURE [dbo].[up_Append_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_Append_BelTransaction]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_TransNo char(8)
 ,@Parm_Status char(1)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelTransaction])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelTransaction] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[TransNo]
   ,[Status]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_TransNo)
   ,RTRIM(@Parm_Status)
       )
  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_BelTransaction')
  DROP PROCEDURE [dbo].[up_TrSel_BelTransaction]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelTransaction]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([TransNo]))
      +LTRIM(RTRIM([Status]))
    AS Data
  FROM     [dbo].[ut_BelTransaction]
  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_BelTransaction'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key0')
CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelTransaction]
(PrjNr,BelArt,BelNr,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_First_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_Prior_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_Next_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_Next50_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_FindL_BelTransaction_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelTransaction_Key0')
  DROP PROCEDURE [dbo].[up_FindF_BelTransaction_Key0]
GO

------------------------------------------------------------
/* ersten Datensatz aus BelTransaction in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_First_BelTransaction_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY PrjNr,BelArt,BelNr,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BelTransaction in Folge Key0 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BelTransaction_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BelTransaction_Key0] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldTransNo char(8)

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

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

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

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

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

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,TransNo,RecId
    RETURN
  END

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen = 26
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND TransNo = SUBSTRING(@xKey,19,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,TransNo 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_BelTransaction'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key1')
CREATE INDEX [Ik_Key1] ON [dbo].[ut_BelTransaction]
(PrjNr,BelArt,BelNr,Status,TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_First_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_Last_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_Prior_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_Next_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_Next50_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_FindL_BelTransaction_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelTransaction_Key1')
  DROP PROCEDURE [dbo].[up_FindF_BelTransaction_Key1]
GO

------------------------------------------------------------
/* ersten Datensatz aus BelTransaction in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_First_BelTransaction_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BelTransaction in Folge Key1 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BelTransaction_Key1] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,Status DESC,TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BelTransaction_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldStatus char(1)
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT PrjNr,BelArt,BelNr,Status,TransNo FROM [dbo].[ut_BelTransaction] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldPrjNr,@oldBelArt,@oldBelNr,@oldStatus,@oldTransNo
  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_BelTransaction]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Status = @oldStatus AND TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Status = @oldStatus AND TransNo > @oldTransNo
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr = @oldBelNr AND Status > @oldStatus
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE PrjNr = @oldPrjNr AND BelArt = @oldBelArt AND BelNr > @oldBelNr
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE PrjNr = @oldPrjNr AND BelArt > @oldBelArt
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE PrjNr > @oldPrjNr
                   ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,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,Status DESC,TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BelTransaction_Key1] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldStatus char(1)
  DECLARE @oldTransNo char(8)

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

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

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

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

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,Status,TransNo,RecId
    RETURN
  END

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

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

  IF @xLen < 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Status = SUBSTRING(@xKey,19,1) AND SUBSTRING(TransNo,1,@xLen-19) = SUBSTRING(@xKey,20,@xLen-19)
      ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
    RETURN
  END

  IF @xLen = 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Status = SUBSTRING(@xKey,19,1) AND TransNo = SUBSTRING(@xKey,20,8)
      ORDER BY PrjNr,BelArt,BelNr,Status,TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,Status DESC,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen < 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,Status DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 18
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      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,Status DESC,TransNo DESC,RecId DESC
    RETURN
  END

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

  IF @xLen < 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Status = SUBSTRING(@xKey,19,1) AND SUBSTRING(TransNo,1,@xLen-19) = SUBSTRING(@xKey,20,@xLen-19)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,Status DESC,TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 27
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE PrjNr = SUBSTRING(@xKey,1,8) AND BelArt = SUBSTRING(@xKey,9,2) AND BelNr = SUBSTRING(@xKey,11,8) AND Status = SUBSTRING(@xKey,19,1) AND TransNo = SUBSTRING(@xKey,20,8)
      ORDER BY PrjNr DESC,BelArt DESC,BelNr DESC,Status DESC,TransNo 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_BelTransaction'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key2')
CREATE INDEX [Ik_Key2] ON [dbo].[ut_BelTransaction]
(TransNo,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_First_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_Last_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_Prior_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_Next_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_Next50_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_FindL_BelTransaction_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelTransaction_Key2')
  DROP PROCEDURE [dbo].[up_FindF_BelTransaction_Key2]
GO

------------------------------------------------------------
/* ersten Datensatz aus BelTransaction in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_First_BelTransaction_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY TransNo,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BelTransaction in Folge Key2 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BelTransaction_Key2] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction] ORDER BY TransNo DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY TransNo,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BelTransaction_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TransNo FROM [dbo].[ut_BelTransaction] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTransNo
  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_BelTransaction]
                   WHERE TransNo = @oldTransNo AND RecId > @oldRecId
                   ORDER BY TransNo,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE TransNo > @oldTransNo
                   ORDER BY TransNo,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 TransNo DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BelTransaction_Key2] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldTransNo char(8)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT TransNo FROM [dbo].[ut_BelTransaction] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldTransNo
  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_BelTransaction]
                   WHERE TransNo = @oldTransNo AND RecId < @oldRecId
                   ORDER BY TransNo DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
                   WHERE TransNo < @oldTransNo
                   ORDER BY TransNo 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 TransNo,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BelTransaction_Key2] @xKey char(128),@xLen integer AS

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE SUBSTRING(TransNo,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY TransNo,RecId
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE TransNo = SUBSTRING(@xKey,1,8)
      ORDER BY TransNo,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE SUBSTRING(TransNo,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY TransNo DESC,RecId DESC
    RETURN
  END

  IF @xLen = 8
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelTransaction]
      WHERE TransNo = SUBSTRING(@xKey,1,8)
      ORDER BY TransNo DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

