------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_MareonCompany')
BEGIN
CREATE TABLE [dbo].[ut_MareonCompany]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[UNR] char(20) DEFAULT ' '
  ,[SubUNR] char(20) DEFAULT ' '
  ,[DestNr] char(15) DEFAULT ' '
  ,[PrjNr] char(8) DEFAULT ' '
  ,[HostDate] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[MainOffice] tinyint DEFAULT 0
  ,[SoapUser] char(50) DEFAULT ' '
  ,[SoapPass] char(50) DEFAULT ' '
  ,[Name1] char(40) DEFAULT ' '
  ,[Name2] char(40) DEFAULT ' '
  ,[Street] char(40) DEFAULT ' '
  ,[Zip] char(8) DEFAULT ' '
  ,[City] char(40) DEFAULT ' '
  ,[PN] char(20) DEFAULT ' '
  ,[ErpType] char(30) DEFAULT ' '
  ,[archive] tinyint DEFAULT 0
  ,[dis13b] tinyint DEFAULT 0
  ,[dis35a] tinyint DEFAULT 0
  ,[disDiscnt] tinyint DEFAULT 0
  ,[disInsurCl] tinyint DEFAULT 0
  ,[disInvoice] tinyint DEFAULT 0
  ,[disInvText] tinyint DEFAULT 0
  ,[disPartInv] tinyint DEFAULT 0
  ,[disSkonto] tinyint DEFAULT 0
  ,[disTenantL] tinyint DEFAULT 0
  ,[disTermPay] tinyint DEFAULT 0
  ,[failInvSum] tinyint DEFAULT 0
  ,[RestOrdPos] tinyint DEFAULT 0
  ,[inventMgmt] tinyint DEFAULT 0
  ,[invWoOrder] tinyint DEFAULT 0
  ,[mxLenInvNo] integer DEFAULT 0
  ,[ownerDiff] tinyint DEFAULT 0
  ,[signature] tinyint DEFAULT 0
  ,[SubUnrCond] tinyint DEFAULT 0
  ,[restCancRs] tinyint DEFAULT 0
  ,[LieferSchein] tinyint DEFAULT 0
  ,[FPosAllways] tinyint DEFAULT 0
  ,[AutoArtikel] tinyint DEFAULT 0
  ,[Portaltyp] char(10) DEFAULT ' '
  ,[NrReserv] tinyint DEFAULT 0
  ,[EnabAllFPA] tinyint DEFAULT 0
  ,[FltDateOrd] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[FltDateInv] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[EnabCondAc] tinyint DEFAULT 0
  ,[EnabPark] tinyint DEFAULT 0
  ,[FpaPosDisc] tinyint DEFAULT 0
  CONSTRAINT [Pk_MareonCompany]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonCompany_KeyDel')
  DROP TRIGGER [dbo].[tr_MareonCompany_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_MareonCompany_KeyUpd')
  DROP TRIGGER [dbo].[tr_MareonCompany_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_MareonCompany_Keys')
  DROP TABLE [dbo].[ut_MareonCompany_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [UNR] 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_MareonCompany' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [SubUNR] 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_MareonCompany' and sc.[name] = 'DestNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [DestNr] 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_MareonCompany' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [PrjNr] char(8) 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_MareonCompany' and sc.[name] = 'HostDate')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] 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_MareonCompany' and sc.[name] = 'MainOffice')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [MainOffice] 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_MareonCompany' and sc.[name] = 'SoapUser')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [SoapUser] 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_MareonCompany' and sc.[name] = 'SoapPass')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [SoapPass] 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_MareonCompany' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [Name1] 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_MareonCompany' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [Name2] 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_MareonCompany' and sc.[name] = 'Street')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [Street] 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_MareonCompany' and sc.[name] = 'Zip')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [Zip] char(8) 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_MareonCompany' and sc.[name] = 'City')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [City] 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_MareonCompany' and sc.[name] = 'PN')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [PN] 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_MareonCompany' and sc.[name] = 'ErpType')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [ErpType] 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_MareonCompany' and sc.[name] = 'archive')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [archive] 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_MareonCompany' and sc.[name] = 'dis13b')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [dis13b] 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_MareonCompany' and sc.[name] = 'dis35a')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [dis35a] 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_MareonCompany' and sc.[name] = 'disDiscnt')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disDiscnt] 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_MareonCompany' and sc.[name] = 'disInsurCl')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disInsurCl] 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_MareonCompany' and sc.[name] = 'disInvoice')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disInvoice] 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_MareonCompany' and sc.[name] = 'disInvText')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disInvText] 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_MareonCompany' and sc.[name] = 'disPartInv')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disPartInv] 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_MareonCompany' and sc.[name] = 'disSkonto')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disSkonto] 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_MareonCompany' and sc.[name] = 'disTenantL')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disTenantL] 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_MareonCompany' and sc.[name] = 'disTermPay')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [disTermPay] 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_MareonCompany' and sc.[name] = 'failInvSum')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [failInvSum] 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_MareonCompany' and sc.[name] = 'RestOrdPos')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [RestOrdPos] 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_MareonCompany' and sc.[name] = 'inventMgmt')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [inventMgmt] 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_MareonCompany' and sc.[name] = 'invWoOrder')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [invWoOrder] 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_MareonCompany' and sc.[name] = 'mxLenInvNo')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [mxLenInvNo] 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_MareonCompany' and sc.[name] = 'ownerDiff')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [ownerDiff] 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_MareonCompany' and sc.[name] = 'signature')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [signature] 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_MareonCompany' and sc.[name] = 'SubUnrCond')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [SubUnrCond] 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_MareonCompany' and sc.[name] = 'restCancRs')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [restCancRs] 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_MareonCompany' and sc.[name] = 'LieferSchein')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [LieferSchein] 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_MareonCompany' and sc.[name] = 'FPosAllways')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [FPosAllways] 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_MareonCompany' and sc.[name] = 'AutoArtikel')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [AutoArtikel] 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_MareonCompany' and sc.[name] = 'Portaltyp')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [Portaltyp] 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_MareonCompany' and sc.[name] = 'NrReserv')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [NrReserv] 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_MareonCompany' and sc.[name] = 'EnabAllFPA')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [EnabAllFPA] 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_MareonCompany' and sc.[name] = 'FltDateOrd')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [FltDateOrd] 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_MareonCompany' and sc.[name] = 'FltDateInv')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [FltDateInv] 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_MareonCompany' and sc.[name] = 'EnabCondAc')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [EnabCondAc] 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_MareonCompany' and sc.[name] = 'EnabPark')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [EnabPark] 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_MareonCompany' and sc.[name] = 'FpaPosDisc')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ADD [FpaPosDisc] 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_MareonCompany' and sc.[name] = 'UNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [UNR] char(20)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'SubUNR')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [SubUNR] char(20)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'DestNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [DestNr] char(15)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'SoapUser')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [SoapUser] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'SoapPass')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [SoapPass] char(50)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'Name1')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [Name1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'Name2')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [Name2] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'Street')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [Street] char(40)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'Zip')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [Zip] char(8)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'City')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [City] char(40)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'PN')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [PN] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'ErpType')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [ErpType] char(30)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_MareonCompany' and sc.[name] = 'Portaltyp')
