------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonInvoicePos')
BEGIN
CREATE TABLE [dbo].[ut_MareonInvoicePos]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NrBsh] char(20) DEFAULT ' '
  ,[PosType] integer DEFAULT 0
  ,[Pos] integer DEFAULT 0
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[HostPos] tinyint DEFAULT 0
  ,[Visible] tinyint DEFAULT 0
  ,[TodoKey] char(20) DEFAULT ' '
  ,[Description] char(50) DEFAULT ' '
  ,[Quantity] float DEFAULT 0
  ,[Price] float DEFAULT 0
  ,[Unit] char(6) DEFAULT ' '
  ,[Discount] float DEFAULT 0
  ,[DscAmount] float DEFAULT 0
  ,[VAT] float DEFAULT 0
  ,[VATAmount] float DEFAULT 0
  ,[wagesAmount] float DEFAULT 0
  ,[FPA] char(20) DEFAULT ' '
  ,[TextPos] integer DEFAULT 0
  ,[TextPdf] integer DEFAULT 0
  ,[Betrag] float DEFAULT 0
  ,[nettoGP] float DEFAULT 0
  ,[nettoBetrag] float DEFAULT 0
  ,[HwpLkz] char(2) DEFAULT ' '
  ,[HwpArtnr] char(18) DEFAULT ' '
  ,[OZ] char(20) DEFAULT ' '
  ,[TextOnly] tinyint DEFAULT 0
  CONSTRAINT [Pk_MareonInvoicePos]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonInvoicePos_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonInvoicePos_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonInvoicePos_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonInvoicePos_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonInvoicePos_Keys')
  DROP TABLE [dbo].[ut_MareonInvoicePos_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'PosType')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [PosType] 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_MareonInvoicePos' and sc.[name] = 'Pos')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [Pos] 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_MareonInvoicePos' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'HostPos')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [HostPos] 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_MareonInvoicePos' and sc.[name] = 'Visible')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [Visible] 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_MareonInvoicePos' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'Description')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [Description] 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_MareonInvoicePos' and sc.[name] = 'Quantity')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'Price')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'Unit')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'Discount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [Discount] 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_MareonInvoicePos' and sc.[name] = 'DscAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [DscAmount] 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_MareonInvoicePos' and sc.[name] = 'VAT')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [VAT] 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_MareonInvoicePos' and sc.[name] = 'VATAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [VATAmount] 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_MareonInvoicePos' and sc.[name] = 'wagesAmount')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [wagesAmount] 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_MareonInvoicePos' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [FPA] 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_MareonInvoicePos' and sc.[name] = 'TextPos')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [TextPos] 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_MareonInvoicePos' and sc.[name] = 'TextPdf')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [TextPdf] 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_MareonInvoicePos' and sc.[name] = 'Betrag')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [Betrag] 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_MareonInvoicePos' and sc.[name] = 'nettoGP')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [nettoGP] 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_MareonInvoicePos' and sc.[name] = 'nettoBetrag')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [nettoBetrag] 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_MareonInvoicePos' and sc.[name] = 'HwpLkz')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [HwpLkz] 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_MareonInvoicePos' and sc.[name] = 'HwpArtnr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [HwpArtnr] 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_MareonInvoicePos' and sc.[name] = 'OZ')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [OZ] 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_MareonInvoicePos' and sc.[name] = 'TextOnly')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ADD [TextOnly] tinyint 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_MareonInvoicePos' and sc.[name] = 'NrBsh')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'TodoKey')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos' and sc.[name] = 'Description')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [Description] char(50)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'Unit')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [Unit] char(6)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'FPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [FPA] char(20)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'HwpLkz')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [HwpLkz] char(2)
END
GO
IF 18 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'HwpArtnr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [HwpArtnr] char(18)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonInvoicePos' and sc.[name] = 'OZ')
BEGIN
  ALTER TABLE [dbo].[ut_MareonInvoicePos] ALTER COLUMN [OZ] char(20)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonInvoicePos')
  DROP VIEW [dbo].[MareonInvoicePos]
