-- 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_Dimensionstexte')
BEGIN
CREATE TABLE [dbo].[ut_Dimensionstexte]
(
  [RecId]  integer NOT NULL
 ,[LKZ] char(2) DEFAULT ''
 ,[ARTNR] char(18) DEFAULT ''
 ,[LFDNR] char(2) DEFAULT ''
 ,[KENNUNG] char(1) DEFAULT ''
 ,[LKZTEXTNR] char(2) DEFAULT ''
 ,[TEXTNR] char(18) DEFAULT ''
 ,[TEXTZEILE] char(55) DEFAULT ''
 ,[ABSCHLUSS] tinyint DEFAULT 0
  CONSTRAINT [Pk_Dimensionstexte]  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_Dimensionstexte_KeyDel')
  DROP TRIGGER [dbo].[tr_Dimensionstexte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Dimensionstexte_KeyUpd')
  DROP TRIGGER [dbo].[tr_Dimensionstexte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Dimensionstexte_Keys')
  DROP TABLE [dbo].[ut_Dimensionstexte_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_Dimensionstexte' and sc.[name] = 'LKZ')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] 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_Dimensionstexte' and sc.[name] = 'ARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [ARTNR] char(18) 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_Dimensionstexte' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [LFDNR] 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_Dimensionstexte' and sc.[name] = 'KENNUNG')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [KENNUNG] 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_Dimensionstexte' and sc.[name] = 'LKZTEXTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [LKZTEXTNR] 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_Dimensionstexte' and sc.[name] = 'TEXTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [TEXTNR] char(18) 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_Dimensionstexte' and sc.[name] = 'TEXTZEILE')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [TEXTZEILE] char(55) 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_Dimensionstexte' and sc.[name] = 'ABSCHLUSS')
BEGIN
  ALTER TABLE [dbo].[ut_Dimensionstexte] ADD [ABSCHLUSS] tinyint NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' 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_Dimensionstexte' and sc.[name] = 'LKZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] 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_Dimensionstexte' 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_Dimensionstexte' and sc.[name] = 'LKZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [LKZ] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'ARTNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'ARTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [ARTNR] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'ARTNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'ARTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [ARTNR] varchar(18)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LFDNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LFDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [LFDNR] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LFDNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LFDNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [LFDNR] varchar(2)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'KENNUNG'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'KENNUNG'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [KENNUNG] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'KENNUNG'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'KENNUNG'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [KENNUNG] 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_Dimensionstexte' and sc.[name] = 'LKZTEXTNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LKZTEXTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [LKZTEXTNR] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LKZTEXTNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'LKZTEXTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [LKZTEXTNR] varchar(2)
END
GO
IF (18 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTNR'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [TEXTNR] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTNR'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTNR'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [TEXTNR] varchar(18)
END
GO
IF (55 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTZEILE'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTZEILE'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [TEXTZEILE] char(55)
END
GO
IF (55 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTZEILE'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Dimensionstexte' and sc.[name] = 'TEXTZEILE'))
BEGIN
   ALTER TABLE [dbo].[ut_Dimensionstexte] ALTER COLUMN [TEXTZEILE] varchar(55)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Dimensionstexte')
  DROP VIEW [dbo].[Dimensionstexte]
GO
CREATE VIEW [dbo].[Dimensionstexte] AS
  SELECT [RecId]
 ,[LKZ]
 ,[ARTNR]
 ,[LFDNR]
 ,[KENNUNG]
 ,[LKZTEXTNR]
 ,[TEXTNR]
 ,[TEXTZEILE]
 ,[ABSCHLUSS]
  FROM [dbo].[ut_Dimensionstexte]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_Sel_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_Sel_Dimensionstexte]  @mRI integer
AS
  SELECT
    [LKZ]
   ,[ARTNR]
   ,[LFDNR]
   ,[KENNUNG]
   ,[LKZTEXTNR]
   ,[TEXTNR]
   ,[TEXTZEILE]
   ,[ABSCHLUSS]
  FROM [dbo].[ut_Dimensionstexte] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_Append_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_Append_Dimensionstexte]
   @Param_LKZ char(2)
  ,@Param_ARTNR char(18)
  ,@Param_LFDNR char(2)
  ,@Param_KENNUNG char(1)
  ,@Param_LKZTEXTNR char(2)
  ,@Param_TEXTNR char(18)
  ,@Param_TEXTZEILE char(55)
  ,@Param_ABSCHLUSS tinyint
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Dimensionstexte()
      INSERT INTO [dbo].[ut_Dimensionstexte] (RecId
        ,[LKZ]
        ,[ARTNR]
        ,[LFDNR]
        ,[KENNUNG]
        ,[LKZTEXTNR]
        ,[TEXTNR]
        ,[TEXTZEILE]
        ,[ABSCHLUSS]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_LKZ))
        ,UPPER(RTRIM(@Param_ARTNR))
        ,RTRIM(@Param_LFDNR)
        ,UPPER(RTRIM(@Param_KENNUNG))
        ,UPPER(RTRIM(@Param_LKZTEXTNR))
        ,UPPER(RTRIM(@Param_TEXTNR))
        ,RTRIM(@Param_TEXTZEILE)
        ,@Param_ABSCHLUSS
          )
      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_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_Add_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_Add_Dimensionstexte]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Dimensionstexte()
  INSERT INTO [dbo].[ut_Dimensionstexte] (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_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_Upd_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_Upd_Dimensionstexte] @mRI integer
  ,@Param_LKZ char(2)
  ,@Param_ARTNR char(18)
  ,@Param_LFDNR char(2)
  ,@Param_KENNUNG char(1)
  ,@Param_LKZTEXTNR char(2)
  ,@Param_TEXTNR char(18)
  ,@Param_TEXTZEILE char(55)
  ,@Param_ABSCHLUSS tinyint
