-- 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_Menu')
BEGIN
CREATE TABLE [dbo].[ut_Menu]
(
  [RecId]  integer NOT NULL
 ,[MenuId] integer DEFAULT 0
 ,[EntryNo] integer DEFAULT 0
 ,[MenuType] integer DEFAULT 0
 ,[CommandId] integer DEFAULT 0
 ,[Descr] char(64) DEFAULT ''
 ,[Access] char(64) DEFAULT ''
 ,[EventName] char(64) DEFAULT ''
 ,[IconName] char(32) DEFAULT ''
 ,[ToolbarId] integer DEFAULT 0
  CONSTRAINT [Pk_Menu]  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_Menu_KeyDel')
  DROP TRIGGER [dbo].[tr_Menu_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Menu_KeyUpd')
  DROP TRIGGER [dbo].[tr_Menu_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Menu_Keys')
  DROP TABLE [dbo].[ut_Menu_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_Menu' and sc.[name] = 'MenuId')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [MenuId] 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_Menu' and sc.[name] = 'EntryNo')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [EntryNo] 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_Menu' and sc.[name] = 'MenuType')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [MenuType] 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_Menu' and sc.[name] = 'CommandId')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [CommandId] 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_Menu' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [Descr] char(64) 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_Menu' and sc.[name] = 'Access')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [Access] char(64) 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_Menu' and sc.[name] = 'EventName')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [EventName] char(64) 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_Menu' and sc.[name] = 'IconName')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [IconName] char(32) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'ToolbarId')
