-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2025, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Optionen')
BEGIN
CREATE TABLE [dbo].[ut_Optionen]
(
  [RecId]  integer NOT NULL
 ,[OptId] char(5) DEFAULT ''
 ,[OptDataC] char(50) DEFAULT ''
 ,[OptDataI] integer DEFAULT 0
 ,[OptDataD] float DEFAULT 0
 ,[OptGroup] char(20) DEFAULT ''
 ,[OptName] char(20) DEFAULT ''
  CONSTRAINT [Pk_Optionen]  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_Optionen_KeyDel')
  DROP TRIGGER [dbo].[tr_Optionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Optionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Optionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Optionen_Keys')
  DROP TABLE [dbo].[ut_Optionen_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_Optionen' and sc.[name] = 'OptId')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptId] char(5) 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_Optionen' and sc.[name] = 'OptDataC')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptDataC] 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_Optionen' and sc.[name] = 'OptDataI')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptDataI] 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_Optionen' and sc.[name] = 'OptDataD')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptDataD] float 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_Optionen' and sc.[name] = 'OptGroup')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptGroup] 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_Optionen' and sc.[name] = 'OptName')
BEGIN
  ALTER TABLE [dbo].[ut_Optionen] ADD [OptName] char(20) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (5 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptId'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptId'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptId] char(5)
END
GO
IF (5 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptId'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptId'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptId] varchar(5)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptDataC'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptDataC'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptDataC] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptDataC'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptDataC'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptDataC] varchar(50)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptGroup'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptGroup'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptGroup] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptGroup'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptGroup'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptGroup] 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_Optionen' and sc.[name] = 'OptName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptName'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptName] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Optionen' and sc.[name] = 'OptName'))
BEGIN
   ALTER TABLE [dbo].[ut_Optionen] ALTER COLUMN [OptName] varchar(20)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Optionen')
  DROP VIEW [dbo].[Optionen]
GO
CREATE VIEW [dbo].[Optionen] AS
  SELECT [RecId]
 ,[OptId]
 ,[OptDataC]
 ,[OptDataI]
 ,[OptDataD]
 ,[OptGroup]
 ,[OptName]
  FROM [dbo].[ut_Optionen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Optionen')
  DROP PROCEDURE [dbo].[up_Sel_Optionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Optionen]  @mRI integer
AS
  SELECT
    [OptId]
   ,[OptDataC]
   ,[OptDataI]
   ,[OptDataD]
   ,[OptGroup]
   ,[OptName]
  FROM [dbo].[ut_Optionen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Optionen')
  DROP PROCEDURE [dbo].[up_Append_Optionen]
GO
CREATE PROCEDURE [dbo].[up_Append_Optionen]
   @Param_OptId char(5)
  ,@Param_OptDataC char(50)
  ,@Param_OptDataI integer
  ,@Param_OptDataD float
  ,@Param_OptGroup char(20)
  ,@Param_OptName char(20)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Optionen()
      INSERT INTO [dbo].[ut_Optionen] (RecId
        ,[OptId]
        ,[OptDataC]
        ,[OptDataI]
        ,[OptDataD]
        ,[OptGroup]
        ,[OptName]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_OptId))
        ,RTRIM(@Param_OptDataC)
        ,@Param_OptDataI
        ,@Param_OptDataD
        ,RTRIM(@Param_OptGroup)
        ,RTRIM(@Param_OptName)
          )
      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_Optionen')
  DROP PROCEDURE [dbo].[up_Add_Optionen]
