-- 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_Bilanzzeilen')
BEGIN
CREATE TABLE [dbo].[ut_Bilanzzeilen]
(
  [RecId]  integer NOT NULL
 ,[LstNr] char(4) DEFAULT ''
 ,[LfdNr] integer DEFAULT 0
 ,[Tiefe] integer DEFAULT 0
 ,[Still] tinyint DEFAULT 0
 ,[PosTyp] integer DEFAULT 0
 ,[PosNr] varchar(20) DEFAULT ''
 ,[KtoTyp] char(1) DEFAULT ''
 ,[KtoVon] char(8) DEFAULT ''
 ,[KtoBis] char(8) DEFAULT ''
 ,[Text] varchar(50) DEFAULT ''
 ,[Fett] tinyint DEFAULT 0
 ,[FormFeed] tinyint DEFAULT 0
 ,[LineFeed] tinyint DEFAULT 0
 ,[LineFeed2] tinyint DEFAULT 0
 ,[Header] integer DEFAULT 0
 ,[Footer] integer DEFAULT 0
 ,[HideEmpty] tinyint DEFAULT 0
 ,[SumUp] integer DEFAULT 0
  CONSTRAINT [Pk_Bilanzzeilen]  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_Bilanzzeilen_KeyDel')
  DROP TRIGGER [dbo].[tr_Bilanzzeilen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bilanzzeilen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Bilanzzeilen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Bilanzzeilen_Keys')
  DROP TABLE [dbo].[ut_Bilanzzeilen_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_Bilanzzeilen' and sc.[name] = 'LstNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [LstNr] char(4) 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_Bilanzzeilen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [LfdNr] 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_Bilanzzeilen' and sc.[name] = 'Tiefe')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Tiefe] 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_Bilanzzeilen' and sc.[name] = 'Still')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Still] 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_Bilanzzeilen' and sc.[name] = 'PosTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [PosTyp] 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_Bilanzzeilen' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [PosNr] varchar(20) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [KtoTyp] 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_Bilanzzeilen' and sc.[name] = 'KtoVon')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [KtoVon] 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_Bilanzzeilen' and sc.[name] = 'KtoBis')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [KtoBis] 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_Bilanzzeilen' and sc.[name] = 'Text')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Text] varchar(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'Fett')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Fett] 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_Bilanzzeilen' and sc.[name] = 'FormFeed')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [FormFeed] 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_Bilanzzeilen' and sc.[name] = 'LineFeed')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [LineFeed] 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_Bilanzzeilen' and sc.[name] = 'LineFeed2')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [LineFeed2] 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_Bilanzzeilen' and sc.[name] = 'Header')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Header] 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_Bilanzzeilen' and sc.[name] = 'Footer')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [Footer] 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_Bilanzzeilen' and sc.[name] = 'HideEmpty')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [HideEmpty] 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_Bilanzzeilen' and sc.[name] = 'SumUp')
BEGIN
  ALTER TABLE [dbo].[ut_Bilanzzeilen] ADD [SumUp] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (4 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'LstNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'LstNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [LstNr] char(4)
END
GO
IF (4 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'LstNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'LstNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [LstNr] varchar(4)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'PosNr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'PosNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [PosNr] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'PosNr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'PosNr'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [PosNr] varchar(20)
END
GO
IF (1 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoTyp'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoTyp] char(1)
END
GO
IF (1 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoTyp'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoTyp'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoTyp] varchar(1)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoVon'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoVon'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoVon] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoVon'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoVon'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoVon] 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_Bilanzzeilen' and sc.[name] = 'KtoBis'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoBis'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoBis] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoBis'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' and sc.[name] = 'KtoBis'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [KtoBis] varchar(8)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' 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_Bilanzzeilen' and sc.[name] = 'Text'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [Text] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Bilanzzeilen' 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_Bilanzzeilen' and sc.[name] = 'Text'))
BEGIN
   ALTER TABLE [dbo].[ut_Bilanzzeilen] ALTER COLUMN [Text] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Bilanzzeilen')
  DROP VIEW [dbo].[Bilanzzeilen]