BEGIN
  ALTER TABLE [dbo].[ut_MareonCompany] ALTER COLUMN [Portaltyp] char(10)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'MareonCompany')
  DROP VIEW [dbo].[MareonCompany]
GO
CREATE VIEW [dbo].[MareonCompany] AS
  SELECT * FROM [dbo].[ut_MareonCompany]
GO
CREATE TABLE [dbo].[ut_MareonCompany_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(50) NOT NULL
  ,[Key01_Value] CHAR(25) NOT NULL
  ,[Key02_Filter] TINYINT NOT NULL
  ,[Key02_Value] CHAR(30) NOT NULL
  CONSTRAINT [Pk_ut_MareonCompany_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_MareonCompany_KeyDel] ON [dbo].[ut_MareonCompany]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_MareonCompany_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_MareonCompany_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Filter],[Key02_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+STR(RecId,10))
     ,UPPER(convert(char(15),[DestNr])+STR(RecId,10))
     ,CASE WHEN (1 =  MainOffice) THEN 1 ELSE 0 END
     ,UPPER(convert(char(20),[UNR])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_MareonCompany_Keys] WITH (SERIALIZABLE) SET
    [Key00_Value] = UPPER(convert(char(20),[UNR])+convert(char(20),[SubUNR])+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(15),[DestNr])+STR([RecId],10))
   ,[Key02_Value] = UPPER(convert(char(20),[UNR])+STR([RecId],10))
   ,[Key02_Filter] = CASE WHEN (1 =  MainOffice) THEN 1 ELSE 0 END
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_MareonCompany')
  DROP PROCEDURE [dbo].[up_Add_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Add_MareonCompany] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_MareonCompany])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonCompany] (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_MareonCompany')
  DROP PROCEDURE [dbo].[up_Upd_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Upd_MareonCompany]
  @RI integer
 ,@Parm_UNR char(20)
 ,@Parm_SubUNR char(20)
 ,@Parm_DestNr char(15)
 ,@Parm_PrjNr char(8)
 ,@Parm_HostDate datetime
 ,@Parm_MainOffice tinyint
 ,@Parm_SoapUser char(50)
 ,@Parm_SoapPass char(50)
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Street char(40)
 ,@Parm_Zip char(8)
 ,@Parm_City char(40)
 ,@Parm_PN char(20)
 ,@Parm_ErpType char(30)
 ,@Parm_archive tinyint
 ,@Parm_dis13b tinyint
 ,@Parm_dis35a tinyint
 ,@Parm_disDiscnt tinyint
 ,@Parm_disInsurCl tinyint
 ,@Parm_disInvoice tinyint
 ,@Parm_disInvText tinyint
 ,@Parm_disPartInv tinyint
 ,@Parm_disSkonto tinyint
 ,@Parm_disTenantL tinyint
 ,@Parm_disTermPay tinyint
 ,@Parm_failInvSum tinyint
 ,@Parm_RestOrdPos tinyint
 ,@Parm_inventMgmt tinyint
 ,@Parm_invWoOrder tinyint
 ,@Parm_mxLenInvNo integer
 ,@Parm_ownerDiff tinyint
 ,@Parm_signature tinyint
 ,@Parm_SubUnrCond tinyint
 ,@Parm_restCancRs tinyint
 ,@Parm_LieferSchein tinyint
 ,@Parm_FPosAllways tinyint
 ,@Parm_AutoArtikel tinyint
 ,@Parm_Portaltyp char(10)
 ,@Parm_NrReserv tinyint
 ,@Parm_EnabAllFPA tinyint
 ,@Parm_FltDateOrd datetime
 ,@Parm_FltDateInv datetime
 ,@Parm_EnabCondAc tinyint
 ,@Parm_EnabPark tinyint
 ,@Parm_FpaPosDisc tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_MareonCompany] SET
     [UNR] = RTRIM(@Parm_UNR)
    ,[SubUNR] = RTRIM(@Parm_SubUNR)
    ,[DestNr] = RTRIM(@Parm_DestNr)
    ,[PrjNr] = RTRIM(@Parm_PrjNr)
    ,[HostDate] = @Parm_HostDate
    ,[MainOffice] = @Parm_MainOffice
    ,[SoapUser] = RTRIM(@Parm_SoapUser)
    ,[SoapPass] = RTRIM(@Parm_SoapPass)
    ,[Name1] = RTRIM(@Parm_Name1)
    ,[Name2] = RTRIM(@Parm_Name2)
    ,[Street] = RTRIM(@Parm_Street)
    ,[Zip] = RTRIM(@Parm_Zip)
    ,[City] = RTRIM(@Parm_City)
    ,[PN] = RTRIM(@Parm_PN)
    ,[ErpType] = RTRIM(@Parm_ErpType)
    ,[archive] = @Parm_archive
    ,[dis13b] = @Parm_dis13b
    ,[dis35a] = @Parm_dis35a
    ,[disDiscnt] = @Parm_disDiscnt
    ,[disInsurCl] = @Parm_disInsurCl
    ,[disInvoice] = @Parm_disInvoice
    ,[disInvText] = @Parm_disInvText
    ,[disPartInv] = @Parm_disPartInv
    ,[disSkonto] = @Parm_disSkonto
    ,[disTenantL] = @Parm_disTenantL
    ,[disTermPay] = @Parm_disTermPay
    ,[failInvSum] = @Parm_failInvSum
    ,[RestOrdPos] = @Parm_RestOrdPos
    ,[inventMgmt] = @Parm_inventMgmt
    ,[invWoOrder] = @Parm_invWoOrder
    ,[mxLenInvNo] = @Parm_mxLenInvNo
    ,[ownerDiff] = @Parm_ownerDiff
    ,[signature] = @Parm_signature
    ,[SubUnrCond] = @Parm_SubUnrCond
    ,[restCancRs] = @Parm_restCancRs
    ,[LieferSchein] = @Parm_LieferSchein
    ,[FPosAllways] = @Parm_FPosAllways
    ,[AutoArtikel] = @Parm_AutoArtikel
    ,[Portaltyp] = RTRIM(@Parm_Portaltyp)
    ,[NrReserv] = @Parm_NrReserv
    ,[EnabAllFPA] = @Parm_EnabAllFPA
    ,[FltDateOrd] = @Parm_FltDateOrd
    ,[FltDateInv] = @Parm_FltDateInv
    ,[EnabCondAc] = @Parm_EnabCondAc
    ,[EnabPark] = @Parm_EnabPark
    ,[FpaPosDisc] = @Parm_FpaPosDisc
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_MareonCompany')
  DROP PROCEDURE [dbo].[up_Sel_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Sel_MareonCompany]
  @RI integer
