-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Belege_Rng')
BEGIN
CREATE TABLE [dbo].[ut_Belege_Rng]
(
  [RecId]  integer NOT NULL
 ,[PRJNR] char(8) DEFAULT ''
 ,[BELEGART] char(2) DEFAULT ''
 ,[BELEGNR] char(8) DEFAULT ''
 ,[TAG] char(2) DEFAULT ''
 ,[MONAT] char(2) DEFAULT ''
 ,[JAHR] char(4) DEFAULT ''
  CONSTRAINT [Pk_Belege_Rng]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Belege_Rng_KeyDel')
  DROP TRIGGER [dbo].[tr_Belege_Rng_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Belege_Rng_KeyUpd')
  DROP TRIGGER [dbo].[tr_Belege_Rng_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Belege_Rng_Keys')
  DROP TABLE [dbo].[ut_Belege_Rng_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] 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_Belege_Rng' and sc.[name] = 'BELEGART')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] ADD [BELEGART] 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_Belege_Rng' and sc.[name] = 'BELEGNR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] ADD [BELEGNR] 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_Belege_Rng' and sc.[name] = 'TAG')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] ADD [TAG] 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_Belege_Rng' and sc.[name] = 'MONAT')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] 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_Belege_Rng' and sc.[name] = 'JAHR')
BEGIN
  ALTER TABLE [dbo].[ut_Belege_Rng] ADD [JAHR] char(4) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'PRJNR')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] 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_Belege_Rng' and sc.[name] = 'BELEGART')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] ALTER COLUMN [BELEGART] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'BELEGNR')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] ALTER COLUMN [BELEGNR] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'TAG')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] ALTER COLUMN [TAG] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'MONAT')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] ALTER COLUMN [MONAT] char(2)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belege_Rng' and sc.[name] = 'JAHR')
BEGIN
   ALTER TABLE [dbo].[ut_Belege_Rng] ALTER COLUMN [JAHR] char(4)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Belege_Rng')
  DROP VIEW [dbo].[Belege_Rng]
GO
CREATE VIEW [dbo].[Belege_Rng] AS
  SELECT [RecId]
 ,[PRJNR]
 ,[BELEGART]
 ,[BELEGNR]
 ,[TAG]
 ,[MONAT]
 ,[JAHR]
  FROM [dbo].[ut_Belege_Rng]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_Belege_Rng', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_Belege_Rng
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Belege_Rng() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Belege_Rng] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Sel_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Sel_Belege_Rng]  @mRI integer
AS
  SELECT
    [PRJNR]
   ,[BELEGART]
   ,[BELEGNR]
   ,[TAG]
   ,[MONAT]
   ,[JAHR]
  FROM [dbo].[ut_Belege_Rng] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Sel2_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Sel2_Belege_Rng]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[Belege_Rng] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Append_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Append_Belege_Rng]
   @Param_PRJNR char(8)
  ,@Param_BELEGART char(2)
  ,@Param_BELEGNR char(8)
  ,@Param_TAG char(2)
  ,@Param_MONAT char(2)
  ,@Param_JAHR char(4)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Belege_Rng()
      INSERT INTO [dbo].[ut_Belege_Rng] (RecId
        ,[PRJNR]
        ,[BELEGART]
        ,[BELEGNR]
        ,[TAG]
        ,[MONAT]
        ,[JAHR]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PRJNR))
        ,UPPER(RTRIM(@Param_BELEGART))
        ,UPPER(RTRIM(@Param_BELEGNR))
        ,RTRIM(@Param_TAG)
        ,RTRIM(@Param_MONAT)
        ,RTRIM(@Param_JAHR)
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Add_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Add_Belege_Rng]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Belege_Rng()
  INSERT INTO [dbo].[ut_Belege_Rng] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Upd_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Upd_Belege_Rng] @mRI integer
  ,@Param_PRJNR char(8)
  ,@Param_BELEGART char(2)
  ,@Param_BELEGNR char(8)
  ,@Param_TAG char(2)
  ,@Param_MONAT char(2)
  ,@Param_JAHR char(4)
AS
  UPDATE [dbo].[ut_Belege_Rng] SET
    [PRJNR] = UPPER(RTRIM(@Param_PRJNR))
   ,[BELEGART] = UPPER(RTRIM(@Param_BELEGART))
   ,[BELEGNR] = UPPER(RTRIM(@Param_BELEGNR))
   ,[TAG] = RTRIM(@Param_TAG)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[JAHR] = RTRIM(@Param_JAHR)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Upd2_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Belege_Rng] @Param_RecId varchar(20)
  ,@Param_PRJNR varchar(8)
  ,@Param_BELEGART varchar(2)
  ,@Param_BELEGNR varchar(8)
  ,@Param_TAG varchar(2)
  ,@Param_MONAT varchar(2)
  ,@Param_JAHR varchar(4)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Belege_Rng] SET
    [PRJNR] = UPPER(RTRIM(@Param_PRJNR))
   ,[BELEGART] = UPPER(RTRIM(@Param_BELEGART))
   ,[BELEGNR] = UPPER(RTRIM(@Param_BELEGNR))
   ,[TAG] = RTRIM(@Param_TAG)
   ,[MONAT] = RTRIM(@Param_MONAT)
   ,[JAHR] = RTRIM(@Param_JAHR)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Belege_Rng] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Del_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Del_Belege_Rng] @mRI integer