BEGIN
  ALTER TABLE [dbo].[ut_Menu] ADD [ToolbarId] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (64 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Descr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [Descr] char(64)
END
GO
IF (64 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Descr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [Descr] varchar(64)
END
GO
IF (64 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Access'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Access'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [Access] char(64)
END
GO
IF (64 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Access'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'Access'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [Access] varchar(64)
END
GO
IF (64 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'EventName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'EventName'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [EventName] char(64)
END
GO
IF (64 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'EventName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'EventName'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [EventName] varchar(64)
END
GO
IF (32 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'IconName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'IconName'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [IconName] char(32)
END
GO
IF (32 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'IconName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Menu' and sc.[name] = 'IconName'))
BEGIN
   ALTER TABLE [dbo].[ut_Menu] ALTER COLUMN [IconName] varchar(32)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Menu')
  DROP VIEW [dbo].[Menu]
GO
CREATE VIEW [dbo].[Menu] AS
  SELECT [RecId]
 ,[MenuId]
 ,[EntryNo]
 ,[MenuType]
 ,[CommandId]
 ,[Descr]
 ,[Access]
 ,[EventName]
 ,[IconName]
 ,[ToolbarId]
  FROM [dbo].[ut_Menu]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Menu')
  DROP PROCEDURE [dbo].[up_Sel_Menu]
GO
CREATE PROCEDURE [dbo].[up_Sel_Menu]  @mRI integer
AS
  SELECT
    [MenuId]
   ,[EntryNo]
   ,[MenuType]
   ,[CommandId]
   ,[Descr]
   ,[Access]
   ,[EventName]
   ,[IconName]
   ,[ToolbarId]
  FROM [dbo].[ut_Menu] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Menu')
  DROP PROCEDURE [dbo].[up_Append_Menu]
GO
CREATE PROCEDURE [dbo].[up_Append_Menu]
   @Param_MenuId integer
  ,@Param_EntryNo integer
  ,@Param_MenuType integer
  ,@Param_CommandId integer
  ,@Param_Descr char(64)
  ,@Param_Access char(64)
  ,@Param_EventName char(64)
  ,@Param_IconName char(32)
  ,@Param_ToolbarId integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Menu()
      INSERT INTO [dbo].[ut_Menu] (RecId
        ,[MenuId]
        ,[EntryNo]
        ,[MenuType]
        ,[CommandId]
        ,[Descr]
        ,[Access]
        ,[EventName]
        ,[IconName]
        ,[ToolbarId]
          ) VALUES (@NeueId
        ,@Param_MenuId
        ,@Param_EntryNo
        ,@Param_MenuType
        ,@Param_CommandId
        ,RTRIM(@Param_Descr)
        ,RTRIM(@Param_Access)
        ,RTRIM(@Param_EventName)
        ,RTRIM(@Param_IconName)
        ,@Param_ToolbarId
          )
      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_Menu')
  DROP PROCEDURE [dbo].[up_Add_Menu]
GO
CREATE PROCEDURE [dbo].[up_Add_Menu]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Menu()
  INSERT INTO [dbo].[ut_Menu] (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_Menu')
  DROP PROCEDURE [dbo].[up_Upd_Menu]
GO
CREATE PROCEDURE [dbo].[up_Upd_Menu] @mRI integer
  ,@Param_MenuId integer
  ,@Param_EntryNo integer
  ,@Param_MenuType integer
  ,@Param_CommandId integer
  ,@Param_Descr char(64)
  ,@Param_Access char(64)
  ,@Param_EventName char(64)
  ,@Param_IconName char(32)
  ,@Param_ToolbarId integer
AS
  UPDATE [dbo].[ut_Menu] SET
    [MenuId] = @Param_MenuId
   ,[EntryNo] = @Param_EntryNo
   ,[MenuType] = @Param_MenuType
   ,[CommandId] = @Param_CommandId
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Access] = RTRIM(@Param_Access)
   ,[EventName] = RTRIM(@Param_EventName)
   ,[IconName] = RTRIM(@Param_IconName)
   ,[ToolbarId] = @Param_ToolbarId
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Menu')
  DROP PROCEDURE [dbo].[up_Upd2_Menu]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Menu] @Param_RecId varchar(20)
  ,@Param_MenuId varchar(4)
  ,@Param_EntryNo varchar(4)
  ,@Param_MenuType varchar(1)
  ,@Param_CommandId varchar(5)
  ,@Param_Descr varchar(64)
  ,@Param_Access varchar(64)
  ,@Param_EventName varchar(64)
  ,@Param_IconName varchar(32)
  ,@Param_ToolbarId varchar(2)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Menu] SET
    [MenuId] = dbo.itcfunc_integer(@Param_MenuId)
   ,[EntryNo] = dbo.itcfunc_integer(@Param_EntryNo)
   ,[MenuType] = dbo.itcfunc_integer(@Param_MenuType)
   ,[CommandId] = dbo.itcfunc_integer(@Param_CommandId)
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Access] = RTRIM(@Param_Access)
   ,[EventName] = RTRIM(@Param_EventName)
   ,[IconName] = RTRIM(@Param_IconName)
   ,[ToolbarId] = dbo.itcfunc_integer(@Param_ToolbarId)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Menu] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Menu')
  DROP PROCEDURE [dbo].[up_TrSel_Menu]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Menu]
AS
  SELECT RecId,
    LTRIM(RTRIM([Descr]))
   +LTRIM(RTRIM([Access]))
   +LTRIM(RTRIM([EventName]))
   +LTRIM(RTRIM([IconName]))
  AS Data FROM [dbo].[ut_Menu] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Menu_Key00')
  DROP PROCEDURE [dbo].[up_First_Menu_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Menu_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00) ORDER BY MenuId,EntryNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Menu_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Menu_Key00] AS EXECUTE [dbo].[up_First_Menu_Key00]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Menu_Key00')
  DROP PROCEDURE [dbo].[up_Next_Menu_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Menu_Key00] @oldRecId integer AS
  DECLARE @oldMenuId integer
  DECLARE @oldEntryNo integer
  SELECT @oldMenuId = MenuId, @oldEntryNo = EntryNo FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId = @oldMenuId) AND (EntryNo = @oldEntryNo) AND (RecId > @oldRecId)
    ORDER BY MenuId,EntryNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId = @oldMenuId) AND (EntryNo > @oldEntryNo)
    ORDER BY MenuId,EntryNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId > @oldMenuId)
    ORDER BY MenuId,EntryNo,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_Menu_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Menu_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Menu_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Menu_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Menu_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Menu_Key00] @oldRecId integer AS
  DECLARE @oldMenuId integer
  DECLARE @oldEntryNo integer
  SELECT @oldMenuId = MenuId, @oldEntryNo = EntryNo FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId = @oldMenuId) AND (EntryNo = @oldEntryNo) AND (RecId < @oldRecId)
    ORDER BY MenuId desc,EntryNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId = @oldMenuId) AND (EntryNo < @oldEntryNo)
    ORDER BY MenuId desc,EntryNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00)
    WHERE (MenuId < @oldMenuId)
    ORDER BY MenuId desc,EntryNo 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_Menu_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Menu_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Menu_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00) WHERE 
      MenuId = @mMenuId AND 
      EntryNo = @mEntryNo
      ORDER BY MenuId,EntryNo,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00) WHERE 
      MenuId = @mMenuId
      ORDER BY MenuId,EntryNo,RecId
    RETURN
  END

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



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

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

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

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00) WHERE 
      MenuId = @mMenuId AND 
      EntryNo = @mEntryNo
      ORDER BY MenuId desc,EntryNo desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key00) WHERE 
      MenuId = @mMenuId
      ORDER BY MenuId desc,EntryNo desc,RecId desc
    RETURN
  END

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



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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Menu_Key01')
  DROP PROCEDURE [dbo].[up_First_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_Menu_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) ORDER BY MenuType,MenuId,EntryNo,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Menu_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Menu_Key01] AS EXECUTE [dbo].[up_First_Menu_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Menu_Key01')
  DROP PROCEDURE [dbo].[up_Last_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_Menu_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Menu_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Menu_Key01] AS EXECUTE [dbo].[up_Last_Menu_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Menu_Key01')
  DROP PROCEDURE [dbo].[up_Next_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Menu_Key01] @oldRecId integer AS
  DECLARE @oldMenuType integer
  DECLARE @oldMenuId integer
  DECLARE @oldEntryNo integer
  SELECT @oldMenuType = MenuType, @oldMenuId = MenuId, @oldEntryNo = EntryNo FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId = @oldMenuId) AND (EntryNo = @oldEntryNo) AND (RecId > @oldRecId)
    ORDER BY MenuType,MenuId,EntryNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId = @oldMenuId) AND (EntryNo > @oldEntryNo)
    ORDER BY MenuType,MenuId,EntryNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId > @oldMenuId)
    ORDER BY MenuType,MenuId,EntryNo,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType > @oldMenuType)
    ORDER BY MenuType,MenuId,EntryNo,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_Menu_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Menu_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Menu_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Menu_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Menu_Key01] @oldRecId integer AS
  DECLARE @oldMenuType integer
  DECLARE @oldMenuId integer
  DECLARE @oldEntryNo integer
  SELECT @oldMenuType = MenuType, @oldMenuId = MenuId, @oldEntryNo = EntryNo FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId = @oldMenuId) AND (EntryNo = @oldEntryNo) AND (RecId < @oldRecId)
    ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId = @oldMenuId) AND (EntryNo < @oldEntryNo)
    ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType = @oldMenuType) AND (MenuId < @oldMenuId)
    ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01)
    WHERE (MenuType < @oldMenuType)
    ORDER BY MenuType desc,MenuId desc,EntryNo 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_Menu_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Menu_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Menu_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Menu_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_Menu_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mMenuType integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mMenuId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,4)
  DECLARE @mEntryNo integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,6,4)

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType AND 
      MenuId = @mMenuId AND 
      EntryNo = @mEntryNo
      ORDER BY MenuType,MenuId,EntryNo,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType AND 
      MenuId = @mMenuId
      ORDER BY MenuType,MenuId,EntryNo,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType
      ORDER BY MenuType,MenuId,EntryNo,RecId
    RETURN
  END

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




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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Menu_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Menu_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_Menu_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mMenuType integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,1)
  DECLARE @mMenuId integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,2,4)
  DECLARE @mEntryNo integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,6,4)

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType AND 
      MenuId = @mMenuId AND 
      EntryNo = @mEntryNo
      ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType AND 
      MenuId = @mMenuId
      ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key01) WHERE 
      MenuType = @mMenuType
      ORDER BY MenuType desc,MenuId desc,EntryNo desc,RecId desc
    RETURN
  END

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




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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Menu_Key02')
  DROP PROCEDURE [dbo].[up_First_Menu_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_Menu_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02) ORDER BY ToolbarId,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Menu_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Menu_Key02] AS EXECUTE [dbo].[up_First_Menu_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Menu_Key02')
  DROP PROCEDURE [dbo].[up_Last_Menu_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_Menu_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02) ORDER BY ToolbarId desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Menu_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Menu_Key02] AS EXECUTE [dbo].[up_Last_Menu_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Menu_Key02')
  DROP PROCEDURE [dbo].[up_Next_Menu_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_Menu_Key02] @oldRecId integer AS
  DECLARE @oldToolbarId integer
  SELECT @oldToolbarId = ToolbarId FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02)
    WHERE (ToolbarId = @oldToolbarId) AND (RecId > @oldRecId)
    ORDER BY ToolbarId,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02)
    WHERE (ToolbarId > @oldToolbarId)
    ORDER BY ToolbarId,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_Menu_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Menu_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_Menu_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Menu_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Menu_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_Menu_Key02] @oldRecId integer AS
  DECLARE @oldToolbarId integer
  SELECT @oldToolbarId = ToolbarId FROM [dbo].[ut_Menu] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02)
    WHERE (ToolbarId = @oldToolbarId) AND (RecId < @oldRecId)
    ORDER BY ToolbarId desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02)
    WHERE (ToolbarId < @oldToolbarId)
    ORDER BY ToolbarId 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_Menu_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Menu_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Menu_Key02] @oldRecId')
GO

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

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02) WHERE 
      ToolbarId = @mToolbarId
      ORDER BY ToolbarId,RecId
    RETURN
  END

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


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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Menu] WITH(INDEX = Ik_Key02) WHERE 
      ToolbarId = @mToolbarId
      ORDER BY ToolbarId desc,RecId desc
    RETURN
  END

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


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


