------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Elster')
BEGIN
CREATE TABLE [dbo].[ut_Elster]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Jahr] char(4) DEFAULT ' '
  ,[UstSpalte] char(3) DEFAULT ' '
  ,[KZ] integer DEFAULT 0
  ,[Laenge] integer DEFAULT 0
  ,[Nachkomma] integer DEFAULT 0
  ,[bSumme] tinyint DEFAULT 0
  ,[VZUmkehr] tinyint DEFAULT 0
  CONSTRAINT [Pk_Elster]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Elster_KeyDel')
  DROP TRIGGER [dbo].[tr_Elster_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Elster_KeyUpd')
  DROP TRIGGER [dbo].[tr_Elster_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Elster_Keys')
  DROP TABLE [dbo].[ut_Elster_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Elster' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] 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_Elster' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [UstSpalte] 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_Elster' and sc.[name] = 'KZ')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [KZ] 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_Elster' and sc.[name] = 'Laenge')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [Laenge] 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_Elster' and sc.[name] = 'Nachkomma')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [Nachkomma] 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_Elster' and sc.[name] = 'bSumme')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [bSumme] 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_Elster' and sc.[name] = 'VZUmkehr')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ADD [VZUmkehr] tinyint NOT NULL DEFAULT 0
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Elster' and sc.[name] = 'Jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ALTER COLUMN [Jahr] char(4)
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Elster' and sc.[name] = 'UstSpalte')
BEGIN
  ALTER TABLE [dbo].[ut_Elster] ALTER COLUMN [UstSpalte] char(3)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Elster')
  DROP VIEW [dbo].[Elster]
GO
CREATE VIEW [dbo].[Elster] AS
  SELECT * FROM [dbo].[ut_Elster]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Elster')
  DROP PROCEDURE [dbo].[up_Add_Elster]
GO
CREATE PROCEDURE [dbo].[up_Add_Elster] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Elster])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Elster] (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_Elster')
  DROP PROCEDURE [dbo].[up_Upd_Elster]
GO
CREATE PROCEDURE [dbo].[up_Upd_Elster]
  @RI integer
 ,@Parm_Jahr char(4)
 ,@Parm_UstSpalte char(3)
 ,@Parm_KZ integer
 ,@Parm_Laenge integer
 ,@Parm_Nachkomma integer
 ,@Parm_bSumme tinyint
 ,@Parm_VZUmkehr tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Elster] SET
     [Jahr] = RTRIM(@Parm_Jahr)
    ,[UstSpalte] = RTRIM(@Parm_UstSpalte)
    ,[KZ] = @Parm_KZ
    ,[Laenge] = @Parm_Laenge
    ,[Nachkomma] = @Parm_Nachkomma
    ,[bSumme] = @Parm_bSumme
    ,[VZUmkehr] = @Parm_VZUmkehr
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Elster')
  DROP PROCEDURE [dbo].[up_Sel_Elster]
GO
CREATE PROCEDURE [dbo].[up_Sel_Elster]
  @RI integer
AS
SELECT
     [Jahr]
    ,[UstSpalte]
    ,[KZ]
    ,[Laenge]
    ,[Nachkomma]
    ,[bSumme]
    ,[VZUmkehr]
  FROM [dbo].[ut_Elster]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Elster')
  DROP PROCEDURE [dbo].[up_Del_Elster]
GO
CREATE PROCEDURE [dbo].[up_Del_Elster]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Elster] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Elster')
  DROP PROCEDURE [dbo].[up_Reorg_Elster]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Elster]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Elster] 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_Elster] 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_Elster] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Elster')
  DROP PROCEDURE [dbo].[up_Append_Elster]
GO
CREATE PROCEDURE [dbo].[up_Append_Elster]
  @Parm_Jahr char(4)
 ,@Parm_UstSpalte char(3)
 ,@Parm_KZ integer
 ,@Parm_Laenge integer
 ,@Parm_Nachkomma integer
 ,@Parm_bSumme tinyint
 ,@Parm_VZUmkehr tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Elster])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Elster] (
    RecId
   ,[Jahr]
   ,[UstSpalte]
   ,[KZ]
   ,[Laenge]
   ,[Nachkomma]
   ,[bSumme]
   ,[VZUmkehr]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Jahr)
   ,RTRIM(@Parm_UstSpalte)
   ,@Parm_KZ
   ,@Parm_Laenge
   ,@Parm_Nachkomma
   ,@Parm_bSumme
   ,@Parm_VZUmkehr
       )
  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_Elster')
  DROP PROCEDURE [dbo].[up_TrSel_Elster]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Elster]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Jahr]))
      +LTRIM(RTRIM([UstSpalte]))
    AS Data
  FROM     [dbo].[ut_Elster]
  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_Elster'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_Elster]
(Jahr,UstSpalte,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Elster_Key00')
  DROP PROCEDURE [dbo].[up_First_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Elster_Key00')
  DROP PROCEDURE [dbo].[up_Last_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Elster_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Elster_Key00')
  DROP PROCEDURE [dbo].[up_Next_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Elster_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Elster_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Elster_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Elster_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Elster_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Elster_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus Elster in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_Elster_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Elster] ORDER BY Jahr,UstSpalte,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus Elster in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_Elster_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Elster] ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY Jahr,UstSpalte,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_Elster_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldUstSpalte char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,UstSpalte FROM [dbo].[ut_Elster] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldUstSpalte
  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_Elster]
                   WHERE Jahr = @oldJahr AND UstSpalte = @oldUstSpalte AND RecId > @oldRecId
                   ORDER BY Jahr,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
                   WHERE Jahr = @oldJahr AND UstSpalte > @oldUstSpalte
                   ORDER BY Jahr,UstSpalte,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
                   WHERE Jahr > @oldJahr
                   ORDER BY Jahr,UstSpalte,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 Jahr DESC,UstSpalte DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_Elster_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldJahr char(4)
  DECLARE @oldUstSpalte char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT Jahr,UstSpalte FROM [dbo].[ut_Elster] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldJahr,@oldUstSpalte
  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_Elster]
                   WHERE Jahr = @oldJahr AND UstSpalte = @oldUstSpalte AND RecId < @oldRecId
                   ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
                   WHERE Jahr = @oldJahr AND UstSpalte < @oldUstSpalte
                   ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
                   WHERE Jahr < @oldJahr
                   ORDER BY Jahr DESC,UstSpalte 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 Jahr,UstSpalte,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_Elster_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(UstSpalte,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND UstSpalte = SUBSTRING(@xKey,5,3)
      ORDER BY Jahr,UstSpalte,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

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

  IF @xLen < 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE SUBSTRING(Jahr,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 4
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen < 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND SUBSTRING(UstSpalte,1,@xLen-4) = SUBSTRING(@xKey,5,@xLen-4)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  IF @xLen = 7
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Elster]
      WHERE Jahr = SUBSTRING(@xKey,1,4) AND UstSpalte = SUBSTRING(@xKey,5,3)
      ORDER BY Jahr DESC,UstSpalte DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

