-- 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_Langtexte')
BEGIN
CREATE TABLE [dbo].[ut_Langtexte]
(
  [RecId]  integer NOT NULL
 ,[Art] char(2) DEFAULT ''
 ,[Lkz] char(2) DEFAULT ''
 ,[Nr] char(18) DEFAULT ''
 ,[Suchen] char(20) DEFAULT ''
  CONSTRAINT [Pk_Langtexte]  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_Langtexte_KeyDel')
  DROP TRIGGER [dbo].[tr_Langtexte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Langtexte_KeyUpd')
  DROP TRIGGER [dbo].[tr_Langtexte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Langtexte_Keys')
  DROP TABLE [dbo].[ut_Langtexte_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_Langtexte' and sc.[name] = 'Art')
BEGIN
  ALTER TABLE [dbo].[ut_Langtexte] ADD [Art] 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_Langtexte' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_Langtexte] 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_Langtexte' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Langtexte] ADD [Nr] 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_Langtexte' and sc.[name] = 'Suchen')
BEGIN
  ALTER TABLE [dbo].[ut_Langtexte] ADD [Suchen] char(20) NOT NULL DEFAULT ''
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_Langtexte' and sc.[name] = 'Art'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Art'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Art] char(2)
END
GO
IF (2 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Art'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Art'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Art] varchar(2)
END
GO
IF (2 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' 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_Langtexte' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] 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_Langtexte' 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_Langtexte' and sc.[name] = 'Lkz'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] 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_Langtexte' and sc.[name] = 'Nr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Nr] char(18)
END
GO
IF (18 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Nr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Nr'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Nr] varchar(18)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Suchen'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Suchen] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Suchen'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Langtexte' and sc.[name] = 'Suchen'))
BEGIN
   ALTER TABLE [dbo].[ut_Langtexte] ALTER COLUMN [Suchen] varchar(20)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Langtexte')
  DROP VIEW [dbo].[Langtexte]
GO
CREATE VIEW [dbo].[Langtexte] AS
  SELECT [RecId]
 ,[Art]
 ,[Lkz]
 ,[Nr]
 ,[Suchen]
  FROM [dbo].[ut_Langtexte]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Langtexte')
  DROP PROCEDURE [dbo].[up_Sel_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_Sel_Langtexte]  @mRI integer
AS
  SELECT
    [Art]
   ,[Lkz]
   ,[Nr]
   ,[Suchen]
  FROM [dbo].[ut_Langtexte] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Langtexte')
  DROP PROCEDURE [dbo].[up_Append_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_Append_Langtexte]
   @Param_Art char(2)
  ,@Param_Lkz char(2)
  ,@Param_Nr char(18)
  ,@Param_Suchen char(20)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Langtexte()
      INSERT INTO [dbo].[ut_Langtexte] (RecId
        ,[Art]
        ,[Lkz]
        ,[Nr]
        ,[Suchen]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Art))
        ,UPPER(RTRIM(@Param_Lkz))
        ,UPPER(RTRIM(@Param_Nr))
        ,UPPER(RTRIM(@Param_Suchen))
          )
      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_Langtexte')
  DROP PROCEDURE [dbo].[up_Add_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_Add_Langtexte]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Langtexte()
  INSERT INTO [dbo].[ut_Langtexte] (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_Langtexte')
  DROP PROCEDURE [dbo].[up_Upd_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_Upd_Langtexte] @mRI integer
  ,@Param_Art char(2)
  ,@Param_Lkz char(2)
  ,@Param_Nr char(18)
  ,@Param_Suchen char(20)
AS
  UPDATE [dbo].[ut_Langtexte] SET
    [Art] = UPPER(RTRIM(@Param_Art))
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Langtexte')
  DROP PROCEDURE [dbo].[up_Upd2_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Langtexte] @Param_RecId varchar(20)
  ,@Param_Art varchar(2)
  ,@Param_Lkz varchar(2)
  ,@Param_Nr varchar(18)
  ,@Param_Suchen varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Langtexte] SET
    [Art] = UPPER(RTRIM(@Param_Art))
   ,[Lkz] = UPPER(RTRIM(@Param_Lkz))
   ,[Nr] = UPPER(RTRIM(@Param_Nr))
   ,[Suchen] = UPPER(RTRIM(@Param_Suchen))
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Langtexte] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Langtexte')
  DROP PROCEDURE [dbo].[up_TrSel_Langtexte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Langtexte]
