-- 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_BelText')
BEGIN
CREATE TABLE [dbo].[ut_BelText]
(
  [RecId]  integer NOT NULL
 ,[PrjNr] char(8) DEFAULT ''
 ,[BelArt] char(2) DEFAULT ''
 ,[BelNr] char(8) DEFAULT ''
 ,[TextArt] char(1) DEFAULT ''
 ,[RefKey] char(6) DEFAULT ''
 ,[LfdNr] char(6) DEFAULT ''
 ,[Abschluss] tinyint DEFAULT 0
 ,[SoftBreak] tinyint DEFAULT 0
 ,[LineFeeds] integer DEFAULT 0
 ,[CntChar] integer DEFAULT 0
 ,[Daten] varchar(55) DEFAULT ''
  CONSTRAINT [Pk_BelText]  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_BelText_KeyDel')
  DROP TRIGGER [dbo].[tr_BelText_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelText_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelText_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelText_Keys')
  DROP TABLE [dbo].[ut_BelText_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_BelText' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [PrjNr] 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_BelText' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [BelArt] 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_BelText' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [BelNr] 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_BelText' and sc.[name] = 'TextArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [TextArt] 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_BelText' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [RefKey] 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_BelText' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [LfdNr] 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_BelText' and sc.[name] = 'Abschluss')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [Abschluss] 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_BelText' and sc.[name] = 'SoftBreak')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [SoftBreak] 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_BelText' and sc.[name] = 'LineFeeds')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [LineFeeds] 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_BelText' and sc.[name] = 'CntChar')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [CntChar] 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_BelText' and sc.[name] = 'Daten')
BEGIN
  ALTER TABLE [dbo].[ut_BelText] ADD [Daten] varchar(55) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'PrjNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [PrjNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'PrjNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'PrjNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [PrjNr] varchar(8)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [BelArt] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [BelArt] varchar(2)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [BelNr] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'BelNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [BelNr] varchar(8)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'TextArt'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'TextArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [TextArt] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'TextArt'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'TextArt'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [TextArt] varchar(1)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'RefKey'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'RefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [RefKey] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'RefKey'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'RefKey'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [RefKey] varchar(6)
END
GO
IF (6 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' 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_BelText' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [LfdNr] char(6)
END
GO
IF (6 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' 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_BelText' and sc.[name] = 'LfdNr'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [LfdNr] varchar(6)
END
GO
IF (55 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'Daten'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'Daten'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [Daten] char(55)
END
GO
IF (55 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'Daten'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_BelText' and sc.[name] = 'Daten'))
BEGIN
   ALTER TABLE [dbo].[ut_BelText] ALTER COLUMN [Daten] varchar(55)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelText')
  DROP VIEW [dbo].[BelText]
GO
CREATE VIEW [dbo].[BelText] AS
  SELECT [RecId]
 ,[PrjNr]
 ,[BelArt]
 ,[BelNr]
 ,[TextArt]
 ,[RefKey]
 ,[LfdNr]
 ,[Abschluss]
 ,[SoftBreak]
 ,[LineFeeds]
 ,[CntChar]
 ,[Daten]
  FROM [dbo].[ut_BelText]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelText')
  DROP PROCEDURE [dbo].[up_Sel_BelText]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelText]  @mRI integer
AS
  SELECT
    [PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[TextArt]
   ,[RefKey]
   ,[LfdNr]
   ,[Abschluss]
   ,[SoftBreak]
   ,[LineFeeds]
   ,[CntChar]
   ,[Daten]
  FROM [dbo].[ut_BelText] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelText')
  DROP PROCEDURE [dbo].[up_Append_BelText]
GO
CREATE PROCEDURE [dbo].[up_Append_BelText]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_TextArt char(1)
  ,@Param_RefKey char(6)
  ,@Param_LfdNr char(6)
  ,@Param_Abschluss tinyint
  ,@Param_SoftBreak tinyint
  ,@Param_LineFeeds integer
  ,@Param_CntChar integer
  ,@Param_Daten varchar(55)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_BelText()
      INSERT INTO [dbo].[ut_BelText] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[TextArt]
        ,[RefKey]
        ,[LfdNr]
        ,[Abschluss]
        ,[SoftBreak]
        ,[LineFeeds]
        ,[CntChar]
        ,[Daten]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_TextArt))
        ,UPPER(RTRIM(@Param_RefKey))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,@Param_Abschluss
        ,@Param_SoftBreak
        ,@Param_LineFeeds
        ,@Param_CntChar
        ,RTRIM(@Param_Daten)
          )
      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_BelText')
  DROP PROCEDURE [dbo].[up_Add_BelText]
GO
CREATE PROCEDURE [dbo].[up_Add_BelText]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_BelText()
  INSERT INTO [dbo].[ut_BelText] (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_BelText')
  DROP PROCEDURE [dbo].[up_Upd_BelText]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelText] @mRI integer
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_TextArt char(1)
  ,@Param_RefKey char(6)
  ,@Param_LfdNr char(6)
  ,@Param_Abschluss tinyint
  ,@Param_SoftBreak tinyint
  ,@Param_LineFeeds integer
  ,@Param_CntChar integer
  ,@Param_Daten varchar(55)
AS
  UPDATE [dbo].[ut_BelText] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[TextArt] = UPPER(RTRIM(@Param_TextArt))
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Abschluss] = @Param_Abschluss
   ,[SoftBreak] = @Param_SoftBreak
   ,[LineFeeds] = @Param_LineFeeds
   ,[CntChar] = @Param_CntChar
   ,[Daten] = RTRIM(@Param_Daten)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_BelText')
  DROP PROCEDURE [dbo].[up_Upd2_BelText]
