------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BuchungsGruppe')
BEGIN
CREATE TABLE [dbo].[ut_BuchungsGruppe]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[BuchGrp] char(10) DEFAULT ' '
  ,[Bez1] char(50) DEFAULT ' '
  ,[Bez2] char(50) DEFAULT ' '
  ,[Kommentar] char(50) DEFAULT ' '
  ,[Standard] tinyint DEFAULT 0
  ,[P13b] tinyint DEFAULT 0
  ,[SteuerFrei] tinyint DEFAULT 0
  ,[SteuerNull] tinyint DEFAULT 0
  CONSTRAINT [Pk_BuchungsGruppe]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchungsGruppe_KeyDel')
  DROP TRIGGER [dbo].[tr_BuchungsGruppe_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BuchungsGruppe_KeyUpd')
  DROP TRIGGER [dbo].[tr_BuchungsGruppe_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BuchungsGruppe_Keys')
  DROP TABLE [dbo].[ut_BuchungsGruppe_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchungsGruppe' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [BuchGrp] char(10) 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_BuchungsGruppe' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [Bez1] 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_BuchungsGruppe' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [Bez2] 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_BuchungsGruppe' and sc.[name] = 'Kommentar')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [Kommentar] 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_BuchungsGruppe' and sc.[name] = 'Standard')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [Standard] 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_BuchungsGruppe' and sc.[name] = 'P13b')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [P13b] 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_BuchungsGruppe' and sc.[name] = 'SteuerFrei')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [SteuerFrei] 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_BuchungsGruppe' and sc.[name] = 'SteuerNull')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ADD [SteuerNull] tinyint NOT NULL DEFAULT 0
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchungsGruppe' and sc.[name] = 'BuchGrp')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ALTER COLUMN [BuchGrp] char(10)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchungsGruppe' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ALTER COLUMN [Bez1] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchungsGruppe' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ALTER COLUMN [Bez2] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BuchungsGruppe' and sc.[name] = 'Kommentar')
BEGIN
  ALTER TABLE [dbo].[ut_BuchungsGruppe] ALTER COLUMN [Kommentar] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BuchungsGruppe')
  DROP VIEW [dbo].[BuchungsGruppe]
GO
CREATE VIEW [dbo].[BuchungsGruppe] AS
  SELECT * FROM [dbo].[ut_BuchungsGruppe]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Add_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Add_BuchungsGruppe] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BuchungsGruppe])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchungsGruppe] (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_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Upd_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Upd_BuchungsGruppe]
  @RI integer
 ,@Parm_BuchGrp char(10)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
 ,@Parm_Kommentar char(50)
 ,@Parm_Standard tinyint
 ,@Parm_P13b tinyint
 ,@Parm_SteuerFrei tinyint
 ,@Parm_SteuerNull tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BuchungsGruppe] SET
     [BuchGrp] = RTRIM(@Parm_BuchGrp)
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
    ,[Kommentar] = RTRIM(@Parm_Kommentar)
    ,[Standard] = @Parm_Standard
    ,[P13b] = @Parm_P13b
    ,[SteuerFrei] = @Parm_SteuerFrei
    ,[SteuerNull] = @Parm_SteuerNull
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Sel_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Sel_BuchungsGruppe]
  @RI integer
AS
SELECT
     [BuchGrp]
    ,[Bez1]
    ,[Bez2]
    ,[Kommentar]
    ,[Standard]
    ,[P13b]
    ,[SteuerFrei]
    ,[SteuerNull]
  FROM [dbo].[ut_BuchungsGruppe]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Del_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Del_BuchungsGruppe]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BuchungsGruppe] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Reorg_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BuchungsGruppe]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BuchungsGruppe] 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_BuchungsGruppe] 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_BuchungsGruppe] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_Append_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_Append_BuchungsGruppe]
  @Parm_BuchGrp char(10)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
 ,@Parm_Kommentar char(50)
 ,@Parm_Standard tinyint
 ,@Parm_P13b tinyint
 ,@Parm_SteuerFrei tinyint
 ,@Parm_SteuerNull tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BuchungsGruppe])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BuchungsGruppe] (
    RecId
   ,[BuchGrp]
   ,[Bez1]
   ,[Bez2]
   ,[Kommentar]
   ,[Standard]
   ,[P13b]
   ,[SteuerFrei]
   ,[SteuerNull]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_BuchGrp)
   ,RTRIM(@Parm_Bez1)
   ,RTRIM(@Parm_Bez2)
   ,RTRIM(@Parm_Kommentar)
   ,@Parm_Standard
   ,@Parm_P13b
   ,@Parm_SteuerFrei
   ,@Parm_SteuerNull
       )
  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_BuchungsGruppe')
  DROP PROCEDURE [dbo].[up_TrSel_BuchungsGruppe]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BuchungsGruppe]
AS
  SELECT   RecId,
      LTRIM(RTRIM([BuchGrp]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
      +LTRIM(RTRIM([Kommentar]))
    AS Data
  FROM     [dbo].[ut_BuchungsGruppe]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_BuchungsGruppe'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_BuchungsGruppe]
(BuchGrp,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_First_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_Last_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_Prior_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_Next_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_Next50_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_FindL_BuchungsGruppe_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BuchungsGruppe_Key00')
  DROP PROCEDURE [dbo].[up_FindF_BuchungsGruppe_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus BuchungsGruppe in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_BuchungsGruppe_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe] ORDER BY BuchGrp,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus BuchungsGruppe in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_BuchungsGruppe_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe] ORDER BY BuchGrp DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY BuchGrp,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_BuchungsGruppe_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchGrp FROM [dbo].[ut_BuchungsGruppe] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchGrp
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
                   WHERE BuchGrp = @oldBuchGrp AND RecId > @oldRecId
                   ORDER BY BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
                   WHERE BuchGrp > @oldBuchGrp
                   ORDER BY BuchGrp,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY BuchGrp DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_BuchungsGruppe_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldBuchGrp char(10)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT BuchGrp FROM [dbo].[ut_BuchungsGruppe] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldBuchGrp
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
                   WHERE BuchGrp = @oldBuchGrp AND RecId < @oldRecId
                   ORDER BY BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
                   WHERE BuchGrp < @oldBuchGrp
                   ORDER BY BuchGrp DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY BuchGrp,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_BuchungsGruppe_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
      WHERE SUBSTRING(BuchGrp,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchGrp,RecId
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
      WHERE BuchGrp = SUBSTRING(@xKey,1,10)
      ORDER BY BuchGrp,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY BuchGrp DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_BuchungsGruppe_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
      WHERE SUBSTRING(BuchGrp,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY BuchGrp DESC,RecId DESC
    RETURN
  END

  IF @xLen = 10
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_BuchungsGruppe]
      WHERE BuchGrp = SUBSTRING(@xKey,1,10)
      ORDER BY BuchGrp DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

