-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, 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_BelegAnhang')
BEGIN
CREATE TABLE [dbo].[ut_BelegAnhang]
(
  [RecId]  integer NOT NULL
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
 ,[DocType] integer DEFAULT 0
 ,[ContType] integer DEFAULT 0
 ,[Prozessed] tinyint DEFAULT 0
 ,[Stamp] char(32) DEFAULT ''
 ,[OrgName] char(250) DEFAULT ''
 ,[Extension] char(10) DEFAULT ''
 ,[Comment] char(50) DEFAULT ''
 ,[DatumFile] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[DatumScan] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Attachment] tinyint DEFAULT 0
  CONSTRAINT [Pk_BelegAnhang]  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_BelegAnhang_KeyDel')
  DROP TRIGGER [dbo].[tr_BelegAnhang_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelegAnhang_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelegAnhang_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelegAnhang_Keys')
  DROP TABLE [dbo].[ut_BelegAnhang_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_BelegAnhang' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] 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_BelegAnhang' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [BelArt] char(2) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [BelNr] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [LfdNr] 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_BelegAnhang' and sc.[name] = 'DocType')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [DocType] 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_BelegAnhang' and sc.[name] = 'ContType')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [ContType] 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_BelegAnhang' and sc.[name] = 'Prozessed')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [Prozessed] 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_BelegAnhang' and sc.[name] = 'Stamp')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [Stamp] char(32) 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_BelegAnhang' and sc.[name] = 'OrgName')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [OrgName] char(250) 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_BelegAnhang' and sc.[name] = 'Extension')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [Extension] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Comment')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [Comment] char(50) 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_BelegAnhang' and sc.[name] = 'DatumFile')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [DatumFile] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'DatumScan')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [DatumScan] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Attachment')
