------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Buchungskladde')
BEGIN
CREATE TABLE [dbo].[ut_Buchungskladde]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[TransNo] char(8) DEFAULT ' '
  ,[LfdNr] char(8) DEFAULT ' '
  ,[Jahr] char(4) DEFAULT ' '
  ,[Monat] char(2) DEFAULT ' '
  ,[Tag] char(2) DEFAULT ' '
  ,[KtoSoll] char(8) DEFAULT ' '
  ,[KtoHaben] char(8) DEFAULT ' '
  ,[UstSchl] char(3) DEFAULT ' '
  ,[Betrag] float DEFAULT 0
  ,[Waehrung] char(6) DEFAULT ' '
  ,[BuText] char(30) DEFAULT ' '
  ,[BelegNr] char(8) DEFAULT ' '
  ,[Verb_Bsh] tinyint DEFAULT 0
  ,[Verb_Ext] tinyint DEFAULT 0
  ,[Kst] char(20) DEFAULT ' '
  ,[Ktr] char(20) DEFAULT ' '
  ,[Faellig] integer DEFAULT 0
  ,[SkoFrist] integer DEFAULT 0
  ,[SkoSatz] float DEFAULT 0
  ,[Name1] char(40) DEFAULT ' '
  ,[Name2] char(40) DEFAULT ' '
  ,[Strasse] char(40) DEFAULT ' '
  ,[Plz] char(8) DEFAULT ' '
  ,[Ort] char(40) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[KontoArtS] integer DEFAULT 0
  ,[KontoArtH] integer DEFAULT 0
  CONSTRAINT [Pk_Buchungskladde]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Buchungskladde_KeyDel')
  DROP TRIGGER [dbo].[tr_Buchungskladde_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Buchungskladde_KeyUpd')
  DROP TRIGGER [dbo].[tr_Buchungskladde_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Buchungskladde_Keys')
  DROP TABLE [dbo].[ut_Buchungskladde_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [TransNo] 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_Buchungskladde' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [LfdNr] 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_Buchungskladde' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Jahr] 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_Buchungskladde' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Monat] 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_Buchungskladde' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Tag] 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_Buchungskladde' and sc.[name] = 'KtoSoll')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [KtoSoll] 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_Buchungskladde' and sc.[name] = 'KtoHaben')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [KtoHaben] 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_Buchungskladde' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [UstSchl] char(3) 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_Buchungskladde' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Betrag] float 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_Buchungskladde' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Waehrung] char(6) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'BuText')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [BuText] char(30) 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_Buchungskladde' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [BelegNr] 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_Buchungskladde' and sc.[name] = 'Verb_Bsh')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Verb_Bsh] 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_Buchungskladde' and sc.[name] = 'Verb_Ext')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Verb_Ext] 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_Buchungskladde' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Kst] 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_Buchungskladde' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Ktr] 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_Buchungskladde' and sc.[name] = 'Faellig')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Faellig] 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_Buchungskladde' and sc.[name] = 'SkoFrist')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [SkoFrist] 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_Buchungskladde' and sc.[name] = 'SkoSatz')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [SkoSatz] float 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_Buchungskladde' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Name1] char(40) 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_Buchungskladde' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Name2] char(40) 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_Buchungskladde' and sc.[name] = 'Strasse')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Strasse] char(40) 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_Buchungskladde' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Plz] 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_Buchungskladde' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [Ort] char(40) 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_Buchungskladde' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [PrjNr] char(8) NOT NULL DEFAULT ' '
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'KontoArtS')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [KontoArtS] 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_Buchungskladde' and sc.[name] = 'KontoArtH')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ADD [KontoArtH] integer NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'TransNo')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [TransNo] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [LfdNr] char(8)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Jahr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Monat] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Tag')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Tag] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'KtoSoll')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [KtoSoll] char(8)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'KtoHaben')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [KtoHaben] char(8)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'UstSchl')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [UstSchl] char(3)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Waehrung')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Waehrung] char(6)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'BuText')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [BuText] char(30)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'BelegNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [BelegNr] char(8)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Kst')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Kst] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Ktr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Ktr] char(20)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Name1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Name2] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Strasse')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Strasse] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Plz')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Plz] char(8)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'Ort')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [Ort] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Buchungskladde' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_Buchungskladde] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Buchungskladde')
  DROP VIEW [dbo].[Buchungskladde]
