------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Bestellpositionen')
BEGIN
CREATE TABLE [dbo].[ut_Bestellpositionen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NRHW] char(15) DEFAULT ' '
  ,[POSNRHW] char(10) DEFAULT ' '
  ,[POSNRGH] char(10) DEFAULT ' '
  ,[ARTNRHWP] char(20) DEFAULT ' '
  ,[ARTNR] char(15) DEFAULT ' '
  ,[MENGE] float DEFAULT 0
  ,[TEXT1] char(40) DEFAULT ' '
  ,[TEXT2] char(40) DEFAULT ' '
  ,[BRUTTO] float DEFAULT 0
  ,[RAB1] float DEFAULT 0
  ,[RAB2] float DEFAULT 0
  ,[NETTO] float DEFAULT 0
  ,[LVNR] char(18) DEFAULT ' '
  ,[PE] integer DEFAULT 0
  ,[ME] integer DEFAULT 0
  ,[Alternativ] tinyint DEFAULT 0
  ,[PosTyp] char(1) DEFAULT ' '
  ,[Vorbehalt] tinyint DEFAULT 0
  ,[PreisKz] integer DEFAULT 0
  ,[KzLager] tinyint DEFAULT 0
  ,[ZuschlTyp] char(2) DEFAULT ' '
  CONSTRAINT [Pk_Bestellpositionen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestellpositionen_KeyDel')
  DROP TRIGGER [dbo].[tr_Bestellpositionen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestellpositionen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Bestellpositionen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Bestellpositionen_Keys')
  DROP TABLE [dbo].[ut_Bestellpositionen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] 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_Bestellpositionen' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] 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_Bestellpositionen' and sc.[name] = 'POSNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [POSNRGH] 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_Bestellpositionen' and sc.[name] = 'ARTNRHWP')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [ARTNRHWP] 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_Bestellpositionen' and sc.[name] = 'ARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [ARTNR] 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_Bestellpositionen' and sc.[name] = 'MENGE')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] 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_Bestellpositionen' and sc.[name] = 'TEXT1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [TEXT1] char(40) 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_Bestellpositionen' and sc.[name] = 'TEXT2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [TEXT2] char(40) 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_Bestellpositionen' and sc.[name] = 'BRUTTO')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [BRUTTO] 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_Bestellpositionen' and sc.[name] = 'RAB1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [RAB1] 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_Bestellpositionen' and sc.[name] = 'RAB2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [RAB2] 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_Bestellpositionen' and sc.[name] = 'NETTO')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [NETTO] 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_Bestellpositionen' and sc.[name] = 'LVNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [LVNR] 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_Bestellpositionen' and sc.[name] = 'PE')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [PE] 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_Bestellpositionen' and sc.[name] = 'ME')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [ME] 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_Bestellpositionen' and sc.[name] = 'Alternativ')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [Alternativ] 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_Bestellpositionen' and sc.[name] = 'PosTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [PosTyp] char(1) 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_Bestellpositionen' and sc.[name] = 'Vorbehalt')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [Vorbehalt] 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_Bestellpositionen' and sc.[name] = 'PreisKz')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [PreisKz] 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_Bestellpositionen' and sc.[name] = 'KzLager')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [KzLager] 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_Bestellpositionen' and sc.[name] = 'ZuschlTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ADD [ZuschlTyp] char(2) NOT NULL DEFAULT ' '
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] 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_Bestellpositionen' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [POSNRHW] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'POSNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [POSNRGH] char(10)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'ARTNRHWP')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [ARTNRHWP] char(20)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'ARTNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [ARTNR] char(15)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'TEXT1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [TEXT1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'TEXT2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [TEXT2] char(40)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'LVNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [LVNR] char(18)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'PosTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [PosTyp] char(1)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellpositionen' and sc.[name] = 'ZuschlTyp')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellpositionen] ALTER COLUMN [ZuschlTyp] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Bestellpositionen')
  DROP VIEW [dbo].[Bestellpositionen]
GO
CREATE VIEW [dbo].[Bestellpositionen] AS
  SELECT * FROM [dbo].[ut_Bestellpositionen]
