------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsTurnus')
BEGIN
CREATE TABLE [dbo].[ut_WartungsTurnus]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[AnlagenId] integer DEFAULT 0
  ,[Monat] char(2) DEFAULT ' '
  ,[BeginnJahr] integer DEFAULT 0
  ,[Turnus] integer DEFAULT 0
  ,[Gross] tinyint DEFAULT 0
  ,[Klein] tinyint DEFAULT 0
  ,[Pauschal] tinyint DEFAULT 0
  ,[Pauschale] float DEFAULT 0
  CONSTRAINT [Pk_WartungsTurnus]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTurnus_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsTurnus_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTurnus_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsTurnus_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsTurnus_Keys')
  DROP TABLE [dbo].[ut_WartungsTurnus_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTurnus' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [AnlagenId] 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_WartungsTurnus' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] 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_WartungsTurnus' and sc.[name] = 'BeginnJahr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [BeginnJahr] 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_WartungsTurnus' and sc.[name] = 'Turnus')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [Turnus] 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_WartungsTurnus' and sc.[name] = 'Gross')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [Gross] 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_WartungsTurnus' and sc.[name] = 'Klein')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [Klein] 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_WartungsTurnus' and sc.[name] = 'Pauschal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [Pauschal] 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_WartungsTurnus' and sc.[name] = 'Pauschale')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ADD [Pauschale] float 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_WartungsTurnus' and sc.[name] = 'Monat')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTurnus] ALTER COLUMN [Monat] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsTurnus')
  DROP VIEW [dbo].[WartungsTurnus]
GO
CREATE VIEW [dbo].[WartungsTurnus] AS
  SELECT * FROM [dbo].[ut_WartungsTurnus]
GO
CREATE TABLE [dbo].[ut_WartungsTurnus_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(20) NOT NULL
  CONSTRAINT [Pk_ut_WartungsTurnus_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsTurnus_KeyDel] ON [dbo].[ut_WartungsTurnus]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsTurnus_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_WartungsTurnus_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([AnlagenId],8,0)+convert(char(2),[Monat])+STR(RecId,10))

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Add_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsTurnus] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WartungsTurnus])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTurnus] (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_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Upd_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsTurnus]
  @RI integer
 ,@Parm_AnlagenId integer
 ,@Parm_Monat char(2)
 ,@Parm_BeginnJahr integer
 ,@Parm_Turnus integer
 ,@Parm_Gross tinyint
 ,@Parm_Klein tinyint
 ,@Parm_Pauschal tinyint
 ,@Parm_Pauschale float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsTurnus] SET
     [AnlagenId] = @Parm_AnlagenId
    ,[Monat] = RTRIM(@Parm_Monat)
    ,[BeginnJahr] = @Parm_BeginnJahr
    ,[Turnus] = @Parm_Turnus
    ,[Gross] = @Parm_Gross
    ,[Klein] = @Parm_Klein
    ,[Pauschal] = @Parm_Pauschal
    ,[Pauschale] = @Parm_Pauschale
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Sel_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsTurnus]
  @RI integer
AS
SELECT
     [AnlagenId]
    ,[Monat]
    ,[BeginnJahr]
    ,[Turnus]
    ,[Gross]
    ,[Klein]
    ,[Pauschal]
    ,[Pauschale]
  FROM [dbo].[ut_WartungsTurnus]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Del_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsTurnus]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsTurnus] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsTurnus]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsTurnus] 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_WartungsTurnus] 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_WartungsTurnus] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_Append_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsTurnus]
  @Parm_AnlagenId integer
 ,@Parm_Monat char(2)
 ,@Parm_BeginnJahr integer
 ,@Parm_Turnus integer
 ,@Parm_Gross tinyint
 ,@Parm_Klein tinyint
 ,@Parm_Pauschal tinyint
 ,@Parm_Pauschale float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WartungsTurnus])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTurnus] (
    RecId
   ,[AnlagenId]
   ,[Monat]
   ,[BeginnJahr]
   ,[Turnus]
   ,[Gross]
   ,[Klein]
   ,[Pauschal]
   ,[Pauschale]
     ) VALUES (
    @NeueId
   ,@Parm_AnlagenId
   ,RTRIM(@Parm_Monat)
   ,@Parm_BeginnJahr
   ,@Parm_Turnus
   ,@Parm_Gross
   ,@Parm_Klein
   ,@Parm_Pauschal
   ,@Parm_Pauschale
       )
  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_WartungsTurnus')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsTurnus]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsTurnus]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Monat]))
    AS Data
  FROM     [dbo].[ut_WartungsTurnus]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsTurnus_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsTurnus_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsTurnus_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsTurnus_Key00]
GO

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

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

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

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

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

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

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