GO
CREATE VIEW [dbo].[MareonInvoicePos] AS
  SELECT * FROM [dbo].[ut_MareonInvoicePos]
GO
CREATE TABLE [dbo].[ut_MareonInvoicePos_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(35) NOT NULL
  CONSTRAINT [Pk_ut_MareonInvoicePos_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonInvoicePos_KeyDel] ON [dbo].[ut_MareonInvoicePos]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonInvoicePos_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Add_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonInvoicePos] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonInvoicePos])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonInvoicePos] (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_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Upd_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonInvoicePos]
  @RI integer
 ,@Parm_NrBsh char(20)
 ,@Parm_PosType integer
 ,@Parm_Pos integer
 ,@Parm_HostDate datetime
 ,@Parm_HostPos tinyint
 ,@Parm_Visible tinyint
 ,@Parm_TodoKey char(20)
 ,@Parm_Description char(50)
 ,@Parm_Quantity float
 ,@Parm_Price float
 ,@Parm_Unit char(6)
 ,@Parm_Discount float
 ,@Parm_DscAmount float
 ,@Parm_VAT float
 ,@Parm_VATAmount float
 ,@Parm_wagesAmount float
 ,@Parm_FPA char(20)
 ,@Parm_TextPos integer
 ,@Parm_TextPdf integer
 ,@Parm_Betrag float
 ,@Parm_nettoGP float
 ,@Parm_nettoBetrag float
 ,@Parm_HwpLkz char(2)
 ,@Parm_HwpArtnr char(18)
 ,@Parm_OZ char(20)
 ,@Parm_TextOnly tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonInvoicePos] SET
     [NrBsh] = RTRIM(@Parm_NrBsh)
    ,[PosType] = @Parm_PosType
    ,[Pos] = @Parm_Pos
    ,[HostDate] = @Parm_HostDate
    ,[HostPos] = @Parm_HostPos
    ,[Visible] = @Parm_Visible
    ,[TodoKey] = RTRIM(@Parm_TodoKey)
    ,[Description] = RTRIM(@Parm_Description)
    ,[Quantity] = @Parm_Quantity
    ,[Price] = @Parm_Price
    ,[Unit] = RTRIM(@Parm_Unit)
    ,[Discount] = @Parm_Discount
    ,[DscAmount] = @Parm_DscAmount
    ,[VAT] = @Parm_VAT
    ,[VATAmount] = @Parm_VATAmount
    ,[wagesAmount] = @Parm_wagesAmount
    ,[FPA] = RTRIM(@Parm_FPA)
    ,[TextPos] = @Parm_TextPos
    ,[TextPdf] = @Parm_TextPdf
    ,[Betrag] = @Parm_Betrag
    ,[nettoGP] = @Parm_nettoGP
    ,[nettoBetrag] = @Parm_nettoBetrag
    ,[HwpLkz] = RTRIM(@Parm_HwpLkz)
    ,[HwpArtnr] = RTRIM(@Parm_HwpArtnr)
    ,[OZ] = RTRIM(@Parm_OZ)
    ,[TextOnly] = @Parm_TextOnly
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Sel_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonInvoicePos]
  @RI integer