GO
CREATE VIEW [dbo].[Buchungskladde] AS
  SELECT * FROM [dbo].[ut_Buchungskladde]
GO
CREATE TABLE [dbo].[ut_Buchungskladde_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(26) NOT NULL
  ,[Key01_Value] CHAR(90) NOT NULL
  CONSTRAINT [Pk_ut_Buchungskladde_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Buchungskladde_KeyDel] ON [dbo].[ut_Buchungskladde]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Buchungskladde_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_Buchungskladde_KeyUpd] ON [dbo].[ut_Buchungskladde]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_Buchungskladde_Keys] WITH (SERIALIZABLE)
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_Buchungskladde_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[TransNo])+convert(char(8),[LfdNr])+STR(RecId,10))
     ,UPPER(convert(char(8),[TransNo])+convert(char(8),[KtoSoll])+convert(char(8),[KtoHaben])+convert(char(20),[Kst])+convert(char(20),[Ktr])+convert(char(8),[PrjNr])+convert(char(8),[LfdNr])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Buchungskladde_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(8),[TransNo])+convert(char(8),[LfdNr])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(8),[TransNo])+convert(char(8),[KtoSoll])+convert(char(8),[KtoHaben])+convert(char(20),[Kst])+convert(char(20),[Ktr])+convert(char(8),[PrjNr])+convert(char(8),[LfdNr])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

UPDATE [dbo].[ut_Buchungskladde] SET RecId = RecId
GO


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Add_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Add_Buchungskladde] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Buchungskladde])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Buchungskladde] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Upd_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Upd_Buchungskladde]
  @RI integer
 ,@Parm_TransNo char(8)
 ,@Parm_LfdNr char(8)
 ,@Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Tag char(2)
 ,@Parm_KtoSoll char(8)
 ,@Parm_KtoHaben char(8)
 ,@Parm_UstSchl char(3)
 ,@Parm_Betrag float
 ,@Parm_Waehrung char(6)
 ,@Parm_BuText char(30)
 ,@Parm_BelegNr char(8)
 ,@Parm_Verb_Bsh tinyint
 ,@Parm_Verb_Ext tinyint
 ,@Parm_Kst char(20)
 ,@Parm_Ktr char(20)
 ,@Parm_Faellig integer
 ,@Parm_SkoFrist integer
 ,@Parm_SkoSatz float
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Strasse char(40)
 ,@Parm_Plz char(8)
 ,@Parm_Ort char(40)
 ,@Parm_PrjNr char(8)
 ,@Parm_KontoArtS integer
 ,@Parm_KontoArtH integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Buchungskladde] SET
     [TransNo] = RTRIM(@Parm_TransNo)
    ,[LfdNr] = RTRIM(@Parm_LfdNr)
    ,[Jahr] = RTRIM(@Parm_Jahr)
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[Tag] = RTRIM(@Parm_Tag)
    ,[KtoSoll] = RTRIM(@Parm_KtoSoll)
    ,[KtoHaben] = RTRIM(@Parm_KtoHaben)
    ,[UstSchl] = RTRIM(@Parm_UstSchl)
    ,[Betrag] = @Parm_Betrag
    ,[Waehrung] = RTRIM(@Parm_Waehrung)
    ,[BuText] = RTRIM(@Parm_BuText)
    ,[BelegNr] = RTRIM(@Parm_BelegNr)
    ,[Verb_Bsh] = @Parm_Verb_Bsh
    ,[Verb_Ext] = @Parm_Verb_Ext
    ,[Kst] = RTRIM(@Parm_Kst)
    ,[Ktr] = RTRIM(@Parm_Ktr)
    ,[Faellig] = @Parm_Faellig
    ,[SkoFrist] = @Parm_SkoFrist
    ,[SkoSatz] = @Parm_SkoSatz
    ,[Name1] = RTRIM(@Parm_Name1)
    ,[Name2] = RTRIM(@Parm_Name2)
    ,[Strasse] = RTRIM(@Parm_Strasse)
    ,[Plz] = RTRIM(@Parm_Plz)
    ,[Ort] = RTRIM(@Parm_Ort)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[KontoArtS] = @Parm_KontoArtS
    ,[KontoArtH] = @Parm_KontoArtH
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Sel_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Sel_Buchungskladde]
  @RI integer