GO
CREATE TABLE [dbo].[ut_Bestellpositionen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[KEY0_Value] CHAR(36) NOT NULL
  ,[KEY1_Value] CHAR(40) NOT NULL
  ,[KEY2_Value] CHAR(45) NOT NULL
  ,[KEY3_Value] CHAR(43) NOT NULL
  CONSTRAINT [Pk_ut_Bestellpositionen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Bestellpositionen_KeyDel] ON [dbo].[ut_Bestellpositionen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Bestellpositionen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Bestellpositionen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[KEY0_Value],[KEY1_Value],[KEY2_Value],[KEY3_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+convert(char(1),[PosTyp])+STR(RecId,10))
     ,UPPER(convert(char(15),[NRHW])+convert(char(15),[ARTNR])+STR(RecId,10))
     ,UPPER(convert(char(15),[NRHW])+convert(char(20),[ARTNRHWP])+STR(RecId,10))
     ,UPPER(convert(char(15),[NRHW])+convert(char(18),[LVNR])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Bestellpositionen_Keys] WITH (SERIALIZABLE) SET
    [KEY0_Value] = UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+convert(char(1),[PosTyp])+STR([RecId],10))
   ,[KEY1_Value] = UPPER(convert(char(15),[NRHW])+convert(char(15),[ARTNR])+STR([RecId],10))
   ,[KEY2_Value] = UPPER(convert(char(15),[NRHW])+convert(char(20),[ARTNRHWP])+STR([RecId],10))
   ,[KEY3_Value] = UPPER(convert(char(15),[NRHW])+convert(char(18),[LVNR])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Add_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Add_Bestellpositionen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Bestellpositionen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestellpositionen] (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_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Upd_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Bestellpositionen]
  @RI integer
 ,@Parm_NRHW char(15)
 ,@Parm_POSNRHW char(10)
 ,@Parm_POSNRGH char(10)
 ,@Parm_ARTNRHWP char(20)
 ,@Parm_ARTNR char(15)
 ,@Parm_MENGE float
 ,@Parm_TEXT1 char(40)
 ,@Parm_TEXT2 char(40)
 ,@Parm_BRUTTO float
 ,@Parm_RAB1 float
 ,@Parm_RAB2 float
 ,@Parm_NETTO float
 ,@Parm_LVNR char(18)
 ,@Parm_PE integer
 ,@Parm_ME integer
 ,@Parm_Alternativ tinyint
 ,@Parm_PosTyp char(1)
 ,@Parm_Vorbehalt tinyint
 ,@Parm_PreisKz integer
 ,@Parm_KzLager tinyint
 ,@Parm_ZuschlTyp char(2)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Bestellpositionen] SET
     [NRHW] = RTRIM(@Parm_NRHW)
    ,[POSNRHW] = RTRIM(@Parm_POSNRHW)
    ,[POSNRGH] = RTRIM(@Parm_POSNRGH)
    ,[ARTNRHWP] = RTRIM(@Parm_ARTNRHWP)
    ,[ARTNR] = RTRIM(@Parm_ARTNR)
    ,[MENGE] = @Parm_MENGE
    ,[TEXT1] = RTRIM(@Parm_TEXT1)
    ,[TEXT2] = RTRIM(@Parm_TEXT2)
    ,[BRUTTO] = @Parm_BRUTTO
    ,[RAB1] = @Parm_RAB1
    ,[RAB2] = @Parm_RAB2
    ,[NETTO] = @Parm_NETTO
    ,[LVNR] = RTRIM(@Parm_LVNR)
    ,[PE] = @Parm_PE
    ,[ME] = @Parm_ME
    ,[Alternativ] = @Parm_Alternativ
    ,[PosTyp] = RTRIM(@Parm_PosTyp)
    ,[Vorbehalt] = @Parm_Vorbehalt
    ,[PreisKz] = @Parm_PreisKz
    ,[KzLager] = @Parm_KzLager
    ,[ZuschlTyp] = RTRIM(@Parm_ZuschlTyp)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Sel_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Bestellpositionen]
  @RI integer