GO
CREATE PROCEDURE [dbo].[up_Upd2_BelText] @Param_RecId varchar(20)
  ,@Param_PrjNr varchar(8)
  ,@Param_BelArt varchar(2)
  ,@Param_BelNr varchar(8)
  ,@Param_TextArt varchar(1)
  ,@Param_RefKey varchar(6)
  ,@Param_LfdNr varchar(6)
  ,@Param_Abschluss varchar(1)
  ,@Param_SoftBreak varchar(1)
  ,@Param_LineFeeds varchar(2)
  ,@Param_CntChar varchar(2)
  ,@Param_Daten varchar(55)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_BelText] SET
    [PrjNr] = UPPER(RTRIM(@Param_PrjNr))
   ,[BelArt] = UPPER(RTRIM(@Param_BelArt))
   ,[BelNr] = UPPER(RTRIM(@Param_BelNr))
   ,[TextArt] = UPPER(RTRIM(@Param_TextArt))
   ,[RefKey] = UPPER(RTRIM(@Param_RefKey))
   ,[LfdNr] = UPPER(RTRIM(@Param_LfdNr))
   ,[Abschluss] = dbo.itcfunc_integer(@Param_Abschluss)
   ,[SoftBreak] = dbo.itcfunc_integer(@Param_SoftBreak)
   ,[LineFeeds] = dbo.itcfunc_integer(@Param_LineFeeds)
   ,[CntChar] = dbo.itcfunc_integer(@Param_CntChar)
   ,[Daten] = RTRIM(@Param_Daten)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_BelText] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_BelText')
  DROP PROCEDURE [dbo].[up_TrSel_BelText]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelText]
AS
  SELECT RecId,
    LTRIM(RTRIM([PrjNr]))
   +LTRIM(RTRIM([BelArt]))
   +LTRIM(RTRIM([BelNr]))
   +LTRIM(RTRIM([TextArt]))
   +LTRIM(RTRIM([RefKey]))
   +LTRIM(RTRIM([LfdNr]))
   +LTRIM(RTRIM([Daten]))
  AS Data FROM [dbo].[ut_BelText] WHERE RecId > 0 ORDER BY RecId
GO

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

