------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Mahnplan')
BEGIN
CREATE TABLE [dbo].[ut_Mahnplan]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Mahnplan] char(2) DEFAULT ' '
  ,[Stufe] integer DEFAULT 0
  ,[NeuFaellig] integer DEFAULT 0
  ,[WarteTage] integer DEFAULT 0
  ,[Gebuehr] float DEFAULT 0
  ,[BelegTitel] char(30) DEFAULT ' '
  ,[ZinsenJN] tinyint DEFAULT 0
  CONSTRAINT [Pk_Mahnplan]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mahnplan_KeyDel')
  DROP TRIGGER [dbo].[tr_Mahnplan_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Mahnplan_KeyUpd')
  DROP TRIGGER [dbo].[tr_Mahnplan_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Mahnplan_Keys')
  DROP TABLE [dbo].[ut_Mahnplan_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnplan' and sc.[name] = 'Mahnplan')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [Mahnplan] 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_Mahnplan' and sc.[name] = 'Stufe')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [Stufe] 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_Mahnplan' and sc.[name] = 'NeuFaellig')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [NeuFaellig] 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_Mahnplan' and sc.[name] = 'WarteTage')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [WarteTage] 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_Mahnplan' and sc.[name] = 'Gebuehr')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [Gebuehr] 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_Mahnplan' and sc.[name] = 'BelegTitel')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [BelegTitel] 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_Mahnplan' and sc.[name] = 'ZinsenJN')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ADD [ZinsenJN] tinyint NOT NULL DEFAULT 0
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnplan' and sc.[name] = 'Mahnplan')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ALTER COLUMN [Mahnplan] char(2)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Mahnplan' and sc.[name] = 'BelegTitel')
BEGIN
  ALTER TABLE [dbo].[ut_Mahnplan] ALTER COLUMN [BelegTitel] char(30)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Mahnplan')
  DROP VIEW [dbo].[Mahnplan]
GO
CREATE VIEW [dbo].[Mahnplan] AS
  SELECT * FROM [dbo].[ut_Mahnplan]
GO
CREATE TABLE [dbo].[ut_Mahnplan_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(14) NOT NULL
  CONSTRAINT [Pk_ut_Mahnplan_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Mahnplan_KeyDel] ON [dbo].[ut_Mahnplan]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Mahnplan_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Mahnplan_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(2),[Mahnplan])+STR([Stufe],2,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Mahnplan_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(2),[Mahnplan])+STR([Stufe],2,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Mahnplan')
  DROP PROCEDURE [dbo].[up_Add_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Add_Mahnplan] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Mahnplan])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mahnplan] (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_Mahnplan')
  DROP PROCEDURE [dbo].[up_Upd_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Upd_Mahnplan]
  @RI integer
 ,@Parm_Mahnplan char(2)
 ,@Parm_Stufe integer
 ,@Parm_NeuFaellig integer
 ,@Parm_WarteTage integer
 ,@Parm_Gebuehr float
 ,@Parm_BelegTitel char(30)
 ,@Parm_ZinsenJN tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Mahnplan] SET
     [Mahnplan] = RTRIM(@Parm_Mahnplan)
    ,[Stufe] = @Parm_Stufe
    ,[NeuFaellig] = @Parm_NeuFaellig
    ,[WarteTage] = @Parm_WarteTage
    ,[Gebuehr] = @Parm_Gebuehr
    ,[BelegTitel] = RTRIM(@Parm_BelegTitel)
    ,[ZinsenJN] = @Parm_ZinsenJN
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Mahnplan')
  DROP PROCEDURE [dbo].[up_Sel_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Sel_Mahnplan]
  @RI integer
AS
SELECT
     [Mahnplan]
    ,[Stufe]
    ,[NeuFaellig]
    ,[WarteTage]
    ,[Gebuehr]
    ,[BelegTitel]
    ,[ZinsenJN]
  FROM [dbo].[ut_Mahnplan]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Mahnplan')
  DROP PROCEDURE [dbo].[up_Del_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Del_Mahnplan]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Mahnplan] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Mahnplan')
  DROP PROCEDURE [dbo].[up_Reorg_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Mahnplan]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Mahnplan] 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_Mahnplan] 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_Mahnplan] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Mahnplan')
  DROP PROCEDURE [dbo].[up_Append_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_Append_Mahnplan]
  @Parm_Mahnplan char(2)
 ,@Parm_Stufe integer
 ,@Parm_NeuFaellig integer
 ,@Parm_WarteTage integer
 ,@Parm_Gebuehr float
 ,@Parm_BelegTitel char(30)
 ,@Parm_ZinsenJN tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Mahnplan])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Mahnplan] (
    RecId
   ,[Mahnplan]
   ,[Stufe]
   ,[NeuFaellig]
   ,[WarteTage]
   ,[Gebuehr]
   ,[BelegTitel]
   ,[ZinsenJN]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_Mahnplan)
   ,@Parm_Stufe
   ,@Parm_NeuFaellig
   ,@Parm_WarteTage
   ,@Parm_Gebuehr
   ,RTRIM(@Parm_BelegTitel)
   ,@Parm_ZinsenJN
       )
  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_Mahnplan')
  DROP PROCEDURE [dbo].[up_TrSel_Mahnplan]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Mahnplan]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Mahnplan]))
      +LTRIM(RTRIM([BelegTitel]))
    AS Data
  FROM     [dbo].[ut_Mahnplan]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_Mahnplan_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_First_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_Last_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_Next_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_Next50_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_FindL_Mahnplan_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Mahnplan_Key00')
  DROP PROCEDURE [dbo].[up_FindF_Mahnplan_Key00]
GO

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

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

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

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

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

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

CREATE PROCEDURE [dbo].[up_FindF_Mahnplan_Key00] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[Key00_Value] AS Key_Value FROM [dbo].[ut_Mahnplan_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_Mahnplan_Key00] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Mahnplan_Keys]
  WHERE [Key00_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key00_Value] DESC
GO

