-- 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_OptionValue')
BEGIN
CREATE TABLE [dbo].[ut_OptionValue]
(
  [RecId]  integer NOT NULL
 ,[LogName] char(20) DEFAULT ''
 ,[Key1] char(20) DEFAULT ''
 ,[Key2] char(50) DEFAULT ''
 ,[Value10] varchar(10) DEFAULT ''
 ,[Value250] varchar(250) DEFAULT ''
 ,[FileStamp] char(32) DEFAULT ''
  CONSTRAINT [Pk_OptionValue]  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_OptionValue_KeyDel')
  DROP TRIGGER [dbo].[tr_OptionValue_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_OptionValue_KeyUpd')
  DROP TRIGGER [dbo].[tr_OptionValue_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_OptionValue_Keys')
  DROP TABLE [dbo].[ut_OptionValue_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_OptionValue' and sc.[name] = 'LogName')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [LogName] 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_OptionValue' and sc.[name] = 'Key1')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [Key1] 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_OptionValue' and sc.[name] = 'Key2')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [Key2] 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_OptionValue' and sc.[name] = 'Value10')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [Value10] varchar(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_OptionValue' and sc.[name] = 'Value250')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [Value250] varchar(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_OptionValue' and sc.[name] = 'FileStamp')
BEGIN
  ALTER TABLE [dbo].[ut_OptionValue] ADD [FileStamp] char(32) NOT NULL DEFAULT ''
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_OptionValue' and sc.[name] = 'LogName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [LogName] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'LogName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'LogName'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [LogName] varchar(20)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key1'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key1'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Key1] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key1'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key1'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Key1] 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_OptionValue' and sc.[name] = 'Key2'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key2'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Key2] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key2'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Key2'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Key2] varchar(50)
END
GO
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value10'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value10'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Value10] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value10'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value10'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Value10] varchar(10)
END
GO
IF (250 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value250'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value250'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Value250] char(250)
END
GO
IF (250 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value250'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'Value250'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [Value250] varchar(250)
END
GO
IF (32 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'FileStamp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'FileStamp'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [FileStamp] char(32)
END
GO
IF (32 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'FileStamp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_OptionValue' and sc.[name] = 'FileStamp'))
BEGIN
   ALTER TABLE [dbo].[ut_OptionValue] ALTER COLUMN [FileStamp] varchar(32)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'OptionValue')
  DROP VIEW [dbo].[OptionValue]
GO
CREATE VIEW [dbo].[OptionValue] AS
  SELECT [RecId]
 ,[LogName]
 ,[Key1]
 ,[Key2]
 ,[Value10]
 ,[Value250]
 ,[FileStamp]
  FROM [dbo].[ut_OptionValue]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_OptionValue')
  DROP PROCEDURE [dbo].[up_Sel_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_Sel_OptionValue]  @mRI integer
AS
  SELECT
    [LogName]
   ,[Key1]
   ,[Key2]
   ,[Value10]
   ,[Value250]
   ,[FileStamp]
  FROM [dbo].[ut_OptionValue] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_OptionValue')
  DROP PROCEDURE [dbo].[up_Append_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_Append_OptionValue]
   @Param_LogName char(20)
  ,@Param_Key1 char(20)
  ,@Param_Key2 char(50)
  ,@Param_Value10 varchar(10)
  ,@Param_Value250 varchar(250)
  ,@Param_FileStamp char(32)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_OptionValue()
      INSERT INTO [dbo].[ut_OptionValue] (RecId
        ,[LogName]
        ,[Key1]
        ,[Key2]
        ,[Value10]
        ,[Value250]
        ,[FileStamp]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_LogName))
        ,UPPER(RTRIM(@Param_Key1))
        ,UPPER(RTRIM(@Param_Key2))
        ,RTRIM(@Param_Value10)
        ,RTRIM(@Param_Value250)
        ,UPPER(RTRIM(@Param_FileStamp))
          )
      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_OptionValue')
  DROP PROCEDURE [dbo].[up_Add_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_Add_OptionValue]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_OptionValue()
  INSERT INTO [dbo].[ut_OptionValue] (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_OptionValue')
  DROP PROCEDURE [dbo].[up_Upd_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_Upd_OptionValue] @mRI integer
  ,@Param_LogName char(20)
  ,@Param_Key1 char(20)
  ,@Param_Key2 char(50)
  ,@Param_Value10 varchar(10)
  ,@Param_Value250 varchar(250)
  ,@Param_FileStamp char(32)
AS
  UPDATE [dbo].[ut_OptionValue] SET
    [LogName] = UPPER(RTRIM(@Param_LogName))
   ,[Key1] = UPPER(RTRIM(@Param_Key1))
   ,[Key2] = UPPER(RTRIM(@Param_Key2))
   ,[Value10] = RTRIM(@Param_Value10)
   ,[Value250] = RTRIM(@Param_Value250)
   ,[FileStamp] = UPPER(RTRIM(@Param_FileStamp))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_OptionValue')
  DROP PROCEDURE [dbo].[up_Upd2_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_Upd2_OptionValue] @Param_RecId varchar(20)
  ,@Param_LogName varchar(20)
  ,@Param_Key1 varchar(20)
  ,@Param_Key2 varchar(50)
  ,@Param_Value10 varchar(10)
  ,@Param_Value250 varchar(250)
  ,@Param_FileStamp varchar(32)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_OptionValue] SET
    [LogName] = UPPER(RTRIM(@Param_LogName))
   ,[Key1] = UPPER(RTRIM(@Param_Key1))
   ,[Key2] = UPPER(RTRIM(@Param_Key2))
   ,[Value10] = RTRIM(@Param_Value10)
   ,[Value250] = RTRIM(@Param_Value250)
   ,[FileStamp] = UPPER(RTRIM(@Param_FileStamp))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_OptionValue] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_OptionValue')
  DROP PROCEDURE [dbo].[up_TrSel_OptionValue]