GO
CREATE VIEW [dbo].[Bilanzzeilen] AS
  SELECT [RecId]
 ,[LstNr]
 ,[LfdNr]
 ,[Tiefe]
 ,[Still]
 ,[PosTyp]
 ,[PosNr]
 ,[KtoTyp]
 ,[KtoVon]
 ,[KtoBis]
 ,[Text]
 ,[Fett]
 ,[FormFeed]
 ,[LineFeed]
 ,[LineFeed2]
 ,[Header]
 ,[Footer]
 ,[HideEmpty]
 ,[SumUp]
  FROM [dbo].[ut_Bilanzzeilen]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_Sel_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Bilanzzeilen]  @mRI integer
AS
  SELECT
    [LstNr]
   ,[LfdNr]
   ,[Tiefe]
   ,[Still]
   ,[PosTyp]
   ,[PosNr]
   ,[KtoTyp]
   ,[KtoVon]
   ,[KtoBis]
   ,[Text]
   ,[Fett]
   ,[FormFeed]
   ,[LineFeed]
   ,[LineFeed2]
   ,[Header]
   ,[Footer]
   ,[HideEmpty]
   ,[SumUp]
  FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_Append_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_Append_Bilanzzeilen]
   @Param_LstNr char(4)
  ,@Param_LfdNr integer
  ,@Param_Tiefe integer
  ,@Param_Still tinyint
  ,@Param_PosTyp integer
  ,@Param_PosNr varchar(20)
  ,@Param_KtoTyp char(1)
  ,@Param_KtoVon char(8)
  ,@Param_KtoBis char(8)
  ,@Param_Text varchar(50)
  ,@Param_Fett tinyint
  ,@Param_FormFeed tinyint
  ,@Param_LineFeed tinyint
  ,@Param_LineFeed2 tinyint
  ,@Param_Header integer
  ,@Param_Footer integer
  ,@Param_HideEmpty tinyint
  ,@Param_SumUp integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Bilanzzeilen()
      INSERT INTO [dbo].[ut_Bilanzzeilen] (RecId
        ,[LstNr]
        ,[LfdNr]
        ,[Tiefe]
        ,[Still]
        ,[PosTyp]
        ,[PosNr]
        ,[KtoTyp]
        ,[KtoVon]
        ,[KtoBis]
        ,[Text]
        ,[Fett]
        ,[FormFeed]
        ,[LineFeed]
        ,[LineFeed2]
        ,[Header]
        ,[Footer]
        ,[HideEmpty]
        ,[SumUp]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_LstNr))
        ,@Param_LfdNr
        ,@Param_Tiefe
        ,@Param_Still
        ,@Param_PosTyp
        ,RTRIM(@Param_PosNr)
        ,UPPER(RTRIM(@Param_KtoTyp))
        ,UPPER(RTRIM(@Param_KtoVon))
        ,UPPER(RTRIM(@Param_KtoBis))
        ,RTRIM(@Param_Text)
        ,@Param_Fett
        ,@Param_FormFeed
        ,@Param_LineFeed
        ,@Param_LineFeed2
        ,@Param_Header
        ,@Param_Footer
        ,@Param_HideEmpty
        ,@Param_SumUp
          )
      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_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_Add_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_Add_Bilanzzeilen]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Bilanzzeilen()
  INSERT INTO [dbo].[ut_Bilanzzeilen] (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_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_Upd_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Bilanzzeilen] @mRI integer
  ,@Param_LstNr char(4)
  ,@Param_LfdNr integer
  ,@Param_Tiefe integer
  ,@Param_Still tinyint
  ,@Param_PosTyp integer
  ,@Param_PosNr varchar(20)
  ,@Param_KtoTyp char(1)
  ,@Param_KtoVon char(8)
  ,@Param_KtoBis char(8)
  ,@Param_Text varchar(50)
  ,@Param_Fett tinyint
  ,@Param_FormFeed tinyint
  ,@Param_LineFeed tinyint
  ,@Param_LineFeed2 tinyint
  ,@Param_Header integer
  ,@Param_Footer integer
  ,@Param_HideEmpty tinyint
  ,@Param_SumUp integer