AS
  SELECT RecId,
    LTRIM(RTRIM([Art]))
   +LTRIM(RTRIM([Lkz]))
   +LTRIM(RTRIM([Nr]))
   +LTRIM(RTRIM([Suchen]))
  AS Data FROM [dbo].[ut_Langtexte] WHERE RecId > 0 ORDER BY RecId
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Langtexte_Primary')
  DROP PROCEDURE [dbo].[up_First_Langtexte_Primary]
GO
CREATE PROCEDURE [dbo].[up_First_Langtexte_Primary] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) ORDER BY Art,Lkz,Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Langtexte_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Langtexte_Primary] AS EXECUTE [dbo].[up_First_Langtexte_Primary]')
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Langtexte_Primary')
  DROP PROCEDURE [dbo].[up_Next_Langtexte_Primary]
GO
CREATE PROCEDURE [dbo].[up_Next_Langtexte_Primary] @oldRecId integer AS
  DECLARE @oldArt char(2)
  DECLARE @oldLkz char(2)
  DECLARE @oldNr char(18)
  SELECT @oldArt = Art, @oldLkz = Lkz, @oldNr = Nr FROM [dbo].[ut_Langtexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art = @oldArt) AND (Lkz = @oldLkz) AND (Nr > @oldNr)
    ORDER BY Art,Lkz,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art = @oldArt) AND (Lkz > @oldLkz)
    ORDER BY Art,Lkz,Nr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art > @oldArt)
    ORDER BY Art,Lkz,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_Langtexte_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Langtexte_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Next_Langtexte_Primary] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Langtexte_Primary')
  DROP PROCEDURE [dbo].[up_Prior_Langtexte_Primary]
GO
CREATE PROCEDURE [dbo].[up_Prior_Langtexte_Primary] @oldRecId integer AS
  DECLARE @oldArt char(2)
  DECLARE @oldLkz char(2)
  DECLARE @oldNr char(18)
  SELECT @oldArt = Art, @oldLkz = Lkz, @oldNr = Nr FROM [dbo].[ut_Langtexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art = @oldArt) AND (Lkz = @oldLkz) AND (Nr < @oldNr)
    ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art = @oldArt) AND (Lkz < @oldLkz)
    ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary)
    WHERE (Art < @oldArt)
    ORDER BY Art desc,Lkz desc,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_Langtexte_Primary')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Langtexte_Primary] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Langtexte_Primary] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Langtexte_Primary')
  DROP PROCEDURE [dbo].[up_FindF_Langtexte_Primary]
GO
CREATE PROCEDURE [dbo].[up_FindF_Langtexte_Primary] @xKey char(250), @xLen integer AS
  DECLARE @mArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mLkz char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,2)
  DECLARE @mNr char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,18)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz AND 
      Nr = @mNr
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art >= @mArt AND SUBSTRING(Art,1,LEN(@mArt)) = @mArt
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz >= @mLkz AND SUBSTRING(Lkz,1,LEN(@mLkz)) = @mLkz
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz AND 
      Nr >= @mNr AND SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Art,Lkz,Nr,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Langtexte_Primary')
  DROP PROCEDURE [dbo].[up_FindL_Langtexte_Primary]
GO
CREATE PROCEDURE [dbo].[up_FindL_Langtexte_Primary] @xKey char(250), @xLen integer AS
  DECLARE @mArt char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,2)
  DECLARE @mLkz char(2) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,3,2)
  DECLARE @mNr char(18) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,18)

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz AND 
      Nr = @mNr
      ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz
      ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt
      ORDER BY Art desc,Lkz desc,Nr 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_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      SUBSTRING(Art,1,LEN(@mArt)) = @mArt
      ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      SUBSTRING(Lkz,1,LEN(@mLkz)) = @mLkz
      ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Primary) WHERE 
      Art = @mArt AND 
      Lkz = @mLkz AND 
      SUBSTRING(Nr,1,LEN(@mNr)) = @mNr
      ORDER BY Art desc,Lkz desc,Nr desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Langtexte_Key_Suchen')
  DROP PROCEDURE [dbo].[up_First_Langtexte_Key_Suchen]