AS
SELECT
     [UNR]
    ,[SubUNR]
    ,[DestNr]
    ,[PrjNr]
    ,[HostDate]
    ,[MainOffice]
    ,[SoapUser]
    ,[SoapPass]
    ,[Name1]
    ,[Name2]
    ,[Street]
    ,[Zip]
    ,[City]
    ,[PN]
    ,[ErpType]
    ,[archive]
    ,[dis13b]
    ,[dis35a]
    ,[disDiscnt]
    ,[disInsurCl]
    ,[disInvoice]
    ,[disInvText]
    ,[disPartInv]
    ,[disSkonto]
    ,[disTenantL]
    ,[disTermPay]
    ,[failInvSum]
    ,[RestOrdPos]
    ,[inventMgmt]
    ,[invWoOrder]
    ,[mxLenInvNo]
    ,[ownerDiff]
    ,[signature]
    ,[SubUnrCond]
    ,[restCancRs]
    ,[LieferSchein]
    ,[FPosAllways]
    ,[AutoArtikel]
    ,[Portaltyp]
    ,[NrReserv]
    ,[EnabAllFPA]
    ,[FltDateOrd]
    ,[FltDateInv]
    ,[EnabCondAc]
    ,[EnabPark]
    ,[FpaPosDisc]
  FROM [dbo].[ut_MareonCompany]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_MareonCompany')
  DROP PROCEDURE [dbo].[up_Del_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Del_MareonCompany]
  @RI integer