BEGIN
  ALTER TABLE [dbo].[ut_BelegAnhang] ADD [Attachment] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'PrjNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [PrjNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'PrjNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [BelArt] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [BelArt] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [BelNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (32 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Stamp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Stamp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Stamp] char(32)
END
GO
IF (32 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Stamp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Stamp'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Stamp] varchar(32)
END
GO
IF (250 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'OrgName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'OrgName'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [OrgName] char(250)
END
GO
IF (250 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'OrgName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'OrgName'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [OrgName] varchar(250)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Extension'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Extension'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Extension] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Extension'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Extension'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Extension] varchar(10)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Comment'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Comment'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Comment] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Comment'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelegAnhang' and sc.[name] = 'Comment'))
BEGIN
   ALTER TABLE [dbo].[ut_BelegAnhang] ALTER COLUMN [Comment] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelegAnhang')
  DROP VIEW [dbo].[BelegAnhang]
GO
CREATE VIEW [dbo].[BelegAnhang] AS
  SELECT [RecId]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[LfdNr]
 ,[DocType]
 ,[ContType]
 ,[Prozessed]
 ,[Stamp]
 ,[OrgName]
 ,[Extension]
 ,[Comment]
 ,[DatumFile]
 ,[DatumScan]
 ,[Attachment]
  FROM [dbo].[ut_BelegAnhang]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelegAnhang')
  DROP PROCEDURE [dbo].[up_Sel_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelegAnhang]  @mRI integer
AS
  SELECT
    [PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
   ,[DocType]
   ,[ContType]
   ,[Prozessed]
   ,[Stamp]
   ,[OrgName]
   ,[Extension]
   ,[Comment]
   ,[DatumFile]
   ,[DatumScan]
   ,[Attachment]
  FROM [dbo].[ut_BelegAnhang] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelegAnhang')
  DROP PROCEDURE [dbo].[up_Append_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_Append_BelegAnhang]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr integer
  ,@Param_DocType integer
  ,@Param_ContType integer
  ,@Param_Prozessed tinyint
  ,@Param_Stamp char(32)
  ,@Param_OrgName char(250)
  ,@Param_Extension char(10)
  ,@Param_Comment char(50)
  ,@Param_DatumFile datetime
  ,@Param_DatumScan datetime
  ,@Param_Attachment tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelegAnhang()
      INSERT INTO [dbo].[ut_BelegAnhang] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[LfdNr]
        ,[DocType]
        ,[ContType]
        ,[Prozessed]
        ,[Stamp]
        ,[OrgName]
        ,[Extension]
        ,[Comment]
        ,[DatumFile]
        ,[DatumScan]
        ,[Attachment]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,@Param_LfdNr
        ,@Param_DocType
        ,@Param_ContType
        ,@Param_Prozessed
        ,UPPER(RTRIM(@Param_Stamp))
        ,RTRIM(@Param_OrgName)
        ,RTRIM(@Param_Extension)
        ,RTRIM(@Param_Comment)
        ,@Param_DatumFile
        ,@Param_DatumScan
        ,@Param_Attachment
          )
      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_BelegAnhang')
  DROP PROCEDURE [dbo].[up_Add_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_Add_BelegAnhang]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BelegAnhang()
  INSERT INTO [dbo].[ut_BelegAnhang] (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_BelegAnhang')
  DROP PROCEDURE [dbo].[up_Upd_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelegAnhang] @mRI integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr integer
  ,@Param_DocType integer
  ,@Param_ContType integer
  ,@Param_Prozessed tinyint
  ,@Param_Stamp char(32)
  ,@Param_OrgName char(250)
  ,@Param_Extension char(10)
  ,@Param_Comment char(50)
  ,@Param_DatumFile datetime
  ,@Param_DatumScan datetime
  ,@Param_Attachment tinyint
AS
  UPDATE [dbo].[ut_BelegAnhang] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = @Param_LfdNr
   ,[DocType] = @Param_DocType
   ,[ContType] = @Param_ContType
   ,[Prozessed] = @Param_Prozessed
   ,[Stamp] = UPPER(RTRIM(@Param_Stamp))
   ,[OrgName] = RTRIM(@Param_OrgName)
   ,[Extension] = RTRIM(@Param_Extension)
   ,[Comment] = RTRIM(@Param_Comment)
   ,[DatumFile] = @Param_DatumFile
   ,[DatumScan] = @Param_DatumScan
   ,[Attachment] = @Param_Attachment
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelegAnhang')
  DROP PROCEDURE [dbo].[up_Upd2_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelegAnhang] @Param_RecId varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_LfdNr varchar(8)
  ,@Param_DocType varchar(2)
  ,@Param_ContType varchar(2)
  ,@Param_Prozessed varchar(1)
  ,@Param_Stamp varchar(32)
  ,@Param_OrgName varchar(250)
  ,@Param_Extension varchar(10)
  ,@Param_Comment varchar(50)
  ,@Param_DatumFile varchar(10)
  ,@Param_DatumScan varchar(10)
  ,@Param_Attachment varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelegAnhang] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[DocType] = dbo.itcfunc_integer(@Param_DocType)
   ,[ContType] = dbo.itcfunc_integer(@Param_ContType)
   ,[Prozessed] = dbo.itcfunc_integer(@Param_Prozessed)
   ,[Stamp] = UPPER(RTRIM(@Param_Stamp))
   ,[OrgName] = RTRIM(@Param_OrgName)
   ,[Extension] = RTRIM(@Param_Extension)
   ,[Comment] = RTRIM(@Param_Comment)
   ,[DatumFile] = dbo.itcfunc_datetime(@Param_DatumFile)
   ,[DatumScan] = dbo.itcfunc_datetime(@Param_DatumScan)
   ,[Attachment] = dbo.itcfunc_integer(@Param_Attachment)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelegAnhang] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BelegAnhang')
  DROP PROCEDURE [dbo].[up_TrSel_BelegAnhang]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelegAnhang]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([Stamp]))
   +LTRIM(RTRIM([OrgName]))
   +LTRIM(RTRIM([Extension]))
   +LTRIM(RTRIM([Comment]))
  AS Data FROM [dbo].[ut_BelegAnhang] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_First_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_BelegAnhang_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelegAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelegAnhang_Key00] AS EXECUTE [dbo].[up_First_BelegAnhang_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_Last_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_BelegAnhang_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelegAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelegAnhang_Key00] AS EXECUTE [dbo].[up_Last_BelegAnhang_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_Next_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_BelegAnhang_Key00] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr > @oldLfdNr)
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,LfdNr,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_BelegAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelegAnhang_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelegAnhang_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelegAnhang_Key00] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr < @oldLfdNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelegAnhang_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelegAnhang_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelegAnhang_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelegAnhang_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,8)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegAnhang_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BelegAnhang_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelegAnhang_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,19,8)

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

  IF @xLen = 26 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr 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_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key00) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_First_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_BelegAnhang_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) ORDER BY BelArt,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelegAnhang_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelegAnhang_Key01] AS EXECUTE [dbo].[up_First_BelegAnhang_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_Last_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_BelegAnhang_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelegAnhang_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelegAnhang_Key01] AS EXECUTE [dbo].[up_Last_BelegAnhang_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_Next_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_BelegAnhang_Key01] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldBelArt = BelArt, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr > @oldLfdNr)
    ORDER BY BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY BelArt,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt > @oldBelArt)
    ORDER BY BelArt,BelNr,LfdNr,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_BelegAnhang_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelegAnhang_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelegAnhang_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_Prior_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelegAnhang_Key01] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldBelArt = BelArt, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (LfdNr < @oldLfdNr)
    ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01)
    WHERE (BelArt < @oldBelArt)
    ORDER BY BelArt desc,BelNr desc,LfdNr 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_BelegAnhang_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelegAnhang_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelegAnhang_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_FindF_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelegAnhang_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,11,8)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      ORDER BY BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt
      ORDER BY BelArt,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY BelArt,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY BelArt,BelNr,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegAnhang_Key01')
  DROP PROCEDURE [dbo].[up_FindL_BelegAnhang_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelegAnhang_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,11,8)

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

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt
      ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key01) WHERE 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY BelArt desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END


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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegAnhang_Key02')
  DROP PROCEDURE [dbo].[up_First_BelegAnhang_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_BelegAnhang_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE (PrjNr = '_DOCPOOL') ORDER BY Stamp,BelArt,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelegAnhang_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelegAnhang_Key02] AS EXECUTE [dbo].[up_First_BelegAnhang_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegAnhang_Key02')
  DROP PROCEDURE [dbo].[up_Last_BelegAnhang_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_BelegAnhang_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE (PrjNr = '_DOCPOOL') ORDER BY Stamp desc,BelArt desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelegAnhang_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelegAnhang_Key02] AS EXECUTE [dbo].[up_Last_BelegAnhang_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegAnhang_Key02')
  DROP PROCEDURE [dbo].[up_Next_BelegAnhang_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_BelegAnhang_Key02] @oldRecId integer AS
  DECLARE @oldStamp char(32)
  DECLARE @oldBelArt char(2)
  SELECT @oldStamp = Stamp, @oldBelArt = BelArt FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp = @oldStamp) AND (BelArt = @oldBelArt) AND (RecId > @oldRecId)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp,BelArt,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp = @oldStamp) AND (BelArt > @oldBelArt)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp,BelArt,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp > @oldStamp)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp,BelArt,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_BelegAnhang_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelegAnhang_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelegAnhang_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegAnhang_Key02')
  DROP PROCEDURE [dbo].[up_Prior_BelegAnhang_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelegAnhang_Key02] @oldRecId integer AS
  DECLARE @oldStamp char(32)
  DECLARE @oldBelArt char(2)
  SELECT @oldStamp = Stamp, @oldBelArt = BelArt FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp = @oldStamp) AND (BelArt = @oldBelArt) AND (RecId < @oldRecId)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp desc,BelArt desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp = @oldStamp) AND (BelArt < @oldBelArt)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp desc,BelArt desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02)
    WHERE ((Stamp < @oldStamp)) AND ((PrjNr = '_DOCPOOL'))
    ORDER BY Stamp desc,BelArt 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_BelegAnhang_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelegAnhang_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelegAnhang_Key02] @oldRecId')
