-- 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_RabWaGruppen')
BEGIN
CREATE TABLE [dbo].[ut_RabWaGruppen]
(
  [RecId]  integer NOT NULL
 ,[BEREICH] char(1) DEFAULT ''
 ,[LKZ] char(2) DEFAULT ''
 ,[IDENT] char(6) DEFAULT ''
 ,[TEXT] char(42) DEFAULT ''
 ,[WERT1] float DEFAULT 0
 ,[WERT2] float DEFAULT 0
 ,[OPTION1] integer DEFAULT 0
 ,[OPTION2] integer DEFAULT 0
 ,[LgKto] char(8) DEFAULT ''
 ,[LgKtoNF] char(8) DEFAULT ''
  CONSTRAINT [Pk_RabWaGruppen]  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_RabWaGruppen_KeyDel')
  DROP TRIGGER [dbo].[tr_RabWaGruppen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RabWaGruppen_KeyUpd')
  DROP TRIGGER [dbo].[tr_RabWaGruppen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RabWaGruppen_Keys')
  DROP TABLE [dbo].[ut_RabWaGruppen_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_RabWaGruppen' and sc.[name] = 'BEREICH')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [BEREICH] char(1) 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_RabWaGruppen' and sc.[name] = 'LKZ')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [LKZ] char(2) 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_RabWaGruppen' and sc.[name] = 'IDENT')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [IDENT] char(6) 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_RabWaGruppen' and sc.[name] = 'TEXT')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [TEXT] char(42) 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_RabWaGruppen' and sc.[name] = 'WERT1')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [WERT1] 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_RabWaGruppen' and sc.[name] = 'WERT2')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [WERT2] 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_RabWaGruppen' and sc.[name] = 'OPTION1')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [OPTION1] 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_RabWaGruppen' and sc.[name] = 'OPTION2')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [OPTION2] 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_RabWaGruppen' and sc.[name] = 'LgKto')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [LgKto] char(8) 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_RabWaGruppen' and sc.[name] = 'LgKtoNF')
BEGIN
  ALTER TABLE [dbo].[ut_RabWaGruppen] ADD [LgKtoNF] char(8) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'BEREICH'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'BEREICH'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [BEREICH] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'BEREICH'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'BEREICH'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [BEREICH] varchar(1)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LKZ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LKZ'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LKZ] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LKZ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LKZ'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LKZ] varchar(2)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'IDENT'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'IDENT'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [IDENT] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'IDENT'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'IDENT'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [IDENT] varchar(6)
END
GO
IF (42 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'TEXT'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'TEXT'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [TEXT] char(42)
END
GO
IF (42 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'TEXT'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'TEXT'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [TEXT] varchar(42)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LgKto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKto'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LgKto] varchar(8)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKtoNF'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKtoNF'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LgKtoNF] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKtoNF'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_RabWaGruppen' and sc.[name] = 'LgKtoNF'))
BEGIN
   ALTER TABLE [dbo].[ut_RabWaGruppen] ALTER COLUMN [LgKtoNF] varchar(8)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RabWaGruppen')
  DROP VIEW [dbo].[RabWaGruppen]
GO
CREATE VIEW [dbo].[RabWaGruppen] AS
  SELECT [RecId]
 ,[BEREICH]
 ,[LKZ]
 ,[IDENT]
 ,[TEXT]
 ,[WERT1]
 ,[WERT2]
 ,[OPTION1]
 ,[OPTION2]
 ,[LgKto]
 ,[LgKtoNF]
  FROM [dbo].[ut_RabWaGruppen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_Sel_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_Sel_RabWaGruppen]  @mRI integer
AS
  SELECT
    [BEREICH]
   ,[LKZ]
   ,[IDENT]
   ,[TEXT]
   ,[WERT1]
   ,[WERT2]
   ,[OPTION1]
   ,[OPTION2]
   ,[LgKto]
   ,[LgKtoNF]
  FROM [dbo].[ut_RabWaGruppen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_Append_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_Append_RabWaGruppen]
   @Param_BEREICH char(1)
  ,@Param_LKZ char(2)
  ,@Param_IDENT char(6)
  ,@Param_TEXT char(42)
  ,@Param_WERT1 float
  ,@Param_WERT2 float
  ,@Param_OPTION1 integer
  ,@Param_OPTION2 integer
  ,@Param_LgKto char(8)
  ,@Param_LgKtoNF char(8)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_RabWaGruppen()
      INSERT INTO [dbo].[ut_RabWaGruppen] (RecId
        ,[BEREICH]
        ,[LKZ]
        ,[IDENT]
        ,[TEXT]
        ,[WERT1]
        ,[WERT2]
        ,[OPTION1]
        ,[OPTION2]
        ,[LgKto]
        ,[LgKtoNF]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_BEREICH))
        ,UPPER(RTRIM(@Param_LKZ))
        ,UPPER(RTRIM(@Param_IDENT))
        ,RTRIM(@Param_TEXT)
        ,@Param_WERT1
        ,@Param_WERT2
        ,@Param_OPTION1
        ,@Param_OPTION2
        ,UPPER(RTRIM(@Param_LgKto))
        ,UPPER(RTRIM(@Param_LgKtoNF))
          )
      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_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_Add_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_Add_RabWaGruppen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_RabWaGruppen()
  INSERT INTO [dbo].[ut_RabWaGruppen] (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_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_Upd_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd_RabWaGruppen] @mRI integer
  ,@Param_BEREICH char(1)
  ,@Param_LKZ char(2)
  ,@Param_IDENT char(6)
  ,@Param_TEXT char(42)
  ,@Param_WERT1 float
  ,@Param_WERT2 float
  ,@Param_OPTION1 integer
  ,@Param_OPTION2 integer
  ,@Param_LgKto char(8)
  ,@Param_LgKtoNF char(8)
