-- 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_RecentFiles')
BEGIN
CREATE TABLE [dbo].[ut_RecentFiles]
(
  [RecId]  integer NOT NULL
 ,[Computer] char(20) DEFAULT ''
 ,[LoginName] char(50) DEFAULT ''
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
  CONSTRAINT [Pk_RecentFiles]  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_RecentFiles_KeyDel')
  DROP TRIGGER [dbo].[tr_RecentFiles_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RecentFiles_KeyUpd')
  DROP TRIGGER [dbo].[tr_RecentFiles_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RecentFiles_Keys')
  DROP TABLE [dbo].[ut_RecentFiles_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_RecentFiles' and sc.[name] = 'Computer')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] ADD [Computer] char(20) 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_RecentFiles' and sc.[name] = 'LoginName')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] ADD [LoginName] 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_RecentFiles' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecentFiles] ADD [LfdNr] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'Computer'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'Computer'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] ALTER COLUMN [Computer] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'Computer'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'Computer'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] ALTER COLUMN [Computer] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'LoginName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'LoginName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] ALTER COLUMN [LoginName] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'LoginName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' and sc.[name] = 'LoginName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] ALTER COLUMN [LoginName] varchar(50)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecentFiles' 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_RecentFiles' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' 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_RecentFiles' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' 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_RecentFiles' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' 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_RecentFiles' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' 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_RecentFiles' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] 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_RecentFiles' 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_RecentFiles' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecentFiles] ALTER COLUMN [BelNr] varchar(8)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RecentFiles')
  DROP VIEW [dbo].[RecentFiles]
GO
CREATE VIEW [dbo].[RecentFiles] AS
  SELECT [RecId]
 ,[Computer]
 ,[LoginName]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[LfdNr]
  FROM [dbo].[ut_RecentFiles]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RecentFiles')
  DROP PROCEDURE [dbo].[up_Sel_RecentFiles]
GO
CREATE PROCEDURE [dbo].[up_Sel_RecentFiles]  @mRI integer
AS
  SELECT
    [Computer]
   ,[LoginName]
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[LfdNr]
  FROM [dbo].[ut_RecentFiles] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RecentFiles')
  DROP PROCEDURE [dbo].[up_Append_RecentFiles]
GO
CREATE PROCEDURE [dbo].[up_Append_RecentFiles]
   @Param_Computer char(20)
  ,@Param_LoginName char(50)
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_RecentFiles()
      INSERT INTO [dbo].[ut_RecentFiles] (RecId
        ,[Computer]
        ,[LoginName]
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[LfdNr]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Computer))
        ,UPPER(RTRIM(@Param_LoginName))
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,@Param_LfdNr
          )
      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_RecentFiles')
  DROP PROCEDURE [dbo].[up_Add_RecentFiles]
