-- 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_KladdenKopf')
BEGIN
CREATE TABLE [dbo].[ut_KladdenKopf]
(
  [RecId]  integer NOT NULL
 ,[Code] char(20) DEFAULT ''
 ,[Bez] char(50) DEFAULT ''
 ,[Kto] char(8) DEFAULT ''
 ,[GK_Kto] char(8) DEFAULT ''
 ,[DupBuchDat] tinyint DEFAULT 0
 ,[DupBelDat] tinyint DEFAULT 0
 ,[CpyBuchDat] tinyint DEFAULT 0
 ,[CpyBelDat] tinyint DEFAULT 0
 ,[CpyKto] tinyint DEFAULT 0
 ,[CpyGK_Kto] tinyint DEFAULT 0
 ,[CpyBelNr] tinyint DEFAULT 0
 ,[CpyKst] tinyint DEFAULT 0
 ,[CpyKtr] tinyint DEFAULT 0
 ,[CpyExtBelN] tinyint DEFAULT 0
 ,[CpyVorgang] tinyint DEFAULT 0
 ,[Layout] integer DEFAULT 0
 ,[AbstKonto] char(8) DEFAULT ''
 ,[ShowAll] tinyint DEFAULT 0
 ,[AbwMandant] char(20) DEFAULT ''
 ,[VkzExport] integer DEFAULT 0
  CONSTRAINT [Pk_KladdenKopf]  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_KladdenKopf_KeyDel')
  DROP TRIGGER [dbo].[tr_KladdenKopf_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_KladdenKopf_KeyUpd')
  DROP TRIGGER [dbo].[tr_KladdenKopf_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_KladdenKopf_Keys')
  DROP TABLE [dbo].[ut_KladdenKopf_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_KladdenKopf' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [Code] char(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_KladdenKopf' and sc.[name] = 'Bez')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [Bez] char(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_KladdenKopf' and sc.[name] = 'Kto')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [Kto] 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_KladdenKopf' and sc.[name] = 'GK_Kto')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [GK_Kto] 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_KladdenKopf' and sc.[name] = 'DupBuchDat')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [DupBuchDat] 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_KladdenKopf' and sc.[name] = 'DupBelDat')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [DupBelDat] 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_KladdenKopf' and sc.[name] = 'CpyBuchDat')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyBuchDat] 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_KladdenKopf' and sc.[name] = 'CpyBelDat')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyBelDat] 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_KladdenKopf' and sc.[name] = 'CpyKto')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyKto] 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_KladdenKopf' and sc.[name] = 'CpyGK_Kto')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyGK_Kto] 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_KladdenKopf' and sc.[name] = 'CpyBelNr')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyBelNr] 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_KladdenKopf' and sc.[name] = 'CpyKst')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyKst] 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_KladdenKopf' and sc.[name] = 'CpyKtr')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyKtr] 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_KladdenKopf' and sc.[name] = 'CpyExtBelN')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyExtBelN] 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_KladdenKopf' and sc.[name] = 'CpyVorgang')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [CpyVorgang] 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_KladdenKopf' and sc.[name] = 'Layout')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [Layout] 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_KladdenKopf' and sc.[name] = 'AbstKonto')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [AbstKonto] 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_KladdenKopf' and sc.[name] = 'ShowAll')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [ShowAll] 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_KladdenKopf' and sc.[name] = 'AbwMandant')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [AbwMandant] char(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_KladdenKopf' and sc.[name] = 'VkzExport')
BEGIN
  ALTER TABLE [dbo].[ut_KladdenKopf] ADD [VkzExport] integer NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Code'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Code] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Code'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Code] varchar(20)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Bez'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Bez] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Bez'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Bez'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Bez] varchar(50)
