-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, 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_Belegarten')
BEGIN
CREATE TABLE [dbo].[ut_Belegarten]
(
  [RecId]  integer NOT NULL
 ,[Nr] integer DEFAULT 0
 ,[NrKreis] integer DEFAULT 0
 ,[Beschr] char(30) DEFAULT ''
 ,[Rechnung] tinyint DEFAULT 0
 ,[Formular] integer DEFAULT 0
 ,[KzDrk] integer DEFAULT 0
 ,[Floskel] tinyint DEFAULT 0
 ,[Icon] char(30) DEFAULT ''
 ,[NextArt] integer DEFAULT 0
 ,[FixAufmass] tinyint DEFAULT 0
 ,[FixVorlauf] tinyint DEFAULT 0
 ,[FixAndSave] tinyint DEFAULT 0
 ,[Anschrift] char(2) DEFAULT ''
  CONSTRAINT [Pk_Belegarten]  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_Belegarten_KeyDel')
  DROP TRIGGER [dbo].[tr_Belegarten_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Belegarten_KeyUpd')
  DROP TRIGGER [dbo].[tr_Belegarten_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Belegarten_Keys')
  DROP TABLE [dbo].[ut_Belegarten_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_Belegarten' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Nr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'NrKreis')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [NrKreis] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Beschr')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Beschr] char(30) 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_Belegarten' and sc.[name] = 'Rechnung')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Rechnung] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Formular')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Formular] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'KzDrk')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [KzDrk] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Floskel')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Floskel] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Icon')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Icon] char(30) 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_Belegarten' and sc.[name] = 'NextArt')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [NextArt] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'FixAufmass')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [FixAufmass] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'FixVorlauf')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [FixVorlauf] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'FixAndSave')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [FixAndSave] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Anschrift')
BEGIN
  ALTER TABLE [dbo].[ut_Belegarten] ADD [Anschrift] char(2) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Beschr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Beschr'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Beschr] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Beschr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Beschr'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Beschr] varchar(30)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Icon'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Icon'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Icon] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Icon'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Icon'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Icon] varchar(30)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Anschrift'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Anschrift'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Anschrift] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Anschrift'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Belegarten' and sc.[name] = 'Anschrift'))
BEGIN
   ALTER TABLE [dbo].[ut_Belegarten] ALTER COLUMN [Anschrift] varchar(2)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Belegarten')
  DROP VIEW [dbo].[Belegarten]
GO
CREATE VIEW [dbo].[Belegarten] AS
  SELECT [RecId]
 ,[Nr]
 ,[NrKreis]
 ,[Beschr]
 ,[Rechnung]
 ,[Formular]
 ,[KzDrk]
 ,[Floskel]
 ,[Icon]
 ,[NextArt]
 ,[FixAufmass]
 ,[FixVorlauf]
 ,[FixAndSave]
 ,[Anschrift]
  FROM [dbo].[ut_Belegarten]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Belegarten')
  DROP PROCEDURE [dbo].[up_Sel_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_Sel_Belegarten]  @mRI integer