AS
  UPDATE [dbo].[ut_Bilanzzeilen] SET
    [LstNr] = UPPER(RTRIM(@Param_LstNr))
   ,[LfdNr] = @Param_LfdNr
   ,[Tiefe] = @Param_Tiefe
   ,[Still] = @Param_Still
   ,[PosTyp] = @Param_PosTyp
   ,[PosNr] = RTRIM(@Param_PosNr)
   ,[KtoTyp] = UPPER(RTRIM(@Param_KtoTyp))
   ,[KtoVon] = UPPER(RTRIM(@Param_KtoVon))
   ,[KtoBis] = UPPER(RTRIM(@Param_KtoBis))
   ,[Text] = RTRIM(@Param_Text)
   ,[Fett] = @Param_Fett
   ,[FormFeed] = @Param_FormFeed
   ,[LineFeed] = @Param_LineFeed
   ,[LineFeed2] = @Param_LineFeed2
   ,[Header] = @Param_Header
   ,[Footer] = @Param_Footer
   ,[HideEmpty] = @Param_HideEmpty
   ,[SumUp] = @Param_SumUp
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_Upd2_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Bilanzzeilen] @Param_RecId varchar(20)
  ,@Param_LstNr varchar(4)
  ,@Param_LfdNr varchar(8)
  ,@Param_Tiefe varchar(1)
  ,@Param_Still varchar(1)
  ,@Param_PosTyp varchar(1)
  ,@Param_PosNr varchar(20)
  ,@Param_KtoTyp varchar(1)
  ,@Param_KtoVon varchar(8)
  ,@Param_KtoBis varchar(8)
  ,@Param_Text varchar(50)
  ,@Param_Fett varchar(1)
  ,@Param_FormFeed varchar(1)
  ,@Param_LineFeed varchar(1)
  ,@Param_LineFeed2 varchar(1)
  ,@Param_Header varchar(1)
  ,@Param_Footer varchar(1)
  ,@Param_HideEmpty varchar(1)
  ,@Param_SumUp varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Bilanzzeilen] SET
    [LstNr] = UPPER(RTRIM(@Param_LstNr))
   ,[LfdNr] = dbo.itcfunc_integer(@Param_LfdNr)
   ,[Tiefe] = dbo.itcfunc_integer(@Param_Tiefe)
   ,[Still] = dbo.itcfunc_integer(@Param_Still)
   ,[PosTyp] = dbo.itcfunc_integer(@Param_PosTyp)
   ,[PosNr] = RTRIM(@Param_PosNr)
   ,[KtoTyp] = UPPER(RTRIM(@Param_KtoTyp))
   ,[KtoVon] = UPPER(RTRIM(@Param_KtoVon))
   ,[KtoBis] = UPPER(RTRIM(@Param_KtoBis))
   ,[Text] = RTRIM(@Param_Text)
   ,[Fett] = dbo.itcfunc_integer(@Param_Fett)
   ,[FormFeed] = dbo.itcfunc_integer(@Param_FormFeed)
   ,[LineFeed] = dbo.itcfunc_integer(@Param_LineFeed)
   ,[LineFeed2] = dbo.itcfunc_integer(@Param_LineFeed2)
   ,[Header] = dbo.itcfunc_integer(@Param_Header)
   ,[Footer] = dbo.itcfunc_integer(@Param_Footer)
   ,[HideEmpty] = dbo.itcfunc_integer(@Param_HideEmpty)
   ,[SumUp] = dbo.itcfunc_integer(@Param_SumUp)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Bilanzzeilen] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Bilanzzeilen')
  DROP PROCEDURE [dbo].[up_TrSel_Bilanzzeilen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Bilanzzeilen]
