-- 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_RecordZoom')
BEGIN
CREATE TABLE [dbo].[ut_RecordZoom]
(
  [RecId]  integer NOT NULL
 ,[TableName] char(30) DEFAULT ''
 ,[LfdNr] char(4) DEFAULT ''
 ,[FieldNo] integer DEFAULT 0
 ,[DisplayName] char(50) DEFAULT ''
  CONSTRAINT [Pk_RecordZoom]  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_RecordZoom_KeyDel')
  DROP TRIGGER [dbo].[tr_RecordZoom_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RecordZoom_KeyUpd')
  DROP TRIGGER [dbo].[tr_RecordZoom_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RecordZoom_Keys')
  DROP TABLE [dbo].[ut_RecordZoom_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_RecordZoom' and sc.[name] = 'TableName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [TableName] char(30) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [LfdNr] char(4) 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_RecordZoom' and sc.[name] = 'FieldNo')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [FieldNo] 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_RecordZoom' and sc.[name] = 'DisplayName')
BEGIN
  ALTER TABLE [dbo].[ut_RecordZoom] ADD [DisplayName] char(50) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [TableName] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'TableName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [TableName] varchar(30)
END
GO
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [LfdNr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [LfdNr] varchar(4)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'DisplayName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'DisplayName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [DisplayName] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'DisplayName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RecordZoom' and sc.[name] = 'DisplayName'))
BEGIN
   ALTER TABLE [dbo].[ut_RecordZoom] ALTER COLUMN [DisplayName] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RecordZoom')
  DROP VIEW [dbo].[RecordZoom]
GO
CREATE VIEW [dbo].[RecordZoom] AS
  SELECT [RecId]
 ,[TableName]
 ,[LfdNr]
 ,[FieldNo]
 ,[DisplayName]
  FROM [dbo].[ut_RecordZoom]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RecordZoom')
  DROP PROCEDURE [dbo].[up_Sel_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Sel_RecordZoom]  @mRI integer
AS
  SELECT
    [TableName]
   ,[LfdNr]
   ,[FieldNo]
   ,[DisplayName]
  FROM [dbo].[ut_RecordZoom] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RecordZoom')
  DROP PROCEDURE [dbo].[up_Append_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Append_RecordZoom]
   @Param_TableName char(30)
  ,@Param_LfdNr char(4)
  ,@Param_FieldNo integer
  ,@Param_DisplayName char(50)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_RecordZoom()
      INSERT INTO [dbo].[ut_RecordZoom] (RecId
        ,[TableName]
        ,[LfdNr]
        ,[FieldNo]
        ,[DisplayName]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_TableName))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,@Param_FieldNo
        ,RTRIM(@Param_DisplayName)
          )
      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_RecordZoom')
  DROP PROCEDURE [dbo].[up_Add_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Add_RecordZoom]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_RecordZoom()
  INSERT INTO [dbo].[ut_RecordZoom] (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_RecordZoom')
  DROP PROCEDURE [dbo].[up_Upd_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Upd_RecordZoom] @mRI integer
  ,@Param_TableName char(30)
  ,@Param_LfdNr char(4)
  ,@Param_FieldNo integer
  ,@Param_DisplayName char(50)
AS
  UPDATE [dbo].[ut_RecordZoom] SET
    [TableName] = UPPER(RTRIM(@Param_TableName))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[FieldNo] = @Param_FieldNo
   ,[DisplayName] = RTRIM(@Param_DisplayName)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_RecordZoom')
  DROP PROCEDURE [dbo].[up_Upd2_RecordZoom]
GO
CREATE PROCEDURE [dbo].[up_Upd2_RecordZoom] @Param_RecId varchar(20)
  ,@Param_TableName varchar(30)
  ,@Param_LfdNr varchar(4)
  ,@Param_FieldNo varchar(3)
  ,@Param_DisplayName varchar(50)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_RecordZoom] SET
    [TableName] = UPPER(RTRIM(@Param_TableName))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[FieldNo] = dbo.itcfunc_integer(@Param_FieldNo)
   ,[DisplayName] = RTRIM(@Param_DisplayName)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_RecordZoom] @mRI