AS
  SELECT
    [Nr]
   ,[NrKreis]
   ,[Beschr]
   ,[Rechnung]
   ,[Formular]
   ,[KzDrk]
   ,[Floskel]
   ,[Icon]
   ,[NextArt]
   ,[FixAufmass]
   ,[FixVorlauf]
   ,[FixAndSave]
   ,[Anschrift]
  FROM [dbo].[ut_Belegarten] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Belegarten')
  DROP PROCEDURE [dbo].[up_Append_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_Append_Belegarten]
   @Param_Nr integer
  ,@Param_NrKreis integer
  ,@Param_Beschr char(30)
  ,@Param_Rechnung tinyint
  ,@Param_Formular integer
  ,@Param_KzDrk integer
  ,@Param_Floskel tinyint
  ,@Param_Icon char(30)
  ,@Param_NextArt integer
  ,@Param_FixAufmass tinyint
  ,@Param_FixVorlauf tinyint
  ,@Param_FixAndSave tinyint
  ,@Param_Anschrift char(2)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Belegarten()
      INSERT INTO [dbo].[ut_Belegarten] (RecId
        ,[Nr]
        ,[NrKreis]
        ,[Beschr]
        ,[Rechnung]
        ,[Formular]
        ,[KzDrk]
        ,[Floskel]
        ,[Icon]
        ,[NextArt]
        ,[FixAufmass]
        ,[FixVorlauf]
        ,[FixAndSave]
        ,[Anschrift]
          ) VALUES (@NeueId
        ,@Param_Nr
        ,@Param_NrKreis
        ,RTRIM(@Param_Beschr)
        ,@Param_Rechnung
        ,@Param_Formular
        ,@Param_KzDrk
        ,@Param_Floskel
        ,RTRIM(@Param_Icon)
        ,@Param_NextArt
        ,@Param_FixAufmass
        ,@Param_FixVorlauf
        ,@Param_FixAndSave
        ,UPPER(RTRIM(@Param_Anschrift))
          )
      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_Belegarten')
  DROP PROCEDURE [dbo].[up_Add_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_Add_Belegarten]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Belegarten()
  INSERT INTO [dbo].[ut_Belegarten] (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_Belegarten')
  DROP PROCEDURE [dbo].[up_Upd_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_Upd_Belegarten] @mRI integer
  ,@Param_Nr integer
  ,@Param_NrKreis integer
  ,@Param_Beschr char(30)
  ,@Param_Rechnung tinyint
  ,@Param_Formular integer
  ,@Param_KzDrk integer
  ,@Param_Floskel tinyint
  ,@Param_Icon char(30)
  ,@Param_NextArt integer
  ,@Param_FixAufmass tinyint
  ,@Param_FixVorlauf tinyint
  ,@Param_FixAndSave tinyint
  ,@Param_Anschrift char(2)
AS
  UPDATE [dbo].[ut_Belegarten] SET
    [Nr] = @Param_Nr
   ,[NrKreis] = @Param_NrKreis
   ,[Beschr] = RTRIM(@Param_Beschr)
   ,[Rechnung] = @Param_Rechnung
   ,[Formular] = @Param_Formular
   ,[KzDrk] = @Param_KzDrk
   ,[Floskel] = @Param_Floskel
   ,[Icon] = RTRIM(@Param_Icon)
   ,[NextArt] = @Param_NextArt
   ,[FixAufmass] = @Param_FixAufmass
   ,[FixVorlauf] = @Param_FixVorlauf
   ,[FixAndSave] = @Param_FixAndSave
   ,[Anschrift] = UPPER(RTRIM(@Param_Anschrift))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Belegarten')
  DROP PROCEDURE [dbo].[up_Upd2_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Belegarten] @Param_RecId varchar(20)
  ,@Param_Nr varchar(2)
  ,@Param_NrKreis varchar(2)
  ,@Param_Beschr varchar(30)
  ,@Param_Rechnung varchar(1)
  ,@Param_Formular varchar(4)
  ,@Param_KzDrk varchar(1)
  ,@Param_Floskel varchar(1)
  ,@Param_Icon varchar(30)
  ,@Param_NextArt varchar(2)
  ,@Param_FixAufmass varchar(1)
  ,@Param_FixVorlauf varchar(1)
  ,@Param_FixAndSave varchar(1)
  ,@Param_Anschrift varchar(2)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Belegarten] SET
    [Nr] = dbo.itcfunc_integer(@Param_Nr)
   ,[NrKreis] = dbo.itcfunc_integer(@Param_NrKreis)
   ,[Beschr] = RTRIM(@Param_Beschr)
   ,[Rechnung] = dbo.itcfunc_integer(@Param_Rechnung)
   ,[Formular] = dbo.itcfunc_integer(@Param_Formular)
   ,[KzDrk] = dbo.itcfunc_integer(@Param_KzDrk)
   ,[Floskel] = dbo.itcfunc_integer(@Param_Floskel)
   ,[Icon] = RTRIM(@Param_Icon)
   ,[NextArt] = dbo.itcfunc_integer(@Param_NextArt)
   ,[FixAufmass] = dbo.itcfunc_integer(@Param_FixAufmass)
   ,[FixVorlauf] = dbo.itcfunc_integer(@Param_FixVorlauf)
   ,[FixAndSave] = dbo.itcfunc_integer(@Param_FixAndSave)
   ,[Anschrift] = UPPER(RTRIM(@Param_Anschrift))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Belegarten] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Belegarten')
  DROP PROCEDURE [dbo].[up_TrSel_Belegarten]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Belegarten]
