------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonJobPos')
BEGIN
CREATE TABLE [dbo].[ut_MareonJobPos]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NrBsh] char(20) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[InventoryKey] char(30) DEFAULT ' '
  ,[Inventory] char(50) DEFAULT ' '
  ,[DamageKey] char(30) DEFAULT ' '
  ,[Damage] char(50) DEFAULT ' '
  ,[DamageText] integer DEFAULT 0
  ,[ObjectKey] char(30) DEFAULT ' '
  ,[Object] char(50) DEFAULT ' '
  ,[TradeKey] char(20) DEFAULT ' '
  ,[Trade] char(50) DEFAULT ' '
  ,[TodoKey] char(20) DEFAULT ' '
  ,[Todo] char(50) DEFAULT ' '
  ,[TodoText] integer DEFAULT 0
  ,[Quantity] float DEFAULT 0
  ,[Unit] char(6) DEFAULT ' '
  ,[Price] float DEFAULT 0
  ,[PosNrMareon] integer DEFAULT 0
  CONSTRAINT [Pk_MareonJobPos]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonJobPos_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonJobPos_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonJobPos_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonJobPos_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonJobPos_Keys')
  DROP TABLE [dbo].[ut_MareonJobPos_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'InventoryKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [InventoryKey] 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_MareonJobPos' and sc.[name] = 'Inventory')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Inventory] 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_MareonJobPos' and sc.[name] = 'DamageKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [DamageKey] 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_MareonJobPos' and sc.[name] = 'Damage')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Damage] 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_MareonJobPos' and sc.[name] = 'DamageText')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [DamageText] 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_MareonJobPos' and sc.[name] = 'ObjectKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [ObjectKey] 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_MareonJobPos' and sc.[name] = 'Object')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Object] 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_MareonJobPos' and sc.[name] = 'TradeKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [TradeKey] 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_MareonJobPos' and sc.[name] = 'Trade')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Trade] 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_MareonJobPos' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'Todo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'TodoText')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [TodoText] 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_MareonJobPos' and sc.[name] = 'Quantity')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'Unit')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Unit] char(6) 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_MareonJobPos' and sc.[name] = 'Price')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [Price] 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_MareonJobPos' and sc.[name] = 'PosNrMareon')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ADD [PosNrMareon] 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_MareonJobPos' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [NrBsh] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'InventoryKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [InventoryKey] char(30)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'Inventory')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Inventory] char(50)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'DamageKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [DamageKey] char(30)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'Damage')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Damage] char(50)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'ObjectKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [ObjectKey] char(30)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'Object')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Object] char(50)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'TradeKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [TradeKey] char(20)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'Trade')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Trade] char(50)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] 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_MareonJobPos' and sc.[name] = 'Todo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Todo] char(50)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonJobPos' and sc.[name] = 'Unit')
BEGIN
  ALTER TABLE [dbo].[ut_MareonJobPos] ALTER COLUMN [Unit] char(6)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonJobPos')
  DROP VIEW [dbo].[MareonJobPos]
GO
CREATE VIEW [dbo].[MareonJobPos] AS
  SELECT * FROM [dbo].[ut_MareonJobPos]
GO
CREATE TABLE [dbo].[ut_MareonJobPos_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(34) NOT NULL
  CONSTRAINT [Pk_ut_MareonJobPos_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonJobPos_KeyDel] ON [dbo].[ut_MareonJobPos]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonJobPos_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Add_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonJobPos] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonJobPos])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonJobPos] (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_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Upd_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonJobPos]
  @RI integer
 ,@Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_HostDate datetime
 ,@Parm_InventoryKey char(30)
 ,@Parm_Inventory char(50)
 ,@Parm_DamageKey char(30)
 ,@Parm_Damage char(50)
 ,@Parm_DamageText integer
 ,@Parm_ObjectKey char(30)
 ,@Parm_Object char(50)
 ,@Parm_TradeKey char(20)
 ,@Parm_Trade char(50)
 ,@Parm_TodoKey char(20)
 ,@Parm_Todo char(50)
 ,@Parm_TodoText integer
 ,@Parm_Quantity float
 ,@Parm_Unit char(6)
 ,@Parm_Price float
 ,@Parm_PosNrMareon integer
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonJobPos] SET
     [NrBsh] = RTRIM(@Parm_NrBsh)
    ,[LfdNr] = @Parm_LfdNr
    ,[HostDate] = @Parm_HostDate
    ,[InventoryKey] = RTRIM(@Parm_InventoryKey)
    ,[Inventory] = RTRIM(@Parm_Inventory)
    ,[DamageKey] = RTRIM(@Parm_DamageKey)
    ,[Damage] = RTRIM(@Parm_Damage)
    ,[DamageText] = @Parm_DamageText
    ,[ObjectKey] = RTRIM(@Parm_ObjectKey)
    ,[Object] = RTRIM(@Parm_Object)
    ,[TradeKey] = RTRIM(@Parm_TradeKey)
    ,[Trade] = RTRIM(@Parm_Trade)
    ,[TodoKey] = RTRIM(@Parm_TodoKey)
    ,[Todo] = RTRIM(@Parm_Todo)
    ,[TodoText] = @Parm_TodoText
    ,[Quantity] = @Parm_Quantity
    ,[Unit] = RTRIM(@Parm_Unit)
    ,[Price] = @Parm_Price
    ,[PosNrMareon] = @Parm_PosNrMareon
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Sel_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonJobPos]
  @RI integer