END
GO
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Kto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Kto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Kto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Kto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'Kto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [Kto] 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_KladdenKopf' and sc.[name] = 'GK_Kto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'GK_Kto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [GK_Kto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'GK_Kto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'GK_Kto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [GK_Kto] 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_KladdenKopf' and sc.[name] = 'AbstKonto'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbstKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [AbstKonto] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbstKonto'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbstKonto'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [AbstKonto] varchar(8)
END
GO
IF (20 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbwMandant'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbwMandant'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [AbwMandant] char(20)
END
GO
IF (20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbwMandant'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_KladdenKopf' and sc.[name] = 'AbwMandant'))
BEGIN
   ALTER TABLE [dbo].[ut_KladdenKopf] ALTER COLUMN [AbwMandant] varchar(20)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'KladdenKopf')
  DROP VIEW [dbo].[KladdenKopf]
GO
CREATE VIEW [dbo].[KladdenKopf] AS
  SELECT [RecId]
 ,[Code]
 ,[Bez]
 ,[Kto]
 ,[GK_Kto]
 ,[DupBuchDat]
 ,[DupBelDat]
 ,[CpyBuchDat]
 ,[CpyBelDat]
 ,[CpyKto]
 ,[CpyGK_Kto]
 ,[CpyBelNr]
 ,[CpyKst]
 ,[CpyKtr]
 ,[CpyExtBelN]
 ,[CpyVorgang]
 ,[Layout]
 ,[AbstKonto]
 ,[ShowAll]
 ,[AbwMandant]
 ,[VkzExport]
  FROM [dbo].[ut_KladdenKopf]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_KladdenKopf')
  DROP PROCEDURE [dbo].[up_Sel_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Sel_KladdenKopf]  @mRI integer
AS
  SELECT
    [Code]
   ,[Bez]
   ,[Kto]
   ,[GK_Kto]
   ,[DupBuchDat]
   ,[DupBelDat]
   ,[CpyBuchDat]
   ,[CpyBelDat]
   ,[CpyKto]
   ,[CpyGK_Kto]
   ,[CpyBelNr]
   ,[CpyKst]
   ,[CpyKtr]
   ,[CpyExtBelN]
   ,[CpyVorgang]
   ,[Layout]
   ,[AbstKonto]
   ,[ShowAll]
   ,[AbwMandant]
   ,[VkzExport]
  FROM [dbo].[ut_KladdenKopf] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_KladdenKopf')
  DROP PROCEDURE [dbo].[up_Append_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Append_KladdenKopf]
   @Param_Code char(20)
  ,@Param_Bez char(50)
  ,@Param_Kto char(8)
  ,@Param_GK_Kto char(8)
  ,@Param_DupBuchDat tinyint
  ,@Param_DupBelDat tinyint
  ,@Param_CpyBuchDat tinyint
  ,@Param_CpyBelDat tinyint
  ,@Param_CpyKto tinyint
  ,@Param_CpyGK_Kto tinyint
  ,@Param_CpyBelNr tinyint
  ,@Param_CpyKst tinyint
  ,@Param_CpyKtr tinyint
  ,@Param_CpyExtBelN tinyint
  ,@Param_CpyVorgang tinyint
  ,@Param_Layout integer
  ,@Param_AbstKonto char(8)
  ,@Param_ShowAll tinyint
  ,@Param_AbwMandant char(20)
  ,@Param_VkzExport integer
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_KladdenKopf()
      INSERT INTO [dbo].[ut_KladdenKopf] (RecId
        ,[Code]
        ,[Bez]
        ,[Kto]
        ,[GK_Kto]
        ,[DupBuchDat]
        ,[DupBelDat]
        ,[CpyBuchDat]
        ,[CpyBelDat]
        ,[CpyKto]
        ,[CpyGK_Kto]
        ,[CpyBelNr]
        ,[CpyKst]
        ,[CpyKtr]
        ,[CpyExtBelN]
        ,[CpyVorgang]
        ,[Layout]
        ,[AbstKonto]
        ,[ShowAll]
        ,[AbwMandant]
        ,[VkzExport]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,RTRIM(@Param_Bez)
        ,UPPER(RTRIM(@Param_Kto))
        ,UPPER(RTRIM(@Param_GK_Kto))
        ,@Param_DupBuchDat
        ,@Param_DupBelDat
        ,@Param_CpyBuchDat
        ,@Param_CpyBelDat
        ,@Param_CpyKto
        ,@Param_CpyGK_Kto
        ,@Param_CpyBelNr
        ,@Param_CpyKst
        ,@Param_CpyKtr
        ,@Param_CpyExtBelN
        ,@Param_CpyVorgang
        ,@Param_Layout
        ,UPPER(RTRIM(@Param_AbstKonto))
        ,@Param_ShowAll
        ,RTRIM(@Param_AbwMandant)
        ,@Param_VkzExport
          )
      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_KladdenKopf')
  DROP PROCEDURE [dbo].[up_Add_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Add_KladdenKopf]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_KladdenKopf()
  INSERT INTO [dbo].[ut_KladdenKopf] (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_KladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd_KladdenKopf] @mRI integer
  ,@Param_Code char(20)
  ,@Param_Bez char(50)
  ,@Param_Kto char(8)
  ,@Param_GK_Kto char(8)
  ,@Param_DupBuchDat tinyint
  ,@Param_DupBelDat tinyint
  ,@Param_CpyBuchDat tinyint
  ,@Param_CpyBelDat tinyint
  ,@Param_CpyKto tinyint
  ,@Param_CpyGK_Kto tinyint
  ,@Param_CpyBelNr tinyint
  ,@Param_CpyKst tinyint
  ,@Param_CpyKtr tinyint
  ,@Param_CpyExtBelN tinyint
  ,@Param_CpyVorgang tinyint
  ,@Param_Layout integer
  ,@Param_AbstKonto char(8)
  ,@Param_ShowAll tinyint
  ,@Param_AbwMandant char(20)
  ,@Param_VkzExport integer