AS
  SELECT RecId,
    LTRIM(RTRIM([Beschr]))
   +LTRIM(RTRIM([Icon]))
   +LTRIM(RTRIM([Anschrift]))
  AS Data FROM [dbo].[ut_Belegarten] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belegarten_Key00')
  DROP PROCEDURE [dbo].[up_First_Belegarten_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Belegarten_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00) ORDER BY Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belegarten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belegarten_Key00] AS EXECUTE [dbo].[up_First_Belegarten_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belegarten_Key00')
  DROP PROCEDURE [dbo].[up_Last_Belegarten_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_Belegarten_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00) ORDER BY Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belegarten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belegarten_Key00] AS EXECUTE [dbo].[up_Last_Belegarten_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belegarten_Key00')
  DROP PROCEDURE [dbo].[up_Next_Belegarten_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Belegarten_Key00] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_Belegarten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belegarten_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belegarten_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belegarten_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Belegarten_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belegarten_Key00] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_Belegarten_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belegarten_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belegarten_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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


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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_First_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_Belegarten_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) ORDER BY NrKreis,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belegarten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belegarten_Key01] AS EXECUTE [dbo].[up_First_Belegarten_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_Last_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_Belegarten_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) ORDER BY NrKreis desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belegarten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belegarten_Key01] AS EXECUTE [dbo].[up_Last_Belegarten_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_Next_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Belegarten_Key01] @oldRecId integer AS
  DECLARE @oldNrKreis integer
  DECLARE @oldNr integer
  SELECT @oldNrKreis = NrKreis, @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01)
    WHERE (NrKreis = @oldNrKreis) AND (Nr > @oldNr)
    ORDER BY NrKreis,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01)
    WHERE (NrKreis > @oldNrKreis)
    ORDER BY NrKreis,Nr,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_Belegarten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belegarten_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belegarten_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belegarten_Key01] @oldRecId integer AS
  DECLARE @oldNrKreis integer
  DECLARE @oldNr integer
  SELECT @oldNrKreis = NrKreis, @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01)
    WHERE (NrKreis = @oldNrKreis) AND (Nr < @oldNr)
    ORDER BY NrKreis desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01)
    WHERE (NrKreis < @oldNrKreis)
    ORDER BY NrKreis desc,Nr 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_Belegarten_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belegarten_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belegarten_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belegarten_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mNrKreis integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,2)

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

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) WHERE 
      NrKreis = @mNrKreis AND 
      Nr = @mNr
      ORDER BY NrKreis,Nr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) WHERE 
      NrKreis = @mNrKreis
      ORDER BY NrKreis,Nr,RecId
    RETURN
  END

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



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belegarten_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Belegarten_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belegarten_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mNrKreis integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,2)

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

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) WHERE 
      NrKreis = @mNrKreis AND 
      Nr = @mNr
      ORDER BY NrKreis desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key01) WHERE 
      NrKreis = @mNrKreis
      ORDER BY NrKreis desc,Nr desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_First_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_Belegarten_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) ORDER BY NextArt,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belegarten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belegarten_Key02] AS EXECUTE [dbo].[up_First_Belegarten_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_Last_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_Belegarten_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) ORDER BY NextArt desc,Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belegarten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belegarten_Key02] AS EXECUTE [dbo].[up_Last_Belegarten_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_Next_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_Belegarten_Key02] @oldRecId integer AS
  DECLARE @oldNextArt integer
  DECLARE @oldNr integer
  SELECT @oldNextArt = NextArt, @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02)
    WHERE (NextArt = @oldNextArt) AND (Nr > @oldNr)
    ORDER BY NextArt,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02)
    WHERE (NextArt > @oldNextArt)
    ORDER BY NextArt,Nr,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_Belegarten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belegarten_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belegarten_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belegarten_Key02] @oldRecId integer AS
  DECLARE @oldNextArt integer
  DECLARE @oldNr integer
  SELECT @oldNextArt = NextArt, @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02)
    WHERE (NextArt = @oldNextArt) AND (Nr < @oldNr)
    ORDER BY NextArt desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02)
    WHERE (NextArt < @oldNextArt)
    ORDER BY NextArt desc,Nr 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_Belegarten_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belegarten_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belegarten_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_Belegarten_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mNextArt integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,2)

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

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) WHERE 
      NextArt = @mNextArt AND 
      Nr = @mNr
      ORDER BY NextArt,Nr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) WHERE 
      NextArt = @mNextArt
      ORDER BY NextArt,Nr,RecId
    RETURN
  END

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



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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Belegarten_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Belegarten_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_Belegarten_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mNextArt integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,2)
  DECLARE @mNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,3,2)

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

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) WHERE 
      NextArt = @mNextArt AND 
      Nr = @mNr
      ORDER BY NextArt desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten] WITH(INDEX = Ik_Key02) WHERE 
      NextArt = @mNextArt
      ORDER BY NextArt desc,Nr desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Belegarten_Key03')
  DROP PROCEDURE [dbo].[up_First_Belegarten_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_Belegarten_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten]  WHERE (1 = Rechnung) ORDER BY Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Belegarten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Belegarten_Key03] AS EXECUTE [dbo].[up_First_Belegarten_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Belegarten_Key03')
  DROP PROCEDURE [dbo].[up_Last_Belegarten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_Belegarten_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten]  WHERE (1 = Rechnung) ORDER BY Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Belegarten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Belegarten_Key03] AS EXECUTE [dbo].[up_Last_Belegarten_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Belegarten_Key03')
  DROP PROCEDURE [dbo].[up_Next_Belegarten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_Belegarten_Key03] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] 
    WHERE ((Nr > @oldNr)) AND ((1 = Rechnung))
    ORDER BY Nr,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_Belegarten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Belegarten_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_Belegarten_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Belegarten_Key03')
  DROP PROCEDURE [dbo].[up_Prior_Belegarten_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_Belegarten_Key03] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Belegarten] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Belegarten] 
    WHERE ((Nr < @oldNr)) AND ((1 = Rechnung))
    ORDER BY Nr 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_Belegarten_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Belegarten_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Belegarten_Key03] @oldRecId')
GO

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

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten]  WHERE 
      Nr = @mNr
      AND ((1 = Rechnung))
      ORDER BY Nr,RecId
    RETURN
  END

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


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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Belegarten]  WHERE 
      Nr = @mNr
      AND ((1 = Rechnung))
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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


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


