------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonJobPosTodo')
BEGIN
CREATE TABLE [dbo].[ut_MareonJobPosTodo]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NrBsh] char(20) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[LfdNrTodo] integer DEFAULT 0
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[TodoKey] char(20) DEFAULT ' '
  ,[Todo] char(50) DEFAULT ' '
  ,[Quantity] float DEFAULT 0
  ,[Description] integer DEFAULT 0
  CONSTRAINT [Pk_MareonJobPosTodo]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonJobPosTodo_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonJobPosTodo_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonJobPosTodo_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonJobPosTodo_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonJobPosTodo_Keys')
  DROP TABLE [dbo].[ut_MareonJobPosTodo_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPosTodo' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [NrBsh] char(20) 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_MareonJobPosTodo' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [LfdNr] 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_MareonJobPosTodo' and sc.[name] = 'LfdNrTodo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [LfdNrTodo] 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_MareonJobPosTodo' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [HostDate] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPosTodo' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [TodoKey] char(20) 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_MareonJobPosTodo' and sc.[name] = 'Todo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [Todo] char(50) 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_MareonJobPosTodo' and sc.[name] = 'Quantity')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [Quantity] 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_MareonJobPosTodo' and sc.[name] = 'Description')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ADD [Description] integer NOT NULL DEFAULT 0
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPosTodo' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ALTER COLUMN [NrBsh] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPosTodo' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ALTER COLUMN [TodoKey] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPosTodo' and sc.[name] = 'Todo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPosTodo] ALTER COLUMN [Todo] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonJobPosTodo')
  DROP VIEW [dbo].[MareonJobPosTodo]
GO
CREATE VIEW [dbo].[MareonJobPosTodo] AS
  SELECT * FROM [dbo].[ut_MareonJobPosTodo]
GO
CREATE TABLE [dbo].[ut_MareonJobPosTodo_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(38) NOT NULL
  CONSTRAINT [Pk_ut_MareonJobPosTodo_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonJobPosTodo_KeyDel] ON [dbo].[ut_MareonJobPosTodo]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonJobPosTodo_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_MareonJobPosTodo_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[NrBsh])+STR([LfdNr],4,0)+STR([LfdNrTodo],4,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_MareonJobPosTodo_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[NrBsh])+STR([LfdNr],4,0)+STR([LfdNrTodo],4,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Add_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonJobPosTodo] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonJobPosTodo])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonJobPosTodo] (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_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Upd_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonJobPosTodo]
  @RI integer
 ,@Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_LfdNrTodo integer
 ,@Parm_HostDate datetime
 ,@Parm_TodoKey char(20)
 ,@Parm_Todo char(50)
 ,@Parm_Quantity float
 ,@Parm_Description integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonJobPosTodo] SET
     [NrBsh] = RTRIM(@Parm_NrBsh)
    ,[LfdNr] = @Parm_LfdNr
    ,[LfdNrTodo] = @Parm_LfdNrTodo
    ,[HostDate] = @Parm_HostDate
    ,[TodoKey] = RTRIM(@Parm_TodoKey)
    ,[Todo] = RTRIM(@Parm_Todo)
    ,[Quantity] = @Parm_Quantity
    ,[Description] = @Parm_Description
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Sel_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonJobPosTodo]
  @RI integer
AS
SELECT
     [NrBsh]
    ,[LfdNr]
    ,[LfdNrTodo]
    ,[HostDate]
    ,[TodoKey]
    ,[Todo]
    ,[Quantity]
    ,[Description]
  FROM [dbo].[ut_MareonJobPosTodo]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Del_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonJobPosTodo]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonJobPosTodo] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Reorg_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonJobPosTodo]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonJobPosTodo] 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_MareonJobPosTodo] 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_MareonJobPosTodo] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_Append_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonJobPosTodo]
  @Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_LfdNrTodo integer
 ,@Parm_HostDate datetime
 ,@Parm_TodoKey char(20)
 ,@Parm_Todo char(50)
 ,@Parm_Quantity float
 ,@Parm_Description integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonJobPosTodo])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonJobPosTodo] (
    RecId
   ,[NrBsh]
   ,[LfdNr]
   ,[LfdNrTodo]
   ,[HostDate]
   ,[TodoKey]
   ,[Todo]
   ,[Quantity]
   ,[Description]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NrBsh)
   ,@Parm_LfdNr
   ,@Parm_LfdNrTodo
   ,@Parm_HostDate
   ,RTRIM(@Parm_TodoKey)
   ,RTRIM(@Parm_Todo)
   ,@Parm_Quantity
   ,@Parm_Description
       )
  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_MareonJobPosTodo')
  DROP PROCEDURE [dbo].[up_TrSel_MareonJobPosTodo]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonJobPosTodo]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NrBsh]))
      +LTRIM(RTRIM([TodoKey]))
      +LTRIM(RTRIM([Todo]))
    AS Data
  FROM     [dbo].[ut_MareonJobPosTodo]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonJobPosTodo_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonJobPosTodo_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonJobPosTodo_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonJobPosTodo_Key00]
GO

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

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

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

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

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

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

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