AS
  UPDATE [dbo].[ut_KladdenKopf] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Bez] = RTRIM(@Param_Bez)
   ,[Kto] = UPPER(RTRIM(@Param_Kto))
   ,[GK_Kto] = UPPER(RTRIM(@Param_GK_Kto))
   ,[DupBuchDat] = @Param_DupBuchDat
   ,[DupBelDat] = @Param_DupBelDat
   ,[CpyBuchDat] = @Param_CpyBuchDat
   ,[CpyBelDat] = @Param_CpyBelDat
   ,[CpyKto] = @Param_CpyKto
   ,[CpyGK_Kto] = @Param_CpyGK_Kto
   ,[CpyBelNr] = @Param_CpyBelNr
   ,[CpyKst] = @Param_CpyKst
   ,[CpyKtr] = @Param_CpyKtr
   ,[CpyExtBelN] = @Param_CpyExtBelN
   ,[CpyVorgang] = @Param_CpyVorgang
   ,[Layout] = @Param_Layout
   ,[AbstKonto] = UPPER(RTRIM(@Param_AbstKonto))
   ,[ShowAll] = @Param_ShowAll
   ,[AbwMandant] = RTRIM(@Param_AbwMandant)
   ,[VkzExport] = @Param_VkzExport
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_KladdenKopf')
  DROP PROCEDURE [dbo].[up_Upd2_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_Upd2_KladdenKopf] @Param_RecId varchar(20)
  ,@Param_Code varchar(20)
  ,@Param_Bez varchar(50)
  ,@Param_Kto varchar(8)
  ,@Param_GK_Kto varchar(8)
  ,@Param_DupBuchDat varchar(1)
  ,@Param_DupBelDat varchar(1)
  ,@Param_CpyBuchDat varchar(1)
  ,@Param_CpyBelDat varchar(1)
  ,@Param_CpyKto varchar(1)
  ,@Param_CpyGK_Kto varchar(1)
  ,@Param_CpyBelNr varchar(1)
  ,@Param_CpyKst varchar(1)
  ,@Param_CpyKtr varchar(1)
  ,@Param_CpyExtBelN varchar(1)
  ,@Param_CpyVorgang varchar(1)
  ,@Param_Layout varchar(2)
  ,@Param_AbstKonto varchar(8)
  ,@Param_ShowAll varchar(1)
  ,@Param_AbwMandant varchar(20)
  ,@Param_VkzExport varchar(1)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_KladdenKopf] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Bez] = RTRIM(@Param_Bez)
   ,[Kto] = UPPER(RTRIM(@Param_Kto))
   ,[GK_Kto] = UPPER(RTRIM(@Param_GK_Kto))
   ,[DupBuchDat] = dbo.itcfunc_integer(@Param_DupBuchDat)
   ,[DupBelDat] = dbo.itcfunc_integer(@Param_DupBelDat)
   ,[CpyBuchDat] = dbo.itcfunc_integer(@Param_CpyBuchDat)
   ,[CpyBelDat] = dbo.itcfunc_integer(@Param_CpyBelDat)
   ,[CpyKto] = dbo.itcfunc_integer(@Param_CpyKto)
   ,[CpyGK_Kto] = dbo.itcfunc_integer(@Param_CpyGK_Kto)
   ,[CpyBelNr] = dbo.itcfunc_integer(@Param_CpyBelNr)
   ,[CpyKst] = dbo.itcfunc_integer(@Param_CpyKst)
   ,[CpyKtr] = dbo.itcfunc_integer(@Param_CpyKtr)
   ,[CpyExtBelN] = dbo.itcfunc_integer(@Param_CpyExtBelN)
   ,[CpyVorgang] = dbo.itcfunc_integer(@Param_CpyVorgang)
   ,[Layout] = dbo.itcfunc_integer(@Param_Layout)
   ,[AbstKonto] = UPPER(RTRIM(@Param_AbstKonto))
   ,[ShowAll] = dbo.itcfunc_integer(@Param_ShowAll)
   ,[AbwMandant] = RTRIM(@Param_AbwMandant)
   ,[VkzExport] = dbo.itcfunc_integer(@Param_VkzExport)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_KladdenKopf] @mRI