GO
CREATE PROCEDURE [dbo].[up_First_Langtexte_Key_Suchen] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) ORDER BY Art,Suchen,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Langtexte_Key_Suchen')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Langtexte_Key_Suchen] AS EXECUTE [dbo].[up_First_Langtexte_Key_Suchen]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Langtexte_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Last_Langtexte_Key_Suchen]
GO
CREATE PROCEDURE [dbo].[up_Last_Langtexte_Key_Suchen] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) ORDER BY Art desc,Suchen desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Langtexte_Key_Suchen')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Langtexte_Key_Suchen] AS EXECUTE [dbo].[up_Last_Langtexte_Key_Suchen]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Langtexte_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Next_Langtexte_Key_Suchen]
GO
CREATE PROCEDURE [dbo].[up_Next_Langtexte_Key_Suchen] @oldRecId integer AS
  DECLARE @oldArt char(2)
  DECLARE @oldSuchen char(20)
  SELECT @oldArt = Art, @oldSuchen = Suchen FROM [dbo].[ut_Langtexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art = @oldArt) AND (Suchen = @oldSuchen) AND (RecId > @oldRecId)
    ORDER BY Art,Suchen,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art = @oldArt) AND (Suchen > @oldSuchen)
    ORDER BY Art,Suchen,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art > @oldArt)
    ORDER BY Art,Suchen,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_Langtexte_Key_Suchen')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Langtexte_Key_Suchen] @oldRecId integer AS EXECUTE [dbo].[up_Next_Langtexte_Key_Suchen] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Langtexte_Key_Suchen')
  DROP PROCEDURE [dbo].[up_Prior_Langtexte_Key_Suchen]
GO
CREATE PROCEDURE [dbo].[up_Prior_Langtexte_Key_Suchen] @oldRecId integer AS
  DECLARE @oldArt char(2)
  DECLARE @oldSuchen char(20)
  SELECT @oldArt = Art, @oldSuchen = Suchen FROM [dbo].[ut_Langtexte] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art = @oldArt) AND (Suchen = @oldSuchen) AND (RecId < @oldRecId)
    ORDER BY Art desc,Suchen desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art = @oldArt) AND (Suchen < @oldSuchen)
    ORDER BY Art desc,Suchen desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen)
    WHERE (Art < @oldArt)
    ORDER BY Art desc,Suchen 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_Langtexte_Key_Suchen')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Langtexte_Key_Suchen] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Langtexte_Key_Suchen] @oldRecId')
GO

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

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

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

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt AND 
      Suchen = @mSuchen
      ORDER BY Art,Suchen,RecId
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt
      ORDER BY Art,Suchen,RecId
    RETURN
  END

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

  IF @xLen < 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art >= @mArt AND SUBSTRING(Art,1,LEN(@mArt)) = @mArt
      ORDER BY Art,Suchen,RecId
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt AND 
      Suchen >= @mSuchen AND SUBSTRING(Suchen,1,LEN(@mSuchen)) = @mSuchen
      ORDER BY Art,Suchen,RecId
    RETURN
  END

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

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

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

  IF @xLen = 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt AND 
      Suchen = @mSuchen
      ORDER BY Art desc,Suchen desc,RecId desc
    RETURN
  END

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt
      ORDER BY Art desc,Suchen 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_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      SUBSTRING(Art,1,LEN(@mArt)) = @mArt
      ORDER BY Art desc,Suchen desc,RecId desc
    RETURN
  END

  IF @xLen < 22 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Langtexte] WITH(INDEX = Ik_Key_Suchen) WHERE 
      Art = @mArt AND 
      SUBSTRING(Suchen,1,LEN(@mSuchen)) = @mSuchen
      ORDER BY Art desc,Suchen desc,RecId desc
    RETURN
  END

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