AS
SELECT
     [NrBsh]
    ,[LfdNr]
    ,[HostDate]
    ,[InventoryKey]
    ,[Inventory]
    ,[DamageKey]
    ,[Damage]
    ,[DamageText]
    ,[ObjectKey]
    ,[Object]
    ,[TradeKey]
    ,[Trade]
    ,[TodoKey]
    ,[Todo]
    ,[TodoText]
    ,[Quantity]
    ,[Unit]
    ,[Price]
    ,[PosNrMareon]
  FROM [dbo].[ut_MareonJobPos]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Del_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonJobPos]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonJobPos] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Reorg_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonJobPos]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonJobPos] 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_MareonJobPos] 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_MareonJobPos] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonJobPos')
  DROP PROCEDURE [dbo].[up_Append_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonJobPos]
  @Parm_NrBsh char(20)
 ,@Parm_LfdNr integer
 ,@Parm_HostDate datetime
 ,@Parm_InventoryKey char(30)
 ,@Parm_Inventory char(50)
 ,@Parm_DamageKey char(30)
 ,@Parm_Damage char(50)
 ,@Parm_DamageText integer
 ,@Parm_ObjectKey char(30)
 ,@Parm_Object char(50)
 ,@Parm_TradeKey char(20)
 ,@Parm_Trade char(50)
 ,@Parm_TodoKey char(20)
 ,@Parm_Todo char(50)
 ,@Parm_TodoText integer
 ,@Parm_Quantity float
 ,@Parm_Unit char(6)
 ,@Parm_Price float
 ,@Parm_PosNrMareon integer
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonJobPos])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonJobPos] (
    RecId
   ,[NrBsh]
   ,[LfdNr]
   ,[HostDate]
   ,[InventoryKey]
   ,[Inventory]
   ,[DamageKey]
   ,[Damage]
   ,[DamageText]
   ,[ObjectKey]
   ,[Object]
   ,[TradeKey]
   ,[Trade]
   ,[TodoKey]
   ,[Todo]
   ,[TodoText]
   ,[Quantity]
   ,[Unit]
   ,[Price]
   ,[PosNrMareon]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NrBsh)
   ,@Parm_LfdNr
   ,@Parm_HostDate
   ,RTRIM(@Parm_InventoryKey)
   ,RTRIM(@Parm_Inventory)
   ,RTRIM(@Parm_DamageKey)
   ,RTRIM(@Parm_Damage)
   ,@Parm_DamageText
   ,RTRIM(@Parm_ObjectKey)
   ,RTRIM(@Parm_Object)
   ,RTRIM(@Parm_TradeKey)
   ,RTRIM(@Parm_Trade)
   ,RTRIM(@Parm_TodoKey)
   ,RTRIM(@Parm_Todo)
   ,@Parm_TodoText
   ,@Parm_Quantity
   ,RTRIM(@Parm_Unit)
   ,@Parm_Price
   ,@Parm_PosNrMareon
       )
  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_MareonJobPos')
  DROP PROCEDURE [dbo].[up_TrSel_MareonJobPos]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonJobPos]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NrBsh]))
      +LTRIM(RTRIM([InventoryKey]))
      +LTRIM(RTRIM([Inventory]))
      +LTRIM(RTRIM([DamageKey]))
      +LTRIM(RTRIM([Damage]))
      +LTRIM(RTRIM([ObjectKey]))
      +LTRIM(RTRIM([Object]))
      +LTRIM(RTRIM([TradeKey]))
      +LTRIM(RTRIM([Trade]))
      +LTRIM(RTRIM([TodoKey]))
      +LTRIM(RTRIM([Todo]))
      +LTRIM(RTRIM([Unit]))
    AS Data
  FROM     [dbo].[ut_MareonJobPos]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonJobPos_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonJobPos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonJobPos_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonJobPos_Key00]
GO

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

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

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

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

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

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

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

