------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BestellAbrufMengen')
BEGIN
CREATE TABLE [dbo].[ut_BestellAbrufMengen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NRHW] char(15) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[POSNRHW] char(10) DEFAULT ' '
  ,[MENGE] float DEFAULT 0
  CONSTRAINT [Pk_BestellAbrufMengen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BestellAbrufMengen_KeyDel')
  DROP TRIGGER [dbo].[tr_BestellAbrufMengen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BestellAbrufMengen_KeyUpd')
  DROP TRIGGER [dbo].[tr_BestellAbrufMengen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BestellAbrufMengen_Keys')
  DROP TABLE [dbo].[ut_BestellAbrufMengen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbrufMengen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] ADD [NRHW] char(15) 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_BestellAbrufMengen' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] 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_BestellAbrufMengen' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] ADD [POSNRHW] char(10) 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_BestellAbrufMengen' and sc.[name] = 'MENGE')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] ADD [MENGE] float NOT NULL DEFAULT 0
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbrufMengen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] ALTER COLUMN [NRHW] char(15)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbrufMengen' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbrufMengen] ALTER COLUMN [POSNRHW] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BestellAbrufMengen')
  DROP VIEW [dbo].[BestellAbrufMengen]
GO
CREATE VIEW [dbo].[BestellAbrufMengen] AS
  SELECT * FROM [dbo].[ut_BestellAbrufMengen]
GO
CREATE TABLE [dbo].[ut_BestellAbrufMengen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[KEY0_Value] CHAR(43) NOT NULL
  ,[KEY1_Value] CHAR(43) NOT NULL
  CONSTRAINT [Pk_ut_BestellAbrufMengen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BestellAbrufMengen_KeyDel] ON [dbo].[ut_BestellAbrufMengen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BestellAbrufMengen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BestellAbrufMengen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[KEY0_Value],[KEY1_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[NRHW])+STR([LfdNr],8,0)+convert(char(10),[POSNRHW])+STR(RecId,10))
     ,UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+STR([LfdNr],8,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BestellAbrufMengen_Keys] WITH (SERIALIZABLE) SET
    [KEY0_Value] = UPPER(convert(char(15),[NRHW])+STR([LfdNr],8,0)+convert(char(10),[POSNRHW])+STR([RecId],10))
   ,[KEY1_Value] = UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+STR([LfdNr],8,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Add_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Add_BestellAbrufMengen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BestellAbrufMengen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BestellAbrufMengen] (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_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Upd_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Upd_BestellAbrufMengen]
  @RI integer
 ,@Parm_NRHW char(15)
 ,@Parm_LfdNr integer
 ,@Parm_POSNRHW char(10)
 ,@Parm_MENGE float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BestellAbrufMengen] SET
     [NRHW] = RTRIM(@Parm_NRHW)
    ,[LfdNr] = @Parm_LfdNr
    ,[POSNRHW] = RTRIM(@Parm_POSNRHW)
    ,[MENGE] = @Parm_MENGE
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Sel_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Sel_BestellAbrufMengen]
  @RI integer
AS
SELECT
     [NRHW]
    ,[LfdNr]
    ,[POSNRHW]
    ,[MENGE]
  FROM [dbo].[ut_BestellAbrufMengen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Del_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Del_BestellAbrufMengen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BestellAbrufMengen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Reorg_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BestellAbrufMengen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BestellAbrufMengen] 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_BestellAbrufMengen] 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_BestellAbrufMengen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_Append_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_Append_BestellAbrufMengen]
  @Parm_NRHW char(15)
 ,@Parm_LfdNr integer
 ,@Parm_POSNRHW char(10)
 ,@Parm_MENGE float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BestellAbrufMengen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BestellAbrufMengen] (
    RecId
   ,[NRHW]
   ,[LfdNr]
   ,[POSNRHW]
   ,[MENGE]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NRHW)
   ,@Parm_LfdNr
   ,RTRIM(@Parm_POSNRHW)
   ,@Parm_MENGE
       )
  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_BestellAbrufMengen')
  DROP PROCEDURE [dbo].[up_TrSel_BestellAbrufMengen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BestellAbrufMengen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NRHW]))
      +LTRIM(RTRIM([POSNRHW]))
    AS Data
  FROM     [dbo].[ut_BestellAbrufMengen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_KEY0] ON [dbo].[ut_BestellAbrufMengen_Keys]
( [KEY0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_First_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_Last_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_Next_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior50_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_Next50_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_BestellAbrufMengen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BestellAbrufMengen_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_BestellAbrufMengen_KEY0]
GO

CREATE PROCEDURE [dbo].[up_First_BestellAbrufMengen_KEY0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys] ORDER BY [KEY0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BestellAbrufMengen_KEY0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys] ORDER BY [KEY0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BestellAbrufMengen_KEY0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY0_Value] > (SELECT [KEY0_Value] FROM [dbo].[ut_BestellAbrufMengen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BestellAbrufMengen_KEY0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY0_Value] > (SELECT [KEY0_Value] FROM [dbo].[ut_BestellAbrufMengen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_FindF_BestellAbrufMengen_KEY0] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[KEY0_Value] AS Key_Value FROM [dbo].[ut_BestellAbrufMengen_Keys]
      WHERE [KEY0_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [KEY0_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_BestellAbrufMengen_KEY0] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY0_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [KEY0_Value] DESC
GO

CREATE INDEX [Ik_KEY1] ON [dbo].[ut_BestellAbrufMengen_Keys]
( [KEY1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_First_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_Last_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_Next_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior50_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_Next50_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_FindL_BestellAbrufMengen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BestellAbrufMengen_KEY1')
  DROP PROCEDURE [dbo].[up_FindF_BestellAbrufMengen_KEY1]
GO

CREATE PROCEDURE [dbo].[up_First_BestellAbrufMengen_KEY1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys] ORDER BY [KEY1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BestellAbrufMengen_KEY1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys] ORDER BY [KEY1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BestellAbrufMengen_KEY1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY1_Value] > (SELECT [KEY1_Value] FROM [dbo].[ut_BestellAbrufMengen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BestellAbrufMengen_KEY1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY1_Value] > (SELECT [KEY1_Value] FROM [dbo].[ut_BestellAbrufMengen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_FindF_BestellAbrufMengen_KEY1] @xKey char(128),@xLen integer
AS
  SELECT a.[DataRecId] FROM
    ( SELECT TOP 1 [DataRecId],[KEY1_Value] AS Key_Value FROM [dbo].[ut_BestellAbrufMengen_Keys]
      WHERE [KEY1_Value] >= SUBSTRING(@xKey,1,@xLen)
      ORDER BY [KEY1_Value]
    ) a
  WHERE a.Key_Value like SUBSTRING(@xKey,1,@xLen)+'%'
GO

CREATE PROCEDURE [dbo].[up_FindL_BestellAbrufMengen_KEY1] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BestellAbrufMengen_Keys]
  WHERE [KEY1_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [KEY1_Value] DESC
GO