AS
  SELECT RecId,
    LTRIM(RTRIM([LstNr]))
   +LTRIM(RTRIM([PosNr]))
   +LTRIM(RTRIM([KtoTyp]))
   +LTRIM(RTRIM([KtoVon]))
   +LTRIM(RTRIM([KtoBis]))
   +LTRIM(RTRIM([Text]))
  AS Data FROM [dbo].[ut_Bilanzzeilen] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bilanzzeilen_Key00')
  DROP PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key00] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldLfdNr integer
  SELECT @oldLstNr = LstNr, @oldLfdNr = LfdNr FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00)
    WHERE (LstNr = @oldLstNr) AND (LfdNr > @oldLfdNr)
    ORDER BY LstNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00)
    WHERE (LstNr > @oldLstNr)
    ORDER BY LstNr,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_Bilanzzeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Bilanzzeilen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Bilanzzeilen_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bilanzzeilen_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key00] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldLfdNr integer
  SELECT @oldLstNr = LstNr, @oldLfdNr = LfdNr FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00)
    WHERE (LstNr = @oldLstNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)
    ORDER BY LstNr 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_Bilanzzeilen] WITH(INDEX = Ik_Key00)
    WHERE (LstNr = @oldLstNr) AND (LfdNr < @oldLfdNr)
    ORDER BY LstNr 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_Bilanzzeilen] WITH(INDEX = Ik_Key00)
    WHERE (LstNr < @oldLstNr)
    ORDER BY LstNr 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_Bilanzzeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Bilanzzeilen_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Bilanzzeilen_Key00] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bilanzzeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,5,8)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      LstNr = @mLstNr AND 
      LfdNr = @mLfdNr
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      LstNr = @mLstNr
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      LstNr >= @mLstNr AND SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bilanzzeilen_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,5,8)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      LstNr = @mLstNr AND 
      LfdNr = @mLfdNr
      ORDER BY LstNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      LstNr = @mLstNr
      ORDER BY LstNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      ORDER BY LstNr 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_Bilanzzeilen_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Bilanzzeilen_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Bilanzzeilen_Key00] @xKey, @xLen')