GO
CREATE PROCEDURE [dbo].[up_Add_RecentFiles]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_RecentFiles()
  INSERT INTO [dbo].[ut_RecentFiles] (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_RecentFiles')
  DROP PROCEDURE [dbo].[up_Upd_RecentFiles]
GO
CREATE PROCEDURE [dbo].[up_Upd_RecentFiles] @mRI integer
  ,@Param_Computer char(20)
  ,@Param_LoginName char(50)
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_LfdNr integer
AS
  UPDATE [dbo].[ut_RecentFiles] SET
    [Computer] = UPPER(RTRIM(@Param_Computer))
   ,[LoginName] = UPPER(RTRIM(@Param_LoginName))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = @Param_LfdNr
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_RecentFiles')
  DROP PROCEDURE [dbo].[up_Upd2_RecentFiles]
GO
CREATE PROCEDURE [dbo].[up_Upd2_RecentFiles] @Param_RecId varchar(20)
  ,@Param_Computer varchar(20)
  ,@Param_LoginName varchar(50)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_LfdNr varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_RecentFiles] SET
    [Computer] = UPPER(RTRIM(@Param_Computer))
   ,[LoginName] = UPPER(RTRIM(@Param_LoginName))
   ,[PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_RecentFiles] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RecentFiles_Key00')
  DROP PROCEDURE [dbo].[up_Next_RecentFiles_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_RecentFiles_Key00] @oldRecId integer AS
  DECLARE @oldComputer char(20)
  DECLARE @oldLoginName char(50)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  SELECT @oldComputer = Computer, @oldLoginName = LoginName, @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr FROM [dbo].[ut_RecentFiles] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00)
    WHERE (Computer = @oldComputer) AND (LoginName > @oldLoginName)
    ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00)
    WHERE (Computer > @oldComputer)
    ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,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_RecentFiles_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RecentFiles_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_RecentFiles_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RecentFiles_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RecentFiles_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_RecentFiles_Key00] @oldRecId integer AS
  DECLARE @oldComputer char(20)
  DECLARE @oldLoginName char(50)
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  SELECT @oldComputer = Computer, @oldLoginName = LoginName, @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr FROM [dbo].[ut_RecentFiles] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

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

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

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00)
    WHERE (Computer = @oldComputer) AND (LoginName < @oldLoginName)
    ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00)
    WHERE (Computer < @oldComputer)
    ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr 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_RecentFiles_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RecentFiles_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RecentFiles_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RecentFiles_Key00')
  DROP PROCEDURE [dbo].[up_FindF_RecentFiles_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_RecentFiles_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mComputer char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mLoginName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,50)
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,71,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,79,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,81,8)

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

  IF @xLen = 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer >= @mComputer AND SUBSTRING(Computer,1,LEN(@mComputer)) = @mComputer
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName >= @mLoginName AND SUBSTRING(LoginName,1,LEN(@mLoginName)) = @mLoginName
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY Computer,LoginName,PrjNr,BelArt,BelNr,RecId
    RETURN
  END

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RecentFiles_Key00')
  DROP PROCEDURE [dbo].[up_FindL_RecentFiles_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_RecentFiles_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mComputer char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mLoginName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,50)
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,71,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,79,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,81,8)

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

  IF @xLen = 88 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Computer,1,LEN(@mComputer)) = @mComputer
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      SUBSTRING(LoginName,1,LEN(@mLoginName)) = @mLoginName
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key00) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY Computer desc,LoginName desc,PrjNr desc,BelArt desc,BelNr desc,RecId desc
    RETURN
  END

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

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


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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RecentFiles_Key01')
  DROP PROCEDURE [dbo].[up_Next_RecentFiles_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_RecentFiles_Key01] @oldRecId integer AS
  DECLARE @oldComputer char(20)
  DECLARE @oldLoginName char(50)
  DECLARE @oldLfdNr integer
  SELECT @oldComputer = Computer, @oldLoginName = LoginName, @oldLfdNr = LfdNr FROM [dbo].[ut_RecentFiles] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName = @oldLoginName) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY Computer,LoginName,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName = @oldLoginName) AND (LfdNr > @oldLfdNr)
    ORDER BY Computer,LoginName,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName > @oldLoginName)
    ORDER BY Computer,LoginName,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer > @oldComputer)
    ORDER BY Computer,LoginName,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_RecentFiles_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RecentFiles_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_RecentFiles_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RecentFiles_Key01')
  DROP PROCEDURE [dbo].[up_Prior_RecentFiles_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_RecentFiles_Key01] @oldRecId integer AS
  DECLARE @oldComputer char(20)
  DECLARE @oldLoginName char(50)
  DECLARE @oldLfdNr integer
  SELECT @oldComputer = Computer, @oldLoginName = LoginName, @oldLfdNr = LfdNr FROM [dbo].[ut_RecentFiles] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName = @oldLoginName) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY Computer desc,LoginName 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_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName = @oldLoginName) AND (LfdNr < @oldLfdNr)
    ORDER BY Computer desc,LoginName 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_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer = @oldComputer) AND (LoginName < @oldLoginName)
    ORDER BY Computer desc,LoginName 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_RecentFiles] WITH(INDEX = Ik_Key01)
    WHERE (Computer < @oldComputer)
    ORDER BY Computer desc,LoginName 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_RecentFiles_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RecentFiles_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RecentFiles_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RecentFiles_Key01')
  DROP PROCEDURE [dbo].[up_FindF_RecentFiles_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_RecentFiles_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mComputer char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mLoginName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,50)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,71,8)

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

  IF @xLen = 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      LfdNr = @mLfdNr
      ORDER BY Computer,LoginName,LfdNr,RecId
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName
      ORDER BY Computer,LoginName,LfdNr,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer
      ORDER BY Computer,LoginName,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer >= @mComputer AND SUBSTRING(Computer,1,LEN(@mComputer)) = @mComputer
      ORDER BY Computer,LoginName,LfdNr,RecId
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      LoginName >= @mLoginName AND SUBSTRING(LoginName,1,LEN(@mLoginName)) = @mLoginName
      ORDER BY Computer,LoginName,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RecentFiles_Key01')
  DROP PROCEDURE [dbo].[up_FindL_RecentFiles_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_RecentFiles_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mComputer char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mLoginName char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,50)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,71,8)

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

  IF @xLen = 78 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName AND 
      LfdNr = @mLfdNr
      ORDER BY Computer desc,LoginName desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      LoginName = @mLoginName
      ORDER BY Computer desc,LoginName desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer
      ORDER BY Computer desc,LoginName desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(Computer,1,LEN(@mComputer)) = @mComputer
      ORDER BY Computer desc,LoginName desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecentFiles] WITH(INDEX = Ik_Key01) WHERE 
      Computer = @mComputer AND 
      SUBSTRING(LoginName,1,LEN(@mLoginName)) = @mLoginName
      ORDER BY Computer desc,LoginName 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_RecentFiles_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_RecentFiles_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_RecentFiles_Key01] @xKey, @xLen')
GO