AS
SELECT
     [TransNo]
    ,[LfdNr]
    ,[Jahr]
    ,[Monat]
    ,[Tag]
    ,[KtoSoll]
    ,[KtoHaben]
    ,[UstSchl]
    ,[Betrag]
    ,[Waehrung]
    ,[BuText]
    ,[BelegNr]
    ,[Verb_Bsh]
    ,[Verb_Ext]
    ,[Kst]
    ,[Ktr]
    ,[Faellig]
    ,[SkoFrist]
    ,[SkoSatz]
    ,[Name1]
    ,[Name2]
    ,[Strasse]
    ,[Plz]
    ,[Ort]
    ,[PrjNr]
    ,[KontoArtS]
    ,[KontoArtH]
  FROM [dbo].[ut_Buchungskladde]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Del_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Del_Buchungskladde]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Buchungskladde] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Reorg_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Buchungskladde]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Buchungskladde] 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_Buchungskladde] 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_Buchungskladde] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Buchungskladde')
  DROP PROCEDURE [dbo].[up_Append_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_Append_Buchungskladde]
  @Parm_TransNo char(8)
 ,@Parm_LfdNr char(8)
 ,@Parm_Jahr char(4)
 ,@Parm_Monat char(2)
 ,@Parm_Tag char(2)
 ,@Parm_KtoSoll char(8)
 ,@Parm_KtoHaben char(8)
 ,@Parm_UstSchl char(3)
 ,@Parm_Betrag float
 ,@Parm_Waehrung char(6)
 ,@Parm_BuText char(30)
 ,@Parm_BelegNr char(8)
 ,@Parm_Verb_Bsh tinyint
 ,@Parm_Verb_Ext tinyint
 ,@Parm_Kst char(20)
 ,@Parm_Ktr char(20)
 ,@Parm_Faellig integer
 ,@Parm_SkoFrist integer
 ,@Parm_SkoSatz float
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Strasse char(40)
 ,@Parm_Plz char(8)
 ,@Parm_Ort char(40)
 ,@Parm_PrjNr char(8)
 ,@Parm_KontoArtS integer
 ,@Parm_KontoArtH integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Buchungskladde])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Buchungskladde] (
    RecId
   ,[TransNo]
   ,[LfdNr]
   ,[Jahr]
   ,[Monat]
   ,[Tag]
   ,[KtoSoll]
   ,[KtoHaben]
   ,[UstSchl]
   ,[Betrag]
   ,[Waehrung]
   ,[BuText]
   ,[BelegNr]
   ,[Verb_Bsh]
   ,[Verb_Ext]
   ,[Kst]
   ,[Ktr]
   ,[Faellig]
   ,[SkoFrist]
   ,[SkoSatz]
   ,[Name1]
   ,[Name2]
   ,[Strasse]
   ,[Plz]
   ,[Ort]
   ,[PrjNr]
   ,[KontoArtS]
   ,[KontoArtH]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_TransNo)
   ,RTRIM(@Parm_LfdNr)
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_Monat)
   ,RTRIM(@Parm_Tag)
   ,RTRIM(@Parm_KtoSoll)
   ,RTRIM(@Parm_KtoHaben)
   ,RTRIM(@Parm_UstSchl)
   ,@Parm_Betrag
   ,RTRIM(@Parm_Waehrung)
   ,RTRIM(@Parm_BuText)
   ,RTRIM(@Parm_BelegNr)
   ,@Parm_Verb_Bsh
   ,@Parm_Verb_Ext
   ,RTRIM(@Parm_Kst)
   ,RTRIM(@Parm_Ktr)
   ,@Parm_Faellig
   ,@Parm_SkoFrist
   ,@Parm_SkoSatz
   ,RTRIM(@Parm_Name1)
   ,RTRIM(@Parm_Name2)
   ,RTRIM(@Parm_Strasse)
   ,RTRIM(@Parm_Plz)
   ,RTRIM(@Parm_Ort)
   ,RTRIM(@Parm_PrjNr)
   ,@Parm_KontoArtS
   ,@Parm_KontoArtH
       )
  IF @@ERROR = 0
  BEGIN
    SELECT @NeueId AS RecId
    RETURN
  END
  SELECT 0 AS RecId
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Buchungskladde')
  DROP PROCEDURE [dbo].[up_TrSel_Buchungskladde]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Buchungskladde]
