------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_RundungsTabelle')
BEGIN
CREATE TABLE [dbo].[ut_RundungsTabelle]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrsKey] char(4) DEFAULT ' '
  ,[BisPreis] float DEFAULT 0
  ,[Maske] char(10) DEFAULT ' '
  ,[ToAdd] float DEFAULT 0
  CONSTRAINT [Pk_RundungsTabelle]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RundungsTabelle_KeyDel')
  DROP TRIGGER [dbo].[tr_RundungsTabelle_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_RundungsTabelle_KeyUpd')
  DROP TRIGGER [dbo].[tr_RundungsTabelle_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_RundungsTabelle_Keys')
  DROP TABLE [dbo].[ut_RundungsTabelle_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RundungsTabelle' and sc.[name] = 'PrsKey')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ADD [PrsKey] 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_RundungsTabelle' and sc.[name] = 'BisPreis')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ADD [BisPreis] 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_RundungsTabelle' and sc.[name] = 'Maske')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ADD [Maske] 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_RundungsTabelle' and sc.[name] = 'ToAdd')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ADD [ToAdd] float 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_RundungsTabelle' and sc.[name] = 'PrsKey')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ALTER COLUMN [PrsKey] char(4)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_RundungsTabelle' and sc.[name] = 'Maske')
BEGIN
  ALTER TABLE [dbo].[ut_RundungsTabelle] ALTER COLUMN [Maske] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'RundungsTabelle')
  DROP VIEW [dbo].[RundungsTabelle]
GO
CREATE VIEW [dbo].[RundungsTabelle] AS
  SELECT * FROM [dbo].[ut_RundungsTabelle]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Add_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Add_RundungsTabelle] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_RundungsTabelle])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RundungsTabelle] (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_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Upd_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Upd_RundungsTabelle]
  @RI integer
 ,@Parm_PrsKey char(4)
 ,@Parm_BisPreis float
 ,@Parm_Maske char(10)
 ,@Parm_ToAdd float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_RundungsTabelle] SET
     [PrsKey] = RTRIM(@Parm_PrsKey)
    ,[BisPreis] = @Parm_BisPreis
    ,[Maske] = RTRIM(@Parm_Maske)
    ,[ToAdd] = @Parm_ToAdd
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Sel_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Sel_RundungsTabelle]
  @RI integer
AS
SELECT
     [PrsKey]
    ,[BisPreis]
    ,[Maske]
    ,[ToAdd]
  FROM [dbo].[ut_RundungsTabelle]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Del_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Del_RundungsTabelle]
  @RI integer
AS
  DELETE FROM [dbo].[ut_RundungsTabelle] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Reorg_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Reorg_RundungsTabelle]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_RundungsTabelle] 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_RundungsTabelle] 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_RundungsTabelle] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_Append_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_Append_RundungsTabelle]
  @Parm_PrsKey char(4)
 ,@Parm_BisPreis float
 ,@Parm_Maske char(10)
 ,@Parm_ToAdd float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_RundungsTabelle])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_RundungsTabelle] (
    RecId
   ,[PrsKey]
   ,[BisPreis]
   ,[Maske]
   ,[ToAdd]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrsKey)
   ,@Parm_BisPreis
   ,RTRIM(@Parm_Maske)
   ,@Parm_ToAdd
       )
  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_RundungsTabelle')
  DROP PROCEDURE [dbo].[up_TrSel_RundungsTabelle]
GO
CREATE PROCEDURE [dbo].[up_TrSel_RundungsTabelle]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrsKey]))
      +LTRIM(RTRIM([Maske]))
    AS Data
  FROM     [dbo].[ut_RundungsTabelle]
  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_RundungsTabelle'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_RundungsTabelle]
(PrsKey,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_First_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_Last_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_Prior_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_Next_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_Next50_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_FindL_RundungsTabelle_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_RundungsTabelle_Key00')
  DROP PROCEDURE [dbo].[up_FindF_RundungsTabelle_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus RundungsTabelle in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_RundungsTabelle_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RundungsTabelle] ORDER BY PrsKey,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus RundungsTabelle in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_RundungsTabelle_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_RundungsTabelle] ORDER BY PrsKey DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY PrsKey,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_RundungsTabelle_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldPrsKey char(4)

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

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

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

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

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

  SELECT CONVERT(integer,0) AS RecId
GO