AS
  DELETE FROM [dbo].[ut_MareonCompany] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_MareonCompany')
  DROP PROCEDURE [dbo].[up_Reorg_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Reorg_MareonCompany]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_MareonCompany] 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_MareonCompany] 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_MareonCompany] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_MareonCompany')
  DROP PROCEDURE [dbo].[up_Append_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_Append_MareonCompany]
  @Parm_UNR char(20)
 ,@Parm_SubUNR char(20)
 ,@Parm_DestNr char(15)
 ,@Parm_PrjNr char(8)
 ,@Parm_HostDate datetime
 ,@Parm_MainOffice tinyint
 ,@Parm_SoapUser char(50)
 ,@Parm_SoapPass char(50)
 ,@Parm_Name1 char(40)
 ,@Parm_Name2 char(40)
 ,@Parm_Street char(40)
 ,@Parm_Zip char(8)
 ,@Parm_City char(40)
 ,@Parm_PN char(20)
 ,@Parm_ErpType char(30)
 ,@Parm_archive tinyint
 ,@Parm_dis13b tinyint
 ,@Parm_dis35a tinyint
 ,@Parm_disDiscnt tinyint
 ,@Parm_disInsurCl tinyint
 ,@Parm_disInvoice tinyint
 ,@Parm_disInvText tinyint
 ,@Parm_disPartInv tinyint
 ,@Parm_disSkonto tinyint
 ,@Parm_disTenantL tinyint
 ,@Parm_disTermPay tinyint
 ,@Parm_failInvSum tinyint
 ,@Parm_RestOrdPos tinyint
 ,@Parm_inventMgmt tinyint
 ,@Parm_invWoOrder tinyint
 ,@Parm_mxLenInvNo integer
 ,@Parm_ownerDiff tinyint
 ,@Parm_signature tinyint
 ,@Parm_SubUnrCond tinyint
 ,@Parm_restCancRs tinyint
 ,@Parm_LieferSchein tinyint
 ,@Parm_FPosAllways tinyint
 ,@Parm_AutoArtikel tinyint
 ,@Parm_Portaltyp char(10)
 ,@Parm_NrReserv tinyint
 ,@Parm_EnabAllFPA tinyint
 ,@Parm_FltDateOrd datetime
 ,@Parm_FltDateInv datetime
 ,@Parm_EnabCondAc tinyint
 ,@Parm_EnabPark tinyint
 ,@Parm_FpaPosDisc tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_MareonCompany])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_MareonCompany] (
    RecId
   ,[UNR]
   ,[SubUNR]
   ,[DestNr]
   ,[PrjNr]
   ,[HostDate]
   ,[MainOffice]
   ,[SoapUser]
   ,[SoapPass]
   ,[Name1]
   ,[Name2]
   ,[Street]
   ,[Zip]
   ,[City]
   ,[PN]
   ,[ErpType]
   ,[archive]
   ,[dis13b]
   ,[dis35a]
   ,[disDiscnt]
   ,[disInsurCl]
   ,[disInvoice]
   ,[disInvText]
   ,[disPartInv]
   ,[disSkonto]
   ,[disTenantL]
   ,[disTermPay]
   ,[failInvSum]
   ,[RestOrdPos]
   ,[inventMgmt]
   ,[invWoOrder]
   ,[mxLenInvNo]
   ,[ownerDiff]
   ,[signature]
   ,[SubUnrCond]
   ,[restCancRs]
   ,[LieferSchein]
   ,[FPosAllways]
   ,[AutoArtikel]
   ,[Portaltyp]
   ,[NrReserv]
   ,[EnabAllFPA]
   ,[FltDateOrd]
   ,[FltDateInv]
   ,[EnabCondAc]
   ,[EnabPark]
   ,[FpaPosDisc]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_UNR)
   ,RTRIM(@Parm_SubUNR)
   ,RTRIM(@Parm_DestNr)
   ,RTRIM(@Parm_PrjNr)
   ,@Parm_HostDate
   ,@Parm_MainOffice
   ,RTRIM(@Parm_SoapUser)
   ,RTRIM(@Parm_SoapPass)
   ,RTRIM(@Parm_Name1)
   ,RTRIM(@Parm_Name2)
   ,RTRIM(@Parm_Street)
   ,RTRIM(@Parm_Zip)
   ,RTRIM(@Parm_City)
   ,RTRIM(@Parm_PN)
   ,RTRIM(@Parm_ErpType)
   ,@Parm_archive
   ,@Parm_dis13b
   ,@Parm_dis35a
   ,@Parm_disDiscnt
   ,@Parm_disInsurCl
   ,@Parm_disInvoice
   ,@Parm_disInvText
   ,@Parm_disPartInv
   ,@Parm_disSkonto
   ,@Parm_disTenantL
   ,@Parm_disTermPay
   ,@Parm_failInvSum
   ,@Parm_RestOrdPos
   ,@Parm_inventMgmt
   ,@Parm_invWoOrder
   ,@Parm_mxLenInvNo
   ,@Parm_ownerDiff
   ,@Parm_signature
   ,@Parm_SubUnrCond
   ,@Parm_restCancRs
   ,@Parm_LieferSchein
   ,@Parm_FPosAllways
   ,@Parm_AutoArtikel
   ,RTRIM(@Parm_Portaltyp)
   ,@Parm_NrReserv
   ,@Parm_EnabAllFPA
   ,@Parm_FltDateOrd
   ,@Parm_FltDateInv
   ,@Parm_EnabCondAc
   ,@Parm_EnabPark
   ,@Parm_FpaPosDisc
       )
  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_MareonCompany')
  DROP PROCEDURE [dbo].[up_TrSel_MareonCompany]