AS
  DELETE FROM [dbo].[ut_Belege_Rng] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Belege_Rng')
  DROP PROCEDURE [dbo].[up_TrSel_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Belege_Rng]
AS
  SELECT RecId,
    LTRIM(RTRIM([PRJNR]))
   +LTRIM(RTRIM([BELEGART]))
   +LTRIM(RTRIM([BELEGNR]))
   +LTRIM(RTRIM([TAG]))
   +LTRIM(RTRIM([MONAT]))
   +LTRIM(RTRIM([JAHR]))
  AS Data FROM [dbo].[ut_Belege_Rng] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Belege_Rng')
  DROP PROCEDURE [dbo].[up_Reorg_Belege_Rng]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Belege_Rng] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Belege_Rng] 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_Belege_Rng] 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_Belege_Rng] )
  END
GO

-- logical key  ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  DROP INDEX [Ik_Key10] ON [dbo].[ut_Belege_Rng]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key10')
  CREATE INDEX [Ik_Key10] ON [dbo].[ut_Belege_Rng] (JAHR,MONAT,TAG,BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_First_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Rng_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Rng_Key10] AS EXECUTE [dbo].[up_First_Belege_Rng_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_Last_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Rng_Key10] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Rng_Key10] AS EXECUTE [dbo].[up_Last_Belege_Rng_Key10]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_Next_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Rng_Key10] @oldRecId integer AS
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR > @oldBELEGNR)
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR > @oldJAHR)
    ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Rng_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Rng_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Rng_Key10] @oldRecId integer AS
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR < @oldBELEGNR)
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10)
    WHERE (JAHR < @oldJAHR)
    ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Rng_Key10] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Rng_Key10] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_Next50_Belege_Rng_Key10]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[qp_Next50_Belege_Rng_Key10]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_Prior50_Belege_Rng_Key10]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[qp_Prior50_Belege_Rng_Key10]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Rng_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Belege_Rng_Key10] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Belege_Rng_Key10] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Rng_Key10')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Rng_Key10]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Rng_Key10] @xKey char(250), @xLen integer AS
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,7,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key10) WHERE 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Belege_Rng_Key10')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Belege_Rng_Key10] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Belege_Rng_Key10] @xKey, @xLen')
GO


-- logical key  ORDER BY BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key11')
  DROP INDEX [Ik_Key11] ON [dbo].[ut_Belege_Rng]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key11')
  CREATE INDEX [Ik_Key11] ON [dbo].[ut_Belege_Rng] (BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_First_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Rng_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) ORDER BY BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Rng_Key11] AS EXECUTE [dbo].[up_First_Belege_Rng_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_Last_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Rng_Key11] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) ORDER BY BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Rng_Key11] AS EXECUTE [dbo].[up_Last_Belege_Rng_Key11]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_Next_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Rng_Key11] @oldRecId integer AS
  DECLARE @oldBELEGNR char(8)
  SELECT @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11)
    WHERE (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11)
    WHERE (BELEGNR > @oldBELEGNR)
    ORDER BY BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Rng_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Rng_Key11] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Rng_Key11] @oldRecId integer AS
  DECLARE @oldBELEGNR char(8)
  SELECT @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11)
    WHERE (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11)
    WHERE (BELEGNR < @oldBELEGNR)
    ORDER BY BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Rng_Key11] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Rng_Key11] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_Next50_Belege_Rng_Key11]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[qp_Next50_Belege_Rng_Key11]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_Prior50_Belege_Rng_Key11]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[qp_Prior50_Belege_Rng_Key11]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Rng_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) WHERE 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGNR,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) WHERE 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY BELEGNR,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Belege_Rng_Key11] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Belege_Rng_Key11] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Rng_Key11')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Rng_Key11]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Rng_Key11] @xKey char(250), @xLen integer AS
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) WHERE 
      BELEGNR = @mBELEGNR
      ORDER BY BELEGNR desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key11) WHERE 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY BELEGNR desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Belege_Rng_Key11')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Belege_Rng_Key11] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Belege_Rng_Key11] @xKey, @xLen')
GO