AS
SELECT
     [NrBsh]
    ,[PosType]
    ,[Pos]
    ,[HostDate]
    ,[HostPos]
    ,[Visible]
    ,[TodoKey]
    ,[Description]
    ,[Quantity]
    ,[Price]
    ,[Unit]
    ,[Discount]
    ,[DscAmount]
    ,[VAT]
    ,[VATAmount]
    ,[wagesAmount]
    ,[FPA]
    ,[TextPos]
    ,[TextPdf]
    ,[Betrag]
    ,[nettoGP]
    ,[nettoBetrag]
    ,[HwpLkz]
    ,[HwpArtnr]
    ,[OZ]
    ,[TextOnly]
  FROM [dbo].[ut_MareonInvoicePos]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Del_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonInvoicePos]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonInvoicePos] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Reorg_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonInvoicePos]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonInvoicePos] 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_MareonInvoicePos] 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_MareonInvoicePos] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_Append_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonInvoicePos]
  @Parm_NrBsh char(20)
 ,@Parm_PosType integer
 ,@Parm_Pos integer
 ,@Parm_HostDate datetime
 ,@Parm_HostPos tinyint
 ,@Parm_Visible tinyint
 ,@Parm_TodoKey char(20)
 ,@Parm_Description char(50)
 ,@Parm_Quantity float
 ,@Parm_Price float
 ,@Parm_Unit char(6)
 ,@Parm_Discount float
 ,@Parm_DscAmount float
 ,@Parm_VAT float
 ,@Parm_VATAmount float
 ,@Parm_wagesAmount float
 ,@Parm_FPA char(20)
 ,@Parm_TextPos integer
 ,@Parm_TextPdf integer
 ,@Parm_Betrag float
 ,@Parm_nettoGP float
 ,@Parm_nettoBetrag float
 ,@Parm_HwpLkz char(2)
 ,@Parm_HwpArtnr char(18)
 ,@Parm_OZ char(20)
 ,@Parm_TextOnly tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonInvoicePos])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonInvoicePos] (
    RecId
   ,[NrBsh]
   ,[PosType]
   ,[Pos]
   ,[HostDate]
   ,[HostPos]
   ,[Visible]
   ,[TodoKey]
   ,[Description]
   ,[Quantity]
   ,[Price]
   ,[Unit]
   ,[Discount]
   ,[DscAmount]
   ,[VAT]
   ,[VATAmount]
   ,[wagesAmount]
   ,[FPA]
   ,[TextPos]
   ,[TextPdf]
   ,[Betrag]
   ,[nettoGP]
   ,[nettoBetrag]
   ,[HwpLkz]
   ,[HwpArtnr]
   ,[OZ]
   ,[TextOnly]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NrBsh)
   ,@Parm_PosType
   ,@Parm_Pos
   ,@Parm_HostDate
   ,@Parm_HostPos
   ,@Parm_Visible
   ,RTRIM(@Parm_TodoKey)
   ,RTRIM(@Parm_Description)
   ,@Parm_Quantity
   ,@Parm_Price
   ,RTRIM(@Parm_Unit)
   ,@Parm_Discount
   ,@Parm_DscAmount
   ,@Parm_VAT
   ,@Parm_VATAmount
   ,@Parm_wagesAmount
   ,RTRIM(@Parm_FPA)
   ,@Parm_TextPos
   ,@Parm_TextPdf
   ,@Parm_Betrag
   ,@Parm_nettoGP
   ,@Parm_nettoBetrag
   ,RTRIM(@Parm_HwpLkz)
   ,RTRIM(@Parm_HwpArtnr)
   ,RTRIM(@Parm_OZ)
   ,@Parm_TextOnly
       )
  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_MareonInvoicePos')
  DROP PROCEDURE [dbo].[up_TrSel_MareonInvoicePos]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonInvoicePos]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NrBsh]))
      +LTRIM(RTRIM([TodoKey]))
      +LTRIM(RTRIM([Description]))
      +LTRIM(RTRIM([Unit]))
      +LTRIM(RTRIM([FPA]))
      +LTRIM(RTRIM([HwpLkz]))
      +LTRIM(RTRIM([HwpArtnr]))
      +LTRIM(RTRIM([OZ]))
    AS Data
  FROM     [dbo].[ut_MareonInvoicePos]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonInvoicePos_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonInvoicePos_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonInvoicePos_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonInvoicePos_Key00]
GO

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

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

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

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

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

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

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

