-- 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_CodeUnits')
BEGIN
CREATE TABLE [dbo].[ut_CodeUnits]
(
  [RecId]  integer NOT NULL
 ,[Nr] integer DEFAULT 0
 ,[Version] char(10) DEFAULT ''
 ,[LastEdit] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
 ,[Author] char(20) DEFAULT ''
 ,[Modify] tinyint DEFAULT 0
 ,[Descr] char(50) DEFAULT ''
 ,[UserBit] integer DEFAULT 0
 ,[BshMin] integer DEFAULT 0
 ,[BshBit] integer DEFAULT 0
 ,[DefTarged] char(30) DEFAULT ''
 ,[MenuName] char(30) DEFAULT ''
  CONSTRAINT [Pk_CodeUnits]  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_CodeUnits_KeyDel')
  DROP TRIGGER [dbo].[tr_CodeUnits_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_CodeUnits_KeyUpd')
  DROP TRIGGER [dbo].[tr_CodeUnits_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_CodeUnits_Keys')
  DROP TABLE [dbo].[ut_CodeUnits_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_CodeUnits' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [Nr] 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_CodeUnits' and sc.[name] = 'Version')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [Version] char(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_CodeUnits' and sc.[name] = 'LastEdit')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [LastEdit] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Author')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [Author] 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_CodeUnits' and sc.[name] = 'Modify')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [Modify] tinyint 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_CodeUnits' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [Descr] 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_CodeUnits' and sc.[name] = 'UserBit')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [UserBit] 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_CodeUnits' and sc.[name] = 'BshMin')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [BshMin] 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_CodeUnits' and sc.[name] = 'BshBit')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [BshBit] 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_CodeUnits' and sc.[name] = 'DefTarged')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [DefTarged] 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_CodeUnits' and sc.[name] = 'MenuName')
BEGIN
  ALTER TABLE [dbo].[ut_CodeUnits] ADD [MenuName] char(30) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Version'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Version'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Version] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Version'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Version'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Version] varchar(10)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Author'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Author'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Author] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Author'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'Author'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Author] 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_CodeUnits' 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_CodeUnits' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Descr] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' 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_CodeUnits' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [Descr] varchar(50)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'DefTarged'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'DefTarged'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [DefTarged] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'DefTarged'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'DefTarged'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [DefTarged] varchar(30)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'MenuName'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'MenuName'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [MenuName] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'MenuName'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_CodeUnits' and sc.[name] = 'MenuName'))
BEGIN
   ALTER TABLE [dbo].[ut_CodeUnits] ALTER COLUMN [MenuName] varchar(30)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'CodeUnits')
  DROP VIEW [dbo].[CodeUnits]
GO
CREATE VIEW [dbo].[CodeUnits] AS
  SELECT [RecId]
 ,[Nr]
 ,[Version]
 ,[LastEdit]
 ,[Author]
 ,[Modify]
 ,[Descr]
 ,[UserBit]
 ,[BshMin]
 ,[BshBit]
 ,[DefTarged]
 ,[MenuName]
  FROM [dbo].[ut_CodeUnits]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_CodeUnits')
  DROP PROCEDURE [dbo].[up_Sel_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_Sel_CodeUnits]  @mRI integer