GO
CREATE PROCEDURE [dbo].[up_TrSel_OptionValue]
AS
  SELECT RecId,
    LTRIM(RTRIM([LogName]))
   +LTRIM(RTRIM([Key1]))
   +LTRIM(RTRIM([Key2]))
   +LTRIM(RTRIM([Value10]))
   +LTRIM(RTRIM([Value250]))
   +LTRIM(RTRIM([FileStamp]))
  AS Data FROM [dbo].[ut_OptionValue] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_OptionValue_Key00')
  DROP PROCEDURE [dbo].[up_First_OptionValue_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_OptionValue_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) ORDER BY LogName,Key1,Key2,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_OptionValue_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_OptionValue_Key00] AS EXECUTE [dbo].[up_First_OptionValue_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_OptionValue_Key00')
  DROP PROCEDURE [dbo].[up_Next_OptionValue_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_OptionValue_Key00] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldKey1 char(20)
  DECLARE @oldKey2 char(50)
  SELECT @oldLogName = LogName, @oldKey1 = Key1, @oldKey2 = Key2 FROM [dbo].[ut_OptionValue] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 = @oldKey1) AND (Key2 = @oldKey2) AND (RecId > @oldRecId)
    ORDER BY LogName,Key1,Key2,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 = @oldKey1) AND (Key2 > @oldKey2)
    ORDER BY LogName,Key1,Key2,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 > @oldKey1)
    ORDER BY LogName,Key1,Key2,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName > @oldLogName)
    ORDER BY LogName,Key1,Key2,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_OptionValue_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_OptionValue_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_OptionValue_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_OptionValue_Key00')
  DROP PROCEDURE [dbo].[up_Prior_OptionValue_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_OptionValue_Key00] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldKey1 char(20)
  DECLARE @oldKey2 char(50)
  SELECT @oldLogName = LogName, @oldKey1 = Key1, @oldKey2 = Key2 FROM [dbo].[ut_OptionValue] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 = @oldKey1) AND (Key2 = @oldKey2) AND (RecId < @oldRecId)
    ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 = @oldKey1) AND (Key2 < @oldKey2)
    ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName = @oldLogName) AND (Key1 < @oldKey1)
    ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00)
    WHERE (LogName < @oldLogName)
    ORDER BY LogName desc,Key1 desc,Key2 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_OptionValue_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_OptionValue_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_OptionValue_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_OptionValue_Key00')
  DROP PROCEDURE [dbo].[up_FindF_OptionValue_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_OptionValue_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mLogName char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mKey1 char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,20)
  DECLARE @mKey2 char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,41,50)

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

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1 AND 
      Key2 = @mKey2
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName >= @mLogName AND SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 >= @mKey1 AND SUBSTRING(Key1,1,LEN(@mKey1)) = @mKey1
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1 AND 
      Key2 >= @mKey2 AND SUBSTRING(Key2,1,LEN(@mKey2)) = @mKey2
      ORDER BY LogName,Key1,Key2,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_OptionValue_Key00')
  DROP PROCEDURE [dbo].[up_FindL_OptionValue_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_OptionValue_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mLogName char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,20)
  DECLARE @mKey1 char(20) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,20)
  DECLARE @mKey2 char(50) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,41,50)

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

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1 AND 
      Key2 = @mKey2
      ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
    RETURN
  END

  IF @xLen = 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1
      ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName
      ORDER BY LogName desc,Key1 desc,Key2 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_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
    RETURN
  END

  IF @xLen < 40 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      SUBSTRING(Key1,1,LEN(@mKey1)) = @mKey1
      ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key00) WHERE 
      LogName = @mLogName AND 
      Key1 = @mKey1 AND 
      SUBSTRING(Key2,1,LEN(@mKey2)) = @mKey2
      ORDER BY LogName desc,Key1 desc,Key2 desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_OptionValue_Key01')
  DROP PROCEDURE [dbo].[up_First_OptionValue_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_OptionValue_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) ORDER BY LogName,Key2,Key1,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_OptionValue_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_OptionValue_Key01] AS EXECUTE [dbo].[up_First_OptionValue_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_OptionValue_Key01')
  DROP PROCEDURE [dbo].[up_Last_OptionValue_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_OptionValue_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_OptionValue_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_OptionValue_Key01] AS EXECUTE [dbo].[up_Last_OptionValue_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_OptionValue_Key01')
  DROP PROCEDURE [dbo].[up_Next_OptionValue_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_OptionValue_Key01] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldKey2 char(50)
  DECLARE @oldKey1 char(20)
  SELECT @oldLogName = LogName, @oldKey2 = Key2, @oldKey1 = Key1 FROM [dbo].[ut_OptionValue] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 = @oldKey2) AND (Key1 = @oldKey1) AND (RecId > @oldRecId)
    ORDER BY LogName,Key2,Key1,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 = @oldKey2) AND (Key1 > @oldKey1)
    ORDER BY LogName,Key2,Key1,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 > @oldKey2)
    ORDER BY LogName,Key2,Key1,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName > @oldLogName)
    ORDER BY LogName,Key2,Key1,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_OptionValue_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_OptionValue_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_OptionValue_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_OptionValue_Key01')
  DROP PROCEDURE [dbo].[up_Prior_OptionValue_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_OptionValue_Key01] @oldRecId integer AS
  DECLARE @oldLogName char(20)
  DECLARE @oldKey2 char(50)
  DECLARE @oldKey1 char(20)
  SELECT @oldLogName = LogName, @oldKey2 = Key2, @oldKey1 = Key1 FROM [dbo].[ut_OptionValue] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 = @oldKey2) AND (Key1 = @oldKey1) AND (RecId < @oldRecId)
    ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 = @oldKey2) AND (Key1 < @oldKey1)
    ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName = @oldLogName) AND (Key2 < @oldKey2)
    ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01)
    WHERE (LogName < @oldLogName)
    ORDER BY LogName desc,Key2 desc,Key1 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_OptionValue_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_OptionValue_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_OptionValue_Key01] @oldRecId')
GO

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

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

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

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

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2 AND 
      Key1 = @mKey1
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName >= @mLogName AND SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 >= @mKey2 AND SUBSTRING(Key2,1,LEN(@mKey2)) = @mKey2
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2 AND 
      Key1 >= @mKey1 AND SUBSTRING(Key1,1,LEN(@mKey1)) = @mKey1
      ORDER BY LogName,Key2,Key1,RecId
    RETURN
  END

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

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

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

  IF @xLen = 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2 AND 
      Key1 = @mKey1
      ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
    RETURN
  END

  IF @xLen = 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2
      ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName
      ORDER BY LogName desc,Key2 desc,Key1 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_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(LogName,1,LEN(@mLogName)) = @mLogName
      ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
    RETURN
  END

  IF @xLen < 70 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      SUBSTRING(Key2,1,LEN(@mKey2)) = @mKey2
      ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
    RETURN
  END

  IF @xLen < 90 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_OptionValue] WITH(INDEX = Ik_Key01) WHERE 
      LogName = @mLogName AND 
      Key2 = @mKey2 AND 
      SUBSTRING(Key1,1,LEN(@mKey1)) = @mKey1
      ORDER BY LogName desc,Key2 desc,Key1 desc,RecId desc
    RETURN
  END

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