GO

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

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

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Next_RecordZoom_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_RecordZoom_Key00] @oldRecId integer AS
  DECLARE @oldTableName char(30)
  DECLARE @oldLfdNr char(4)
  SELECT @oldTableName = TableName, @oldLfdNr = LfdNr FROM [dbo].[ut_RecordZoom] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName = @oldTableName) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY TableName,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName = @oldTableName) AND (LfdNr > @oldLfdNr)
    ORDER BY TableName,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName > @oldTableName)
    ORDER BY TableName,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_RecordZoom_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RecordZoom_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_RecordZoom_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RecordZoom_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RecordZoom_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_RecordZoom_Key00] @oldRecId integer AS
  DECLARE @oldTableName char(30)
  DECLARE @oldLfdNr char(4)
  SELECT @oldTableName = TableName, @oldLfdNr = LfdNr FROM [dbo].[ut_RecordZoom] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName = @oldTableName) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY TableName 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_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName = @oldTableName) AND (LfdNr < @oldLfdNr)
    ORDER BY TableName 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_RecordZoom] WITH(INDEX = Ik_Key00)
    WHERE (TableName < @oldTableName)
    ORDER BY TableName 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_RecordZoom_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RecordZoom_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RecordZoom_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName AND 
      LfdNr = @mLfdNr
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName >= @mTableName AND SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY TableName,LfdNr,RecId
    RETURN
  END

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

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

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

  IF @xLen = 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName AND 
      LfdNr = @mLfdNr
      ORDER BY TableName desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName
      ORDER BY TableName desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY TableName desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 34 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key00) WHERE 
      TableName = @mTableName AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY TableName 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_RecordZoom_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_RecordZoom_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_RecordZoom_Key00] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_RecordZoom_Key01')
  DROP PROCEDURE [dbo].[up_First_RecordZoom_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_RecordZoom_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) ORDER BY TableName,DisplayName,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_RecordZoom_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_RecordZoom_Key01] AS EXECUTE [dbo].[up_First_RecordZoom_Key01]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RecordZoom_Key01')
  DROP PROCEDURE [dbo].[up_Next_RecordZoom_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_RecordZoom_Key01] @oldRecId integer AS
  DECLARE @oldTableName char(30)
  DECLARE @oldDisplayName char(50)
  SELECT @oldTableName = TableName, @oldDisplayName = DisplayName FROM [dbo].[ut_RecordZoom] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName = @oldTableName) AND (DisplayName = @oldDisplayName) AND (RecId > @oldRecId)
    ORDER BY TableName,DisplayName,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName = @oldTableName) AND (DisplayName > @oldDisplayName)
    ORDER BY TableName,DisplayName,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName > @oldTableName)
    ORDER BY TableName,DisplayName,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_RecordZoom_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RecordZoom_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_RecordZoom_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RecordZoom_Key01')
  DROP PROCEDURE [dbo].[up_Prior_RecordZoom_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_RecordZoom_Key01] @oldRecId integer AS
  DECLARE @oldTableName char(30)
  DECLARE @oldDisplayName char(50)
  SELECT @oldTableName = TableName, @oldDisplayName = DisplayName FROM [dbo].[ut_RecordZoom] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName = @oldTableName) AND (DisplayName = @oldDisplayName) AND (RecId < @oldRecId)
    ORDER BY TableName desc,DisplayName desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName = @oldTableName) AND (DisplayName < @oldDisplayName)
    ORDER BY TableName desc,DisplayName desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01)
    WHERE (TableName < @oldTableName)
    ORDER BY TableName desc,DisplayName 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_RecordZoom_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RecordZoom_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RecordZoom_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName AND 
      DisplayName = @mDisplayName
      ORDER BY TableName,DisplayName,RecId
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName
      ORDER BY TableName,DisplayName,RecId
    RETURN
  END

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

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName >= @mTableName AND SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY TableName,DisplayName,RecId
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName AND 
      DisplayName >= @mDisplayName AND SUBSTRING(DisplayName,1,LEN(@mDisplayName)) = @mDisplayName
      ORDER BY TableName,DisplayName,RecId
    RETURN
  END

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

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

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

  IF @xLen = 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName AND 
      DisplayName = @mDisplayName
      ORDER BY TableName desc,DisplayName desc,RecId desc
    RETURN
  END

  IF @xLen = 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName
      ORDER BY TableName desc,DisplayName desc,RecId desc
    RETURN
  END

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

  IF @xLen < 30 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(TableName,1,LEN(@mTableName)) = @mTableName
      ORDER BY TableName desc,DisplayName desc,RecId desc
    RETURN
  END

  IF @xLen < 80 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RecordZoom] WITH(INDEX = Ik_Key01) WHERE 
      TableName = @mTableName AND 
      SUBSTRING(DisplayName,1,LEN(@mDisplayName)) = @mDisplayName
      ORDER BY TableName desc,DisplayName desc,RecId desc
    RETURN
  END

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