GO


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_First_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_Bilanzzeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) ORDER BY LstNr,KtoTyp,KtoVon,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Bilanzzeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Bilanzzeilen_Key01] AS EXECUTE [dbo].[up_First_Bilanzzeilen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_Last_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_Bilanzzeilen_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Bilanzzeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Bilanzzeilen_Key01] AS EXECUTE [dbo].[up_Last_Bilanzzeilen_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key01] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldKtoTyp char(1)
  DECLARE @oldKtoVon char(8)
  SELECT @oldLstNr = LstNr, @oldKtoTyp = KtoTyp, @oldKtoVon = KtoVon FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp = @oldKtoTyp) AND (KtoVon = @oldKtoVon) AND (RecId > @oldRecId)
    ORDER BY LstNr,KtoTyp,KtoVon,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp = @oldKtoTyp) AND (KtoVon > @oldKtoVon)
    ORDER BY LstNr,KtoTyp,KtoVon,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp > @oldKtoTyp)
    ORDER BY LstNr,KtoTyp,KtoVon,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr > @oldLstNr)
    ORDER BY LstNr,KtoTyp,KtoVon,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_Bilanzzeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Bilanzzeilen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_Bilanzzeilen_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key01] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldKtoTyp char(1)
  DECLARE @oldKtoVon char(8)
  SELECT @oldLstNr = LstNr, @oldKtoTyp = KtoTyp, @oldKtoVon = KtoVon FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp = @oldKtoTyp) AND (KtoVon = @oldKtoVon) AND (RecId < @oldRecId)
    ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp = @oldKtoTyp) AND (KtoVon < @oldKtoVon)
    ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr = @oldLstNr) AND (KtoTyp < @oldKtoTyp)
    ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01)
    WHERE (LstNr < @oldLstNr)
    ORDER BY LstNr desc,KtoTyp desc,KtoVon 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_Bilanzzeilen_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Bilanzzeilen_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Bilanzzeilen_Key01] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mKtoTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,1)
  DECLARE @mKtoVon char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,8)

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

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp AND 
      KtoVon = @mKtoVon
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr >= @mLstNr AND SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp >= @mKtoTyp AND SUBSTRING(KtoTyp,1,LEN(@mKtoTyp)) = @mKtoTyp
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp AND 
      KtoVon >= @mKtoVon AND SUBSTRING(KtoVon,1,LEN(@mKtoVon)) = @mKtoVon
      ORDER BY LstNr,KtoTyp,KtoVon,RecId
    RETURN
  END

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bilanzzeilen_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mKtoTyp char(1) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,5,1)
  DECLARE @mKtoVon char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,6,8)

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

  IF @xLen = 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp AND 
      KtoVon = @mKtoVon
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

  IF @xLen < 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      SUBSTRING(KtoTyp,1,LEN(@mKtoTyp)) = @mKtoTyp
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

  IF @xLen < 13 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key01) WHERE 
      LstNr = @mLstNr AND 
      KtoTyp = @mKtoTyp AND 
      SUBSTRING(KtoVon,1,LEN(@mKtoVon)) = @mKtoVon
      ORDER BY LstNr desc,KtoTyp desc,KtoVon desc,RecId desc
    RETURN
  END

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


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_First_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_First_Bilanzzeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 0 = Still ORDER BY LstNr,LfdNr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Bilanzzeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Bilanzzeilen_Key02] AS EXECUTE [dbo].[up_First_Bilanzzeilen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_Last_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Last_Bilanzzeilen_Key02] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 0 = Still ORDER BY LstNr desc,LfdNr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Bilanzzeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Bilanzzeilen_Key02] AS EXECUTE [dbo].[up_Last_Bilanzzeilen_Key02]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Next_Bilanzzeilen_Key02] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldLfdNr integer
  SELECT @oldLstNr = LstNr, @oldLfdNr = LfdNr FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr = @oldLstNr) AND (LfdNr = @oldLfdNr) AND (RecId > @oldRecId)) AND (0 = Still)
    ORDER BY LstNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr = @oldLstNr) AND (LfdNr > @oldLfdNr)) AND (0 = Still)
    ORDER BY LstNr,LfdNr,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr > @oldLstNr)) AND (0 = Still)
    ORDER BY LstNr,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_Bilanzzeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Bilanzzeilen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Next_Bilanzzeilen_Key02] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_Prior_Bilanzzeilen_Key02] @oldRecId integer AS
  DECLARE @oldLstNr char(4)
  DECLARE @oldLfdNr integer
  SELECT @oldLstNr = LstNr, @oldLfdNr = LfdNr FROM [dbo].[ut_Bilanzzeilen] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr = @oldLstNr) AND (LfdNr = @oldLfdNr) AND (RecId < @oldRecId)) AND (0 = Still)
    ORDER BY LstNr 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_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr = @oldLstNr) AND (LfdNr < @oldLfdNr)) AND (0 = Still)
    ORDER BY LstNr 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_Bilanzzeilen] WITH(INDEX = Ik_Key02)
    WHERE ((LstNr < @oldLstNr)) AND (0 = Still)
    ORDER BY LstNr 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_Bilanzzeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Bilanzzeilen_Key02] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Bilanzzeilen_Key02] @oldRecId')
GO

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindF_Bilanzzeilen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,5,8)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      LstNr = @mLstNr AND 
      LfdNr = @mLfdNr
      AND (0 = Still)
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      LstNr = @mLstNr
      AND (0 = Still)
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      LstNr >= @mLstNr AND SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      AND (0 = Still)
      ORDER BY LstNr,LfdNr,RecId
    RETURN
  END


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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bilanzzeilen_Key02')
  DROP PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key02]
GO
CREATE PROCEDURE [dbo].[up_FindL_Bilanzzeilen_Key02] @xKey char(250), @xLen integer AS
  DECLARE @mLstNr char(4) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,4)
  DECLARE @mLfdNr integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,5,8)

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

  IF @xLen = 12 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      LstNr = @mLstNr AND 
      LfdNr = @mLfdNr
      AND (0 = Still)
      ORDER BY LstNr desc,LfdNr desc,RecId desc
    RETURN
  END

  IF @xLen = 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      LstNr = @mLstNr
      AND (0 = Still)
      ORDER BY LstNr desc,LfdNr desc,RecId desc
    RETURN
  END

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

  IF @xLen < 4 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Bilanzzeilen] WITH(INDEX = Ik_Key02) WHERE 
      SUBSTRING(LstNr,1,LEN(@mLstNr)) = @mLstNr
      AND (0 = Still)
      ORDER BY LstNr 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_Bilanzzeilen_Key02')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Bilanzzeilen_Key02] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Bilanzzeilen_Key02] @xKey, @xLen')
GO