AS
  UPDATE [dbo].[ut_Dimensionstexte] SET
    [LKZ] = UPPER(RTRIM(@Param_LKZ))
   ,[ARTNR] = UPPER(RTRIM(@Param_ARTNR))
   ,[LFDNR] = RTRIM(@Param_LFDNR)
   ,[KENNUNG] = UPPER(RTRIM(@Param_KENNUNG))
   ,[LKZTEXTNR] = UPPER(RTRIM(@Param_LKZTEXTNR))
   ,[TEXTNR] = UPPER(RTRIM(@Param_TEXTNR))
   ,[TEXTZEILE] = RTRIM(@Param_TEXTZEILE)
   ,[ABSCHLUSS] = @Param_ABSCHLUSS
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_Upd2_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Dimensionstexte] @Param_RecId varchar(20)
  ,@Param_LKZ varchar(2)
  ,@Param_ARTNR varchar(18)
  ,@Param_LFDNR varchar(2)
  ,@Param_KENNUNG varchar(1)
  ,@Param_LKZTEXTNR varchar(2)
  ,@Param_TEXTNR varchar(18)
  ,@Param_TEXTZEILE varchar(55)
  ,@Param_ABSCHLUSS varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Dimensionstexte] SET
    [LKZ] = UPPER(RTRIM(@Param_LKZ))
   ,[ARTNR] = UPPER(RTRIM(@Param_ARTNR))
   ,[LFDNR] = RTRIM(@Param_LFDNR)
   ,[KENNUNG] = UPPER(RTRIM(@Param_KENNUNG))
   ,[LKZTEXTNR] = UPPER(RTRIM(@Param_LKZTEXTNR))
   ,[TEXTNR] = UPPER(RTRIM(@Param_TEXTNR))
   ,[TEXTZEILE] = RTRIM(@Param_TEXTZEILE)
   ,[ABSCHLUSS] = dbo.itcfunc_integer(@Param_ABSCHLUSS)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Dimensionstexte] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Dimensionstexte')
  DROP PROCEDURE [dbo].[up_TrSel_Dimensionstexte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Dimensionstexte]