-- logical key  ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelText' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  DROP INDEX [Ik_Key0] ON [dbo].[ut_BelText]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_BelText' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelText] (PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelText_Key0')
  DROP PROCEDURE [dbo].[up_First_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_First_BelText_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_BelText_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_BelText_Key0] AS EXECUTE [dbo].[up_First_BelText_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelText_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_Last_BelText_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_BelText_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_BelText_Key0] AS EXECUTE [dbo].[up_Last_BelText_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelText_Key0')
  DROP PROCEDURE [dbo].[up_Next_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_Next_BelText_Key0] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldTextArt char(1)
  DECLARE @oldRefKey char(6)
  DECLARE @oldLfdNr char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldTextArt = TextArt, @oldRefKey = RefKey, @oldLfdNr = LfdNr FROM [dbo].[ut_BelText] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey = @oldRefKey) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey = @oldRefKey) AND (LfdNr > @oldLfdNr)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey > @oldRefKey)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt > @oldTextArt)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr > @oldBelNr)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt > @oldBelArt)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr > @oldPrjNr)
    ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,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_BelText_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_BelText_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_BelText_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelText_Key0')
  DROP PROCEDURE [dbo].[up_Prior_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_BelText_Key0] @oldRecId integer AS
  DECLARE @oldPrjNr char(8)
  DECLARE @oldBelArt char(2)
  DECLARE @oldBelNr char(8)
  DECLARE @oldTextArt char(1)
  DECLARE @oldRefKey char(6)
  DECLARE @oldLfdNr char(6)
  SELECT @oldPrjNr = PrjNr, @oldBelArt = BelArt, @oldBelNr = BelNr, @oldTextArt = TextArt, @oldRefKey = RefKey, @oldLfdNr = LfdNr FROM [dbo].[ut_BelText] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey = @oldRefKey) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey = @oldRefKey) AND (LfdNr < @oldLfdNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt = @oldTextArt) AND (RefKey < @oldRefKey)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr = @oldBelNr) AND (TextArt < @oldTextArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt = @oldBelArt) AND (BelNr < @oldBelNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr = @oldPrjNr) AND (BelArt < @oldBelArt)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText] WITH(INDEX = Ik_Key0)
    WHERE (PrjNr < @oldPrjNr)
    ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_BelText_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_BelText_Key0] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelText_Key0')
  DROP PROCEDURE [dbo].[up_FindF_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindF_BelText_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mTextArt char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,1)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,6)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,6)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey AND 
      LfdNr = @mLfdNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr >= @mPrjNr AND SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt >= @mBelArt AND SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr >= @mBelNr AND SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt >= @mTextArt AND SUBSTRING(TextArt,1,LEN(@mTextArt)) = @mTextArt
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey >= @mRefKey AND SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey AND 
      LfdNr >= @mLfdNr AND SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY PrjNr,BelArt,BelNr,TextArt,RefKey,LfdNr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelText_Key0')
  DROP PROCEDURE [dbo].[up_FindL_BelText_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_BelText_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mPrjNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)
  DECLARE @mBelArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,9,2)
  DECLARE @mBelNr char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,11,8)
  DECLARE @mTextArt char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,19,1)
  DECLARE @mRefKey char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,20,6)
  DECLARE @mLfdNr char(6) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,26,6)

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

  IF @xLen = 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey AND 
      LfdNr = @mLfdNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(PrjNr,1,LEN(@mPrjNr)) = @mPrjNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      SUBSTRING(BelArt,1,LEN(@mBelArt)) = @mBelArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 18 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      SUBSTRING(BelNr,1,LEN(@mBelNr)) = @mBelNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 19 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      SUBSTRING(TextArt,1,LEN(@mTextArt)) = @mTextArt
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 25 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      SUBSTRING(RefKey,1,LEN(@mRefKey)) = @mRefKey
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen < 31 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BelText] WITH(INDEX = Ik_Key0) WHERE 
      PrjNr = @mPrjNr AND 
      BelArt = @mBelArt AND 
      BelNr = @mBelNr AND 
      TextArt = @mTextArt AND 
      RefKey = @mRefKey AND 
      SUBSTRING(LfdNr,1,LEN(@mLfdNr)) = @mLfdNr
      ORDER BY PrjNr desc,BelArt desc,BelNr desc,TextArt desc,RefKey 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_BelText_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_BelText_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_BelText_Key0] @xKey, @xLen')
GO