GO
CREATE PROCEDURE [dbo].[up_TrSel_MareonCompany]
AS
  SELECT   RecId,
      LTRIM(RTRIM([UNR]))
      +LTRIM(RTRIM([SubUNR]))
      +LTRIM(RTRIM([DestNr]))
      +LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([SoapUser]))
      +LTRIM(RTRIM([SoapPass]))
      +LTRIM(RTRIM([Name1]))
      +LTRIM(RTRIM([Name2]))
      +LTRIM(RTRIM([Street]))
      +LTRIM(RTRIM([Zip]))
      +LTRIM(RTRIM([City]))
      +LTRIM(RTRIM([PN]))
      +LTRIM(RTRIM([ErpType]))
      +LTRIM(RTRIM([Portaltyp]))
    AS Data
  FROM     [dbo].[ut_MareonCompany]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key00] ON [dbo].[ut_MareonCompany_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_First_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_Last_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_Prior_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_Next_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_Next50_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_FindL_MareonCompany_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonCompany_Key00')
  DROP PROCEDURE [dbo].[up_FindF_MareonCompany_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_MareonCompany_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_First_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_Last_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_Prior_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_Next_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_Next50_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_FindL_MareonCompany_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonCompany_Key01')
  DROP PROCEDURE [dbo].[up_FindF_MareonCompany_Key01]
GO

CREATE PROCEDURE [dbo].[up_First_MareonCompany_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys] ORDER BY [Key01_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonCompany_Key01]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys] ORDER BY [Key01_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonCompany_Key01] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_MareonCompany_Key01] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key01_Value] > (SELECT [Key01_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key01_Value]
GO

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_MareonCompany_Keys]
( [Key02_Filter],[Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_First_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_Last_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_Prior_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_Next_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_Next50_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_FindL_MareonCompany_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_MareonCompany_Key02')
  DROP PROCEDURE [dbo].[up_FindF_MareonCompany_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_MareonCompany_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_MareonCompany_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_MareonCompany_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior_MareonCompany_Key02] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next50_MareonCompany_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Prior50_MareonCompany_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] < (SELECT [Key02_Value] FROM [dbo].[ut_MareonCompany_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value] DESC
GO

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

CREATE PROCEDURE [dbo].[up_FindL_MareonCompany_Key02] @xKey char(128),@xLen integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_MareonCompany_Keys]
  WHERE [Key02_Filter] = 1 AND [Key02_Value] like SUBSTRING(@xKey,1,@xLen)+'%'
  ORDER BY [Key02_Value] DESC
GO