AS
  SELECT   RecId,
      LTRIM(RTRIM([TransNo]))
      +LTRIM(RTRIM([LfdNr]))
      +LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([Monat]))
      +LTRIM(RTRIM([Tag]))
      +LTRIM(RTRIM([KtoSoll]))
      +LTRIM(RTRIM([KtoHaben]))
      +LTRIM(RTRIM([UstSchl]))
      +LTRIM(RTRIM([Waehrung]))
      +LTRIM(RTRIM([BuText]))
      +LTRIM(RTRIM([BelegNr]))
      +LTRIM(RTRIM([Kst]))
      +LTRIM(RTRIM([Ktr]))
      +LTRIM(RTRIM([Name1]))
      +LTRIM(RTRIM([Name2]))
      +LTRIM(RTRIM([Strasse]))
      +LTRIM(RTRIM([Plz]))
      +LTRIM(RTRIM([Ort]))
      +LTRIM(RTRIM([PrjNr]))
    AS Data
  FROM     [dbo].[ut_Buchungskladde]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_Buchungskladde_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_First_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_Last_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_Next_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Buchungskladde_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungskladde_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Buchungskladde_Key00]
GO

CREATE PROCEDURE [dbo].[up_First_Buchungskladde_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys] ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Buchungskladde_Key00]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys] ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Buchungskladde_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_Buchungskladde_Key00] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_Buchungskladde_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key00_Value] > (SELECT [Key00_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_Buchungskladde_Key00] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key00_Value] < (SELECT [Key00_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key00_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_Buchungskladde_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_Buchungskladde_Keys]
      WHERE [Key00_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key00_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_Buchungskladde_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

CREATE INDEX [Ik_Key01] ON [dbo].[ut_Buchungskladde_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_First_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_Last_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_Prior_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_Next_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_Next50_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_FindL_Buchungskladde_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Buchungskladde_Key01')
  DROP PROCEDURE [dbo].[up_FindF_Buchungskladde_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_Buchungskladde_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Buchungskladde_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Buchungskladde_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_Buchungskladde_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_Buchungskladde_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_Buchungskladde_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key01_Value] < (SELECT [Key01_Value] FROM [dbo].[ut_Buchungskladde_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_FindF_Buchungskladde_Key01] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key01_Value] AS Key_Value FROM [dbo].[ut_Buchungskladde_Keys]
      WHERE [Key01_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [Key01_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_Buchungskladde_Key01] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Buchungskladde_Keys]
  WHERE [Key01_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key01_Value] DESC
GO

