------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsTodo')
BEGIN
CREATE TABLE [dbo].[ut_WartungsTodo]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[AnlagenId] integer DEFAULT 0
  ,[PosNr] integer DEFAULT 0
  ,[Gross] tinyint DEFAULT 0
  ,[Klein] tinyint DEFAULT 0
  ,[Bedarf] tinyint DEFAULT 0
  ,[Menge] float DEFAULT 0
  ,[Einheit] integer DEFAULT 0
  ,[Lkz] char(2) DEFAULT ' '
  ,[ArtNr] char(18) DEFAULT ' '
  ,[Bez1] char(42) DEFAULT ' '
  ,[Bez2] char(42) DEFAULT ' '
  ,[Merkmal] char(42) DEFAULT ' '
  CONSTRAINT [Pk_WartungsTodo]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTodo_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsTodo_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsTodo_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsTodo_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsTodo_Keys')
  DROP TABLE [dbo].[ut_WartungsTodo_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'AnlagenId')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] 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_WartungsTodo' and sc.[name] = 'PosNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [PosNr] 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_WartungsTodo' and sc.[name] = 'Gross')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] 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_WartungsTodo' and sc.[name] = 'Klein')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] 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_WartungsTodo' and sc.[name] = 'Bedarf')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Bedarf] 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_WartungsTodo' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Menge] 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_WartungsTodo' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Einheit] 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_WartungsTodo' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Lkz] 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_WartungsTodo' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [ArtNr] char(18) 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_WartungsTodo' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Bez1] char(42) 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_WartungsTodo' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Bez2] char(42) 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_WartungsTodo' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ADD [Merkmal] char(42) NOT NULL DEFAULT ' '
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'Lkz')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ALTER COLUMN [Lkz] char(2)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'ArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ALTER COLUMN [ArtNr] char(18)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ALTER COLUMN [Bez1] char(42)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ALTER COLUMN [Bez2] char(42)
END
GO
IF 42 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsTodo' and sc.[name] = 'Merkmal')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsTodo] ALTER COLUMN [Merkmal] char(42)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsTodo')
  DROP VIEW [dbo].[WartungsTodo]
GO
CREATE VIEW [dbo].[WartungsTodo] AS
  SELECT * FROM [dbo].[ut_WartungsTodo]
GO
CREATE TABLE [dbo].[ut_WartungsTodo_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(22) NOT NULL
  CONSTRAINT [Pk_ut_WartungsTodo_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsTodo_KeyDel] ON [dbo].[ut_WartungsTodo]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsTodo_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_WartungsTodo_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([AnlagenId],8,0)+STR([PosNr],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WartungsTodo_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(STR([AnlagenId],8,0)+STR([PosNr],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Add_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsTodo] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_WartungsTodo])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTodo] (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_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Upd_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsTodo]
  @RI integer
 ,@Parm_AnlagenId integer
 ,@Parm_PosNr integer
 ,@Parm_Gross tinyint
 ,@Parm_Klein tinyint
 ,@Parm_Bedarf tinyint
 ,@Parm_Menge float
 ,@Parm_Einheit integer
 ,@Parm_Lkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_Bez1 char(42)
 ,@Parm_Bez2 char(42)
 ,@Parm_Merkmal char(42)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsTodo] SET
     [AnlagenId] = @Parm_AnlagenId
    ,[PosNr] = @Parm_PosNr
    ,[Gross] = @Parm_Gross
    ,[Klein] = @Parm_Klein
    ,[Bedarf] = @Parm_Bedarf
    ,[Menge] = @Parm_Menge
    ,[Einheit] = @Parm_Einheit
    ,[Lkz] = RTRIM(@Parm_Lkz)
    ,[ArtNr] = RTRIM(@Parm_ArtNr)
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
    ,[Merkmal] = RTRIM(@Parm_Merkmal)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Sel_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsTodo]
  @RI integer
AS
SELECT
     [AnlagenId]
    ,[PosNr]
    ,[Gross]
    ,[Klein]
    ,[Bedarf]
    ,[Menge]
    ,[Einheit]
    ,[Lkz]
    ,[ArtNr]
    ,[Bez1]
    ,[Bez2]
    ,[Merkmal]
  FROM [dbo].[ut_WartungsTodo]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Del_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsTodo]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsTodo] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsTodo]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsTodo] 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_WartungsTodo] 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_WartungsTodo] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_WartungsTodo')
  DROP PROCEDURE [dbo].[up_Append_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsTodo]
  @Parm_AnlagenId integer
 ,@Parm_PosNr integer
 ,@Parm_Gross tinyint
 ,@Parm_Klein tinyint
 ,@Parm_Bedarf tinyint
 ,@Parm_Menge float
 ,@Parm_Einheit integer
 ,@Parm_Lkz char(2)
 ,@Parm_ArtNr char(18)
 ,@Parm_Bez1 char(42)
 ,@Parm_Bez2 char(42)
 ,@Parm_Merkmal char(42)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_WartungsTodo])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_WartungsTodo] (
    RecId
   ,[AnlagenId]
   ,[PosNr]
   ,[Gross]
   ,[Klein]
   ,[Bedarf]
   ,[Menge]
   ,[Einheit]
   ,[Lkz]
   ,[ArtNr]
   ,[Bez1]
   ,[Bez2]
   ,[Merkmal]
     ) VALUES (
    @NeueId
   ,@Parm_AnlagenId
   ,@Parm_PosNr
   ,@Parm_Gross
   ,@Parm_Klein
   ,@Parm_Bedarf
   ,@Parm_Menge
   ,@Parm_Einheit
   ,RTRIM(@Parm_Lkz)
   ,RTRIM(@Parm_ArtNr)
   ,RTRIM(@Parm_Bez1)
   ,RTRIM(@Parm_Bez2)
   ,RTRIM(@Parm_Merkmal)
       )
  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_WartungsTodo')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsTodo]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsTodo]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Lkz]))
      +LTRIM(RTRIM([ArtNr]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
      +LTRIM(RTRIM([Merkmal]))
    AS Data
  FROM     [dbo].[ut_WartungsTodo]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsTodo_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsTodo_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsTodo_Key00]
GO

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

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

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

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

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

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

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