GO

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

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp AND 
      BelArt = @mBelArt
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp,BelArt,RecId
    RETURN
  END

  IF @xLen = 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp,BelArt,RecId
    RETURN
  END

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

  IF @xLen < 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp >= @mStamp AND SUBSTRING(Stamp,1,LEN(@mStamp)) = @mStamp
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp,BelArt,RecId
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp,BelArt,RecId
    RETURN
  END

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp AND 
      BelArt = @mBelArt
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp desc,BelArt desc,RecId desc
    RETURN
  END

  IF @xLen = 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp desc,BelArt desc,RecId desc
    RETURN
  END

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

  IF @xLen < 32 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(Stamp,1,LEN(@mStamp)) = @mStamp
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp desc,BelArt desc,RecId desc
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key02) WHERE 
      Stamp = @mStamp AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((PrjNr = '_DOCPOOL'))
      ORDER BY Stamp desc,BelArt desc,RecId desc
    RETURN
  END

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


-- logical key  ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelegAnhang' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  DROP INDEX [Ik_Key03] ON [dbo].[ut_BelegAnhang]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelegAnhang' and so.[id] = si.[id] and si.[name] = 'Ik_Key03')
  CREATE INDEX [Ik_Key03] ON [dbo].[ut_BelegAnhang] (PrjNr,Prozessed,BelArt,DatumScan,BelNr,LfdNr)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_First_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_First_BelegAnhang_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE (PrjNr = '_DOCPOOL') AND (0 = Prozessed) ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelegAnhang_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelegAnhang_Key03] AS EXECUTE [dbo].[up_First_BelegAnhang_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_Last_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_Last_BelegAnhang_Key03] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE (PrjNr = '_DOCPOOL') AND (0 = Prozessed) ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelegAnhang_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelegAnhang_Key03] AS EXECUTE [dbo].[up_Last_BelegAnhang_Key03]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_Next_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_Next_BelegAnhang_Key03] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldDatumScan datetime
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldBelArt = BelArt, @oldDatumScan = DatumScan, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr = @oldBelNr) AND (LfdNr > @oldLfdNr)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr > @oldBelNr)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan > @oldDatumScan)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt > @oldBelArt)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt,DatumScan,BelNr,LfdNr,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_BelegAnhang_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelegAnhang_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelegAnhang_Key03] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_Prior_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelegAnhang_Key03] @oldRecId integer AS
  DECLARE @oldBelArt char(2)
  DECLARE @oldDatumScan datetime
  DECLARE @oldBelNr char(8)
  DECLARE @oldLfdNr integer
  SELECT @oldBelArt = BelArt, @oldDatumScan = DatumScan, @oldBelNr = BelNr, @oldLfdNr = LfdNr FROM [dbo].[ut_BelegAnhang] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr = @oldBelNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr = @oldBelNr) AND (LfdNr < @oldLfdNr)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan = @oldDatumScan) AND (BelNr < @oldBelNr)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt = @oldBelArt) AND (DatumScan < @oldDatumScan)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03)
    WHERE ((BelArt < @oldBelArt)) AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
    ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr 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_BelegAnhang_Key03')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelegAnhang_Key03] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelegAnhang_Key03] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_FindF_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelegAnhang_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mDatumScan datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,3,10)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,21,8)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      BelNr = @mBelNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END


  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt,DatumScan,BelNr,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelegAnhang_Key03')
  DROP PROCEDURE [dbo].[up_FindL_BelegAnhang_Key03]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelegAnhang_Key03] @xKey char(250), @xLen integer AS
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mDatumScan datetime = dbo.itcfunc_KeyToken_datetime(@xKey,@xLen,3,10)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,13,8)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,21,8)

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

  IF @xLen = 28 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      BelNr = @mBelNr AND 
      LfdNr = @mLfdNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      BelNr = @mBelNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END


  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelegAnhang] WITH(INDEX = Ik_Key03) WHERE 
      BelArt = @mBelArt AND 
      DatumScan = @mDatumScan AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      AND ((PrjNr = '_DOCPOOL') AND (0 = Prozessed))
      ORDER BY BelArt desc,DatumScan desc,BelNr desc,LfdNr desc,RecId desc
    RETURN
  END


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