AS
  SELECT RecId,
    LTRIM(RTRIM([LKZ]))
   +LTRIM(RTRIM([ARTNR]))
   +LTRIM(RTRIM([LFDNR]))
   +LTRIM(RTRIM([KENNUNG]))
   +LTRIM(RTRIM([LKZTEXTNR]))
   +LTRIM(RTRIM([TEXTNR]))
   +LTRIM(RTRIM([TEXTZEILE]))
  AS Data FROM [dbo].[ut_Dimensionstexte] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_First_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_First_Dimensionstexte_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) ORDER BY LKZ,ARTNR,LFDNR,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Dimensionstexte_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Dimensionstexte_Key0] AS EXECUTE [dbo].[up_First_Dimensionstexte_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_Last_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_Last_Dimensionstexte_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Dimensionstexte_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Dimensionstexte_Key0] AS EXECUTE [dbo].[up_Last_Dimensionstexte_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_Next_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_Next_Dimensionstexte_Key0] @oldRecId integer AS
  DECLARE @oldLKZ char(2)
  DECLARE @oldARTNR char(18)
  DECLARE @oldLFDNR char(2)
  SELECT @oldLKZ = LKZ, @oldARTNR = ARTNR, @oldLFDNR = LFDNR FROM [dbo].[ut_Dimensionstexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ = @oldLKZ) AND (ARTNR = @oldARTNR) AND (LFDNR > @oldLFDNR)
    ORDER BY LKZ,ARTNR,LFDNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ = @oldLKZ) AND (ARTNR > @oldARTNR)
    ORDER BY LKZ,ARTNR,LFDNR,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ > @oldLKZ)
    ORDER BY LKZ,ARTNR,LFDNR,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_Dimensionstexte_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Dimensionstexte_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_Dimensionstexte_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_Dimensionstexte_Key0] @oldRecId integer AS
  DECLARE @oldLKZ char(2)
  DECLARE @oldARTNR char(18)
  DECLARE @oldLFDNR char(2)
  SELECT @oldLKZ = LKZ, @oldARTNR = ARTNR, @oldLFDNR = LFDNR FROM [dbo].[ut_Dimensionstexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ = @oldLKZ) AND (ARTNR = @oldARTNR) AND (LFDNR < @oldLFDNR)
    ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ = @oldLKZ) AND (ARTNR < @oldARTNR)
    ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0)
    WHERE (LKZ < @oldLKZ)
    ORDER BY LKZ desc,ARTNR desc,LFDNR 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_Dimensionstexte_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Dimensionstexte_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Dimensionstexte_Key0] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindF_Dimensionstexte_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mLKZ char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mARTNR char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,18)
  DECLARE @mLFDNR char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,2)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR AND 
      LFDNR = @mLFDNR
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ >= @mLKZ AND SUBSTRING(LKZ,1,LEN(@mLKZ)) = @mLKZ
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR >= @mARTNR AND SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR AND 
      LFDNR >= @mLFDNR AND SUBSTRING(LFDNR,1,LEN(@mLFDNR)) = @mLFDNR
      ORDER BY LKZ,ARTNR,LFDNR,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Dimensionstexte_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Dimensionstexte_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_Dimensionstexte_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mLKZ char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mARTNR char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,18)
  DECLARE @mLFDNR char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,21,2)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR AND 
      LFDNR = @mLFDNR
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(LKZ,1,LEN(@mLKZ)) = @mLKZ
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      SUBSTRING(ARTNR,1,LEN(@mARTNR)) = @mARTNR
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Dimensionstexte] WITH(INDEX = Ik_Key0) WHERE 
      LKZ = @mLKZ AND 
      ARTNR = @mARTNR AND 
      SUBSTRING(LFDNR,1,LEN(@mLFDNR)) = @mLFDNR
      ORDER BY LKZ desc,ARTNR desc,LFDNR desc,RecId desc
    RETURN
  END

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