AS
SELECT
     [NRHW]
    ,[POSNRHW]
    ,[POSNRGH]
    ,[ARTNRHWP]
    ,[ARTNR]
    ,[MENGE]
    ,[TEXT1]
    ,[TEXT2]
    ,[BRUTTO]
    ,[RAB1]
    ,[RAB2]
    ,[NETTO]
    ,[LVNR]
    ,[PE]
    ,[ME]
    ,[Alternativ]
    ,[PosTyp]
    ,[Vorbehalt]
    ,[PreisKz]
    ,[KzLager]
    ,[ZuschlTyp]
  FROM [dbo].[ut_Bestellpositionen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Del_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Del_Bestellpositionen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Bestellpositionen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Reorg_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Bestellpositionen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Bestellpositionen] 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_Bestellpositionen] 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_Bestellpositionen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_Append_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_Append_Bestellpositionen]
  @Parm_NRHW char(15)
 ,@Parm_POSNRHW char(10)
 ,@Parm_POSNRGH char(10)
 ,@Parm_ARTNRHWP char(20)
 ,@Parm_ARTNR char(15)
 ,@Parm_MENGE float
 ,@Parm_TEXT1 char(40)
 ,@Parm_TEXT2 char(40)
 ,@Parm_BRUTTO float
 ,@Parm_RAB1 float
 ,@Parm_RAB2 float
 ,@Parm_NETTO float
 ,@Parm_LVNR char(18)
 ,@Parm_PE integer
 ,@Parm_ME integer
 ,@Parm_Alternativ tinyint
 ,@Parm_PosTyp char(1)
 ,@Parm_Vorbehalt tinyint
 ,@Parm_PreisKz integer
 ,@Parm_KzLager tinyint
 ,@Parm_ZuschlTyp char(2)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Bestellpositionen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestellpositionen] (
    RecId
   ,[NRHW]
   ,[POSNRHW]
   ,[POSNRGH]
   ,[ARTNRHWP]
   ,[ARTNR]
   ,[MENGE]
   ,[TEXT1]
   ,[TEXT2]
   ,[BRUTTO]
   ,[RAB1]
   ,[RAB2]
   ,[NETTO]
   ,[LVNR]
   ,[PE]
   ,[ME]
   ,[Alternativ]
   ,[PosTyp]
   ,[Vorbehalt]
   ,[PreisKz]
   ,[KzLager]
   ,[ZuschlTyp]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NRHW)
   ,RTRIM(@Parm_POSNRHW)
   ,RTRIM(@Parm_POSNRGH)
   ,RTRIM(@Parm_ARTNRHWP)
   ,RTRIM(@Parm_ARTNR)
   ,@Parm_MENGE
   ,RTRIM(@Parm_TEXT1)
   ,RTRIM(@Parm_TEXT2)
   ,@Parm_BRUTTO
   ,@Parm_RAB1
   ,@Parm_RAB2
   ,@Parm_NETTO
   ,RTRIM(@Parm_LVNR)
   ,@Parm_PE
   ,@Parm_ME
   ,@Parm_Alternativ
   ,RTRIM(@Parm_PosTyp)
   ,@Parm_Vorbehalt
   ,@Parm_PreisKz
   ,@Parm_KzLager
   ,RTRIM(@Parm_ZuschlTyp)
       )
  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_Bestellpositionen')
  DROP PROCEDURE [dbo].[up_TrSel_Bestellpositionen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Bestellpositionen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NRHW]))
      +LTRIM(RTRIM([POSNRHW]))
      +LTRIM(RTRIM([POSNRGH]))
      +LTRIM(RTRIM([ARTNRHWP]))
      +LTRIM(RTRIM([ARTNR]))
      +LTRIM(RTRIM([TEXT1]))
      +LTRIM(RTRIM([TEXT2]))
      +LTRIM(RTRIM([LVNR]))
      +LTRIM(RTRIM([PosTyp]))
      +LTRIM(RTRIM([ZuschlTyp]))
    AS Data
  FROM     [dbo].[ut_Bestellpositionen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_KEY0] ON [dbo].[ut_Bestellpositionen_Keys]
( [KEY0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_First_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_Bestellpositionen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellpositionen_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_Bestellpositionen_KEY0]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_KEY1] ON [dbo].[ut_Bestellpositionen_Keys]
( [KEY1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_First_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_FindL_Bestellpositionen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellpositionen_KEY1')
  DROP PROCEDURE [dbo].[up_FindF_Bestellpositionen_KEY1]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_KEY2] ON [dbo].[ut_Bestellpositionen_Keys]
( [KEY2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_First_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_Prior_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_FindL_Bestellpositionen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellpositionen_KEY2')
  DROP PROCEDURE [dbo].[up_FindF_Bestellpositionen_KEY2]
GO

CREATE PROCEDURE [dbo].[up_First_Bestellpositionen_KEY2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys] ORDER BY [KEY2_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys] ORDER BY [KEY2_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY2] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys]
  WHERE [KEY2_Value] > (SELECT [KEY2_Value] FROM [dbo].[ut_Bestellpositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY2_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY2] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys]
  WHERE [KEY2_Value] > (SELECT [KEY2_Value] FROM [dbo].[ut_Bestellpositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY2_Value]
GO

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

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

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

CREATE INDEX [Ik_KEY3] ON [dbo].[ut_Bestellpositionen_Keys]
( [KEY3_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_First_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_Prior_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_FindL_Bestellpositionen_KEY3]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellpositionen_KEY3')
  DROP PROCEDURE [dbo].[up_FindF_Bestellpositionen_KEY3]
GO

CREATE PROCEDURE [dbo].[up_First_Bestellpositionen_KEY3]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys] ORDER BY [KEY3_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Bestellpositionen_KEY3]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys] ORDER BY [KEY3_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Bestellpositionen_KEY3] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys]
  WHERE [KEY3_Value] > (SELECT [KEY3_Value] FROM [dbo].[ut_Bestellpositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY3_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Bestellpositionen_KEY3] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Bestellpositionen_Keys]
  WHERE [KEY3_Value] > (SELECT [KEY3_Value] FROM [dbo].[ut_Bestellpositionen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY3_Value]
GO

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

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

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