AS
  SELECT
    [Nr]
   ,[Version]
   ,[LastEdit]
   ,[Author]
   ,[Modify]
   ,[Descr]
   ,[UserBit]
   ,[BshMin]
   ,[BshBit]
   ,[DefTarged]
   ,[MenuName]
  FROM [dbo].[ut_CodeUnits] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_CodeUnits')
  DROP PROCEDURE [dbo].[up_Append_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_Append_CodeUnits]
   @Param_Nr integer
  ,@Param_Version char(10)
  ,@Param_LastEdit datetime
  ,@Param_Author char(20)
  ,@Param_Modify tinyint
  ,@Param_Descr char(50)
  ,@Param_UserBit integer
  ,@Param_BshMin integer
  ,@Param_BshBit integer
  ,@Param_DefTarged char(30)
  ,@Param_MenuName char(30)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_CodeUnits()
      INSERT INTO [dbo].[ut_CodeUnits] (RecId
        ,[Nr]
        ,[Version]
        ,[LastEdit]
        ,[Author]
        ,[Modify]
        ,[Descr]
        ,[UserBit]
        ,[BshMin]
        ,[BshBit]
        ,[DefTarged]
        ,[MenuName]
          ) VALUES (@NeueId
        ,@Param_Nr
        ,UPPER(RTRIM(@Param_Version))
        ,@Param_LastEdit
        ,RTRIM(@Param_Author)
        ,@Param_Modify
        ,RTRIM(@Param_Descr)
        ,@Param_UserBit
        ,@Param_BshMin
        ,@Param_BshBit
        ,RTRIM(@Param_DefTarged)
        ,RTRIM(@Param_MenuName)
          )
      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_CodeUnits')
  DROP PROCEDURE [dbo].[up_Add_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_Add_CodeUnits]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_CodeUnits()
  INSERT INTO [dbo].[ut_CodeUnits] (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_CodeUnits')
  DROP PROCEDURE [dbo].[up_Upd_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_Upd_CodeUnits] @mRI integer
  ,@Param_Nr integer
  ,@Param_Version char(10)
  ,@Param_LastEdit datetime
  ,@Param_Author char(20)
  ,@Param_Modify tinyint
  ,@Param_Descr char(50)
  ,@Param_UserBit integer
  ,@Param_BshMin integer
  ,@Param_BshBit integer
  ,@Param_DefTarged char(30)
  ,@Param_MenuName char(30)
AS
  UPDATE [dbo].[ut_CodeUnits] SET
    [Nr] = @Param_Nr
   ,[Version] = UPPER(RTRIM(@Param_Version))
   ,[LastEdit] = @Param_LastEdit
   ,[Author] = RTRIM(@Param_Author)
   ,[Modify] = @Param_Modify
   ,[Descr] = RTRIM(@Param_Descr)
   ,[UserBit] = @Param_UserBit
   ,[BshMin] = @Param_BshMin
   ,[BshBit] = @Param_BshBit
   ,[DefTarged] = RTRIM(@Param_DefTarged)
   ,[MenuName] = RTRIM(@Param_MenuName)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_CodeUnits')
  DROP PROCEDURE [dbo].[up_Upd2_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_Upd2_CodeUnits] @Param_RecId varchar(20)
  ,@Param_Nr varchar(6)
  ,@Param_Version varchar(10)
  ,@Param_LastEdit varchar(10)
  ,@Param_Author varchar(20)
  ,@Param_Modify varchar(1)
  ,@Param_Descr varchar(50)
  ,@Param_UserBit varchar(2)
  ,@Param_BshMin varchar(3)
  ,@Param_BshBit varchar(2)
  ,@Param_DefTarged varchar(30)
  ,@Param_MenuName varchar(30)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_CodeUnits] SET
    [Nr] = dbo.itcfunc_integer(@Param_Nr)
   ,[Version] = UPPER(RTRIM(@Param_Version))
   ,[LastEdit] = dbo.itcfunc_datetime(@Param_LastEdit)
   ,[Author] = RTRIM(@Param_Author)
   ,[Modify] = dbo.itcfunc_integer(@Param_Modify)
   ,[Descr] = RTRIM(@Param_Descr)
   ,[UserBit] = dbo.itcfunc_integer(@Param_UserBit)
   ,[BshMin] = dbo.itcfunc_integer(@Param_BshMin)
   ,[BshBit] = dbo.itcfunc_integer(@Param_BshBit)
   ,[DefTarged] = RTRIM(@Param_DefTarged)
   ,[MenuName] = RTRIM(@Param_MenuName)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_CodeUnits] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_CodeUnits')
  DROP PROCEDURE [dbo].[up_TrSel_CodeUnits]
GO
CREATE PROCEDURE [dbo].[up_TrSel_CodeUnits]
AS
  SELECT RecId,
    LTRIM(RTRIM([Version]))
   +LTRIM(RTRIM([Author]))
   +LTRIM(RTRIM([Descr]))
   +LTRIM(RTRIM([DefTarged]))
   +LTRIM(RTRIM([MenuName]))
  AS Data FROM [dbo].[ut_CodeUnits] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

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

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_CodeUnits] WITH(INDEX = Ik_Key00)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_CodeUnits_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_CodeUnits_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_CodeUnits_Key00] @oldRecId')
GO

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

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_CodeUnits] WITH(INDEX = Ik_Key00)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_CodeUnits_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_CodeUnits_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_CodeUnits_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_CodeUnits] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr,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_CodeUnits_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_CodeUnits_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_CodeUnits_Key00] @xKey, @xLen')
GO

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

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

  IF @xLen = 6 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_CodeUnits] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr 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_CodeUnits_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_CodeUnits_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_CodeUnits_Key00] @xKey, @xLen')
GO