AS
  UPDATE [dbo].[ut_RabWaGruppen] SET
    [BEREICH] = UPPER(RTRIM(@Param_BEREICH))
   ,[LKZ] = UPPER(RTRIM(@Param_LKZ))
   ,[IDENT] = UPPER(RTRIM(@Param_IDENT))
   ,[TEXT] = RTRIM(@Param_TEXT)
   ,[WERT1] = @Param_WERT1
   ,[WERT2] = @Param_WERT2
   ,[OPTION1] = @Param_OPTION1
   ,[OPTION2] = @Param_OPTION2
   ,[LgKto] = UPPER(RTRIM(@Param_LgKto))
   ,[LgKtoNF] = UPPER(RTRIM(@Param_LgKtoNF))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_Upd2_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_RabWaGruppen] @Param_RecId varchar(20)
  ,@Param_BEREICH varchar(1)
  ,@Param_LKZ varchar(2)
  ,@Param_IDENT varchar(6)
  ,@Param_TEXT varchar(42)
  ,@Param_WERT1 varchar(10)
  ,@Param_WERT2 varchar(10)
  ,@Param_OPTION1 varchar(1)
  ,@Param_OPTION2 varchar(1)
  ,@Param_LgKto varchar(8)
  ,@Param_LgKtoNF varchar(8)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_RabWaGruppen] SET
    [BEREICH] = UPPER(RTRIM(@Param_BEREICH))
   ,[LKZ] = UPPER(RTRIM(@Param_LKZ))
   ,[IDENT] = UPPER(RTRIM(@Param_IDENT))
   ,[TEXT] = RTRIM(@Param_TEXT)
   ,[WERT1] = dbo.itcfunc_float(@Param_WERT1)
   ,[WERT2] = dbo.itcfunc_float(@Param_WERT2)
   ,[OPTION1] = dbo.itcfunc_integer(@Param_OPTION1)
   ,[OPTION2] = dbo.itcfunc_integer(@Param_OPTION2)
   ,[LgKto] = UPPER(RTRIM(@Param_LgKto))
   ,[LgKtoNF] = UPPER(RTRIM(@Param_LgKtoNF))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_RabWaGruppen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_RabWaGruppen')
  DROP PROCEDURE [dbo].[up_TrSel_RabWaGruppen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_RabWaGruppen]