GO

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

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_KladdenKopf')
  DROP PROCEDURE [dbo].[up_TrSel_KladdenKopf]
GO
CREATE PROCEDURE [dbo].[up_TrSel_KladdenKopf]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([Bez]))
   +LTRIM(RTRIM([Kto]))
   +LTRIM(RTRIM([GK_Kto]))
   +LTRIM(RTRIM([AbstKonto]))
   +LTRIM(RTRIM([AbwMandant]))
  AS Data FROM [dbo].[ut_KladdenKopf] WHERE RecId > 0 ORDER BY RecId
GO

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

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

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

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_KladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Next_KladdenKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_KladdenKopf_Key00] @oldRecId integer AS
  DECLARE @oldCode char(20)
  SELECT @oldCode = Code FROM [dbo].[ut_KladdenKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

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

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00)
    WHERE (Code > @oldCode)
    ORDER BY Code,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_KladdenKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_KladdenKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_KladdenKopf_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_KladdenKopf_Key00')
  DROP PROCEDURE [dbo].[up_Prior_KladdenKopf_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_KladdenKopf_Key00] @oldRecId integer AS
  DECLARE @oldCode char(20)
  SELECT @oldCode = Code FROM [dbo].[ut_KladdenKopf] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00)
    WHERE (Code = @oldCode) AND (RecId < @oldRecId)
    ORDER BY Code desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00)
    WHERE (Code < @oldCode)
    ORDER BY Code 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_KladdenKopf_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_KladdenKopf_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_KladdenKopf_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code,RecId
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code,RecId
    RETURN
  END

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

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

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

  IF @xLen = 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

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

  IF @xLen < 20 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_KladdenKopf] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

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