-- logical key  ORDER BY PRJNR,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  DROP INDEX [Ik_Key12] ON [dbo].[ut_Belege_Rng]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key12')
  CREATE INDEX [Ik_Key12] ON [dbo].[ut_Belege_Rng] (PRJNR,BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_First_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Rng_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) ORDER BY PRJNR,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Rng_Key12] AS EXECUTE [dbo].[up_First_Belege_Rng_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_Last_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Rng_Key12] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) ORDER BY PRJNR desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Rng_Key12] AS EXECUTE [dbo].[up_Last_Belege_Rng_Key12]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_Next_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Rng_Key12] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY PRJNR,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGNR > @oldBELEGNR)
    ORDER BY PRJNR,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Rng_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Rng_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Rng_Key12] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY PRJNR desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR = @oldPRJNR) AND (BELEGNR < @oldBELEGNR)
    ORDER BY PRJNR desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Rng_Key12] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Rng_Key12] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_Next50_Belege_Rng_Key12]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[qp_Next50_Belege_Rng_Key12]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_Prior50_Belege_Rng_Key12]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[qp_Prior50_Belege_Rng_Key12]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Rng_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR,BELEGNR,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Belege_Rng_Key12] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Belege_Rng_Key12] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Rng_Key12')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Rng_Key12]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Rng_Key12] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key12) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR desc,BELEGNR desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Belege_Rng_Key12')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Belege_Rng_Key12] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Belege_Rng_Key12] @xKey, @xLen')
GO


-- logical key  ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  DROP INDEX [Ik_Key13] ON [dbo].[ut_Belege_Rng]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Belege_Rng' and so.[id] = si.[id] and si.[name] = 'Ik_Key13')
  CREATE INDEX [Ik_Key13] ON [dbo].[ut_Belege_Rng] (PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_First_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_First_Belege_Rng_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belege_Rng_Key13] AS EXECUTE [dbo].[up_First_Belege_Rng_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_Last_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_Last_Belege_Rng_Key13] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belege_Rng_Key13] AS EXECUTE [dbo].[up_Last_Belege_Rng_Key13]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_Next_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_Next_Belege_Rng_Key13] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId > @oldRecId)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR > @oldBELEGNR)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG > @oldTAG)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT > @oldMONAT)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR > @oldJAHR)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR > @oldPRJNR)
    ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belege_Rng_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belege_Rng_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_Prior_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belege_Rng_Key13] @oldRecId integer AS
  DECLARE @oldPRJNR char(8)
  DECLARE @oldJAHR char(4)
  DECLARE @oldMONAT char(2)
  DECLARE @oldTAG char(2)
  DECLARE @oldBELEGNR char(8)
  SELECT @oldPRJNR = PRJNR, @oldJAHR = JAHR, @oldMONAT = MONAT, @oldTAG = TAG, @oldBELEGNR = BELEGNR FROM [dbo].[ut_Belege_Rng] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR = @oldBELEGNR) AND (RecId < @oldRecId)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG = @oldTAG) AND (BELEGNR < @oldBELEGNR)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT = @oldMONAT) AND (TAG < @oldTAG)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR = @oldJAHR) AND (MONAT < @oldMONAT)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR = @oldPRJNR) AND (JAHR < @oldJAHR)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13)
    WHERE (PRJNR < @oldPRJNR)
    ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belege_Rng_Key13] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belege_Rng_Key13] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_Next50_Belege_Rng_Key13]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[qp_Next50_Belege_Rng_Key13]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_Prior50_Belege_Rng_Key13]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[qp_Prior50_Belege_Rng_Key13]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_FindF_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belege_Rng_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR >= @mPRJNR AND SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR >= @mJAHR AND SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT >= @mMONAT AND SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG >= @mTAG AND SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR >= @mBELEGNR AND SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR,JAHR,MONAT,TAG,BELEGNR,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Belege_Rng_Key13] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Belege_Rng_Key13] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belege_Rng_Key13')
  DROP PROCEDURE [dbo].[up_FindL_Belege_Rng_Key13]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belege_Rng_Key13] @xKey char(250), @xLen integer AS
  DECLARE @mPRJNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mJAHR char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,4)
  DECLARE @mMONAT char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,2)
  DECLARE @mTAG char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,15,2)
  DECLARE @mBELEGNR char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,17,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      BELEGNR = @mBELEGNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      SUBSTRING(PRJNR,1,LEN(@mPRJNR)) = @mPRJNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      SUBSTRING(JAHR,1,LEN(@mJAHR)) = @mJAHR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 14 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      SUBSTRING(MONAT,1,LEN(@mMONAT)) = @mMONAT
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 16 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      SUBSTRING(TAG,1,LEN(@mTAG)) = @mTAG
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  IF @xLen < 24 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belege_Rng] WITH(INDEX = Ik_Key13) WHERE 
      PRJNR = @mPRJNR AND 
      JAHR = @mJAHR AND 
      MONAT = @mMONAT AND 
      TAG = @mTAG AND 
      SUBSTRING(BELEGNR,1,LEN(@mBELEGNR)) = @mBELEGNR
      ORDER BY PRJNR desc,JAHR desc,MONAT desc,TAG desc,BELEGNR desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Belege_Rng_Key13')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Belege_Rng_Key13] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Belege_Rng_Key13] @xKey, @xLen')
GO