GO
CREATE PROCEDURE [dbo].[up_Add_Optionen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Optionen()
  INSERT INTO [dbo].[ut_Optionen] (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_Optionen')
  DROP PROCEDURE [dbo].[up_Upd_Optionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Optionen] @mRI integer
  ,@Param_OptId char(5)
  ,@Param_OptDataC char(50)
  ,@Param_OptDataI integer
  ,@Param_OptDataD float
  ,@Param_OptGroup char(20)
  ,@Param_OptName char(20)
AS
  UPDATE [dbo].[ut_Optionen] SET
    [OptId] = UPPER(RTRIM(@Param_OptId))
   ,[OptDataC] = RTRIM(@Param_OptDataC)
   ,[OptDataI] = @Param_OptDataI
   ,[OptDataD] = @Param_OptDataD
   ,[OptGroup] = RTRIM(@Param_OptGroup)
   ,[OptName] = RTRIM(@Param_OptName)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Optionen')
  DROP PROCEDURE [dbo].[up_Upd2_Optionen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Optionen] @Param_RecId varchar(20)
  ,@Param_OptId varchar(5)
  ,@Param_OptDataC varchar(50)
  ,@Param_OptDataI varchar(8)
  ,@Param_OptDataD varchar(14)
  ,@Param_OptGroup varchar(20)
  ,@Param_OptName varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Optionen] SET
    [OptId] = UPPER(RTRIM(@Param_OptId))
   ,[OptDataC] = RTRIM(@Param_OptDataC)
   ,[OptDataI] = dbo.itcfunc_integer(@Param_OptDataI)
   ,[OptDataD] = dbo.itcfunc_float(@Param_OptDataD)
   ,[OptGroup] = RTRIM(@Param_OptGroup)
   ,[OptName] = RTRIM(@Param_OptName)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Optionen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Optionen')
  DROP PROCEDURE [dbo].[up_TrSel_Optionen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Optionen]
AS
  SELECT RecId,
    LTRIM(RTRIM([OptId]))
   +LTRIM(RTRIM([OptDataC]))
   +LTRIM(RTRIM([OptGroup]))
   +LTRIM(RTRIM([OptName]))
  AS Data FROM [dbo].[ut_Optionen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Optionen_Primary')
  DROP PROCEDURE [dbo].[up_First_Optionen_Primary]
GO
CREATE PROCEDURE [dbo].[up_First_Optionen_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) ORDER BY OptId,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Optionen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Optionen_Primary] AS EXECUTE [dbo].[up_First_Optionen_Primary]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Optionen_Primary')
  DROP PROCEDURE [dbo].[up_Last_Optionen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Last_Optionen_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) ORDER BY OptId desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Optionen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Optionen_Primary] AS EXECUTE [dbo].[up_Last_Optionen_Primary]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Optionen_Primary')
  DROP PROCEDURE [dbo].[up_Next_Optionen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Next_Optionen_Primary] @oldRecId integer AS
  DECLARE @oldOptId char(5)
  SELECT @oldOptId = OptId FROM [dbo].[ut_Optionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary)
    WHERE (OptId > @oldOptId)
    ORDER BY OptId,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_Optionen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Optionen_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Next_Optionen_Primary] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Optionen_Primary')
  DROP PROCEDURE [dbo].[up_Prior_Optionen_Primary]
GO
CREATE PROCEDURE [dbo].[up_Prior_Optionen_Primary] @oldRecId integer AS
  DECLARE @oldOptId char(5)
  SELECT @oldOptId = OptId FROM [dbo].[ut_Optionen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary)
    WHERE (OptId < @oldOptId)
    ORDER BY OptId 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_Optionen_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Optionen_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Optionen_Primary] @oldRecId')
GO

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

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

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

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

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) WHERE 
      OptId = @mOptId
      ORDER BY OptId,RecId
    RETURN
  END

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

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) WHERE 
      OptId >= @mOptId AND SUBSTRING(OptId,1,LEN(@mOptId)) = @mOptId
      ORDER BY OptId,RecId
    RETURN
  END

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

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

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

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) WHERE 
      OptId = @mOptId
      ORDER BY OptId desc,RecId desc
    RETURN
  END

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

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Optionen] WITH(INDEX = Ik_Primary) WHERE 
      SUBSTRING(OptId,1,LEN(@mOptId)) = @mOptId
      ORDER BY OptId desc,RecId desc
    RETURN
  END

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