AS
  SELECT RecId,
    LTRIM(RTRIM([BEREICH]))
   +LTRIM(RTRIM([LKZ]))
   +LTRIM(RTRIM([IDENT]))
   +LTRIM(RTRIM([TEXT]))
   +LTRIM(RTRIM([LgKto]))
   +LTRIM(RTRIM([LgKtoNF]))
  AS Data FROM [dbo].[ut_RabWaGruppen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_First_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_First_RabWaGruppen_KEY0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) ORDER BY BEREICH,LKZ,IDENT,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_RabWaGruppen_KEY0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_RabWaGruppen_KEY0] AS EXECUTE [dbo].[up_First_RabWaGruppen_KEY0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_Last_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_Last_RabWaGruppen_KEY0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_RabWaGruppen_KEY0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_RabWaGruppen_KEY0] AS EXECUTE [dbo].[up_Last_RabWaGruppen_KEY0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_Next_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_Next_RabWaGruppen_KEY0] @oldRecId integer AS
  DECLARE @oldBEREICH char(1)
  DECLARE @oldLKZ char(2)
  DECLARE @oldIDENT char(6)
  SELECT @oldBEREICH = BEREICH, @oldLKZ = LKZ, @oldIDENT = IDENT FROM [dbo].[ut_RabWaGruppen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH = @oldBEREICH) AND (LKZ = @oldLKZ) AND (IDENT > @oldIDENT)
    ORDER BY BEREICH,LKZ,IDENT,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH = @oldBEREICH) AND (LKZ > @oldLKZ)
    ORDER BY BEREICH,LKZ,IDENT,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH > @oldBEREICH)
    ORDER BY BEREICH,LKZ,IDENT,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_RabWaGruppen_KEY0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_RabWaGruppen_KEY0] @oldRecId integer AS EXECUTE [dbo].[up_Next_RabWaGruppen_KEY0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_Prior_RabWaGruppen_KEY0] @oldRecId integer AS
  DECLARE @oldBEREICH char(1)
  DECLARE @oldLKZ char(2)
  DECLARE @oldIDENT char(6)
  SELECT @oldBEREICH = BEREICH, @oldLKZ = LKZ, @oldIDENT = IDENT FROM [dbo].[ut_RabWaGruppen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH = @oldBEREICH) AND (LKZ = @oldLKZ) AND (IDENT < @oldIDENT)
    ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH = @oldBEREICH) AND (LKZ < @oldLKZ)
    ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0)
    WHERE (BEREICH < @oldBEREICH)
    ORDER BY BEREICH desc,LKZ desc,IDENT 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_RabWaGruppen_KEY0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_RabWaGruppen_KEY0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_RabWaGruppen_KEY0] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_FindF_RabWaGruppen_KEY0] @xKey char(250), @xLen integer AS
  DECLARE @mBEREICH char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mLKZ char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,2)
  DECLARE @mIDENT char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,4,6)

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ AND 
      IDENT = @mIDENT
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH >= @mBEREICH AND SUBSTRING(BEREICH,1,LEN(@mBEREICH)) = @mBEREICH
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ >= @mLKZ AND SUBSTRING(LKZ,1,LEN(@mLKZ)) = @mLKZ
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ AND 
      IDENT >= @mIDENT AND SUBSTRING(IDENT,1,LEN(@mIDENT)) = @mIDENT
      ORDER BY BEREICH,LKZ,IDENT,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RabWaGruppen_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_RabWaGruppen_KEY0]
GO
CREATE PROCEDURE [dbo].[up_FindL_RabWaGruppen_KEY0] @xKey char(250), @xLen integer AS
  DECLARE @mBEREICH char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,1)
  DECLARE @mLKZ char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,2,2)
  DECLARE @mIDENT char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,4,6)

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

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ AND 
      IDENT = @mIDENT
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

  IF @xLen = 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

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

  IF @xLen < 1 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      SUBSTRING(BEREICH,1,LEN(@mBEREICH)) = @mBEREICH
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

  IF @xLen < 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      SUBSTRING(LKZ,1,LEN(@mLKZ)) = @mLKZ
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

  IF @xLen < 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_RabWaGruppen] WITH(INDEX = Ik_KEY0) WHERE 
      BEREICH = @mBEREICH AND 
      LKZ = @mLKZ AND 
      SUBSTRING(IDENT,1,LEN(@mIDENT)) = @mIDENT
      ORDER BY BEREICH desc,LKZ desc,IDENT desc,RecId desc
    RETURN
  END

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


