------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BelNe')
BEGIN
CREATE TABLE [dbo].[ut_BelNe]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PrjNr] char(8) DEFAULT ' '
  ,[BelArt] char(2) DEFAULT ' '
  ,[BelNr] char(8) DEFAULT ' '
  ,[RefKey] char(6) DEFAULT ' '
  ,[NeKz] char(2) DEFAULT ' '
  ,[Gewicht] float DEFAULT 0
  ,[Gebinde] float DEFAULT 0
  ,[BasisPreis] float DEFAULT 0
  ,[KgJeBasis] float DEFAULT 0
  ,[TagesPreis] float DEFAULT 0
  CONSTRAINT [Pk_BelNe]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelNe_KeyDel')
  DROP TRIGGER [dbo].[tr_BelNe_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BelNe_KeyUpd')
  DROP TRIGGER [dbo].[tr_BelNe_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BelNe_Keys')
  DROP TABLE [dbo].[ut_BelNe_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] 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_BelNe' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [BelArt] 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_BelNe' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [BelNr] 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_BelNe' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [RefKey] 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_BelNe' and sc.[name] = 'NeKz')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [NeKz] 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_BelNe' and sc.[name] = 'Gewicht')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [Gewicht] 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_BelNe' and sc.[name] = 'Gebinde')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [Gebinde] 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_BelNe' and sc.[name] = 'BasisPreis')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [BasisPreis] 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_BelNe' and sc.[name] = 'KgJeBasis')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [KgJeBasis] 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_BelNe' and sc.[name] = 'TagesPreis')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ADD [TagesPreis] float NOT NULL DEFAULT 0
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'PrjNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ALTER COLUMN [PrjNr] char(8)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'BelArt')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ALTER COLUMN [BelArt] char(2)
END
GO
IF 8 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'BelNr')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ALTER COLUMN [BelNr] char(8)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'RefKey')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ALTER COLUMN [RefKey] char(6)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BelNe' and sc.[name] = 'NeKz')
BEGIN
  ALTER TABLE [dbo].[ut_BelNe] ALTER COLUMN [NeKz] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BelNe')
  DROP VIEW [dbo].[BelNe]
GO
CREATE VIEW [dbo].[BelNe] AS
  SELECT * FROM [dbo].[ut_BelNe]
GO
CREATE TABLE [dbo].[ut_BelNe_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(36) NOT NULL
  CONSTRAINT [Pk_ut_BelNe_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BelNe_KeyDel] ON [dbo].[ut_BelNe]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BelNe_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_BelNe_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(2),[NeKz])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_BelNe_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(8),[PrjNr])+convert(char(2),[BelArt])+convert(char(8),[BelNr])+convert(char(6),[RefKey])+convert(char(2),[NeKz])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BelNe')
  DROP PROCEDURE [dbo].[up_Add_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Add_BelNe] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BelNe])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelNe] (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_BelNe')
  DROP PROCEDURE [dbo].[up_Upd_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Upd_BelNe]
  @RI integer
 ,@Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_NeKz char(2)
 ,@Parm_Gewicht float
 ,@Parm_Gebinde float
 ,@Parm_BasisPreis float
 ,@Parm_KgJeBasis float
 ,@Parm_TagesPreis float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BelNe] SET
     [PrjNr] = RTRIM(@Parm_PrjNr)
    ,[BelArt] = RTRIM(@Parm_BelArt)
    ,[BelNr] = RTRIM(@Parm_BelNr)
    ,[RefKey] = RTRIM(@Parm_RefKey)
    ,[NeKz] = RTRIM(@Parm_NeKz)
    ,[Gewicht] = @Parm_Gewicht
    ,[Gebinde] = @Parm_Gebinde
    ,[BasisPreis] = @Parm_BasisPreis
    ,[KgJeBasis] = @Parm_KgJeBasis
    ,[TagesPreis] = @Parm_TagesPreis
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_BelNe')
  DROP PROCEDURE [dbo].[up_Sel_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Sel_BelNe]
  @RI integer
AS
SELECT
     [PrjNr]
    ,[BelArt]
    ,[BelNr]
    ,[RefKey]
    ,[NeKz]
    ,[Gewicht]
    ,[Gebinde]
    ,[BasisPreis]
    ,[KgJeBasis]
    ,[TagesPreis]
  FROM [dbo].[ut_BelNe]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_BelNe')
  DROP PROCEDURE [dbo].[up_Del_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Del_BelNe]
  @RI integer
AS
  DELETE FROM [dbo].[ut_BelNe] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_BelNe')
  DROP PROCEDURE [dbo].[up_Reorg_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Reorg_BelNe]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_BelNe] 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_BelNe] 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_BelNe] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BelNe')
  DROP PROCEDURE [dbo].[up_Append_BelNe]
GO
CREATE PROCEDURE [dbo].[up_Append_BelNe]
  @Parm_PrjNr char(8)
 ,@Parm_BelArt char(2)
 ,@Parm_BelNr char(8)
 ,@Parm_RefKey char(6)
 ,@Parm_NeKz char(2)
 ,@Parm_Gewicht float
 ,@Parm_Gebinde float
 ,@Parm_BasisPreis float
 ,@Parm_KgJeBasis float
 ,@Parm_TagesPreis float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BelNe])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BelNe] (
    RecId
   ,[PrjNr]
   ,[BelArt]
   ,[BelNr]
   ,[RefKey]
   ,[NeKz]
   ,[Gewicht]
   ,[Gebinde]
   ,[BasisPreis]
   ,[KgJeBasis]
   ,[TagesPreis]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PrjNr)
   ,RTRIM(@Parm_BelArt)
   ,RTRIM(@Parm_BelNr)
   ,RTRIM(@Parm_RefKey)
   ,RTRIM(@Parm_NeKz)
   ,@Parm_Gewicht
   ,@Parm_Gebinde
   ,@Parm_BasisPreis
   ,@Parm_KgJeBasis
   ,@Parm_TagesPreis
       )
  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_BelNe')
  DROP PROCEDURE [dbo].[up_TrSel_BelNe]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BelNe]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PrjNr]))
      +LTRIM(RTRIM([BelArt]))
      +LTRIM(RTRIM([BelNr]))
      +LTRIM(RTRIM([RefKey]))
      +LTRIM(RTRIM([NeKz]))
    AS Data
  FROM     [dbo].[ut_BelNe]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_BelNe_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_First_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_Last_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_Prior_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_Next_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_Next50_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_FindL_BelNe_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BelNe_Key0')
  DROP PROCEDURE [dbo].[up_FindF_BelNe_Key0]
GO

CREATE PROCEDURE [dbo].[up_First_BelNe_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelNe_Keys] ORDER BY [Key0_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_BelNe_Key0]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelNe_Keys] ORDER BY [Key0_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_BelNe_Key0] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_BelNe_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_BelNe_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_BelNe_Key0] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_BelNe_Keys]
  WHERE [Key0_Value] > (SELECT [Key0_Value] FROM [dbo].[ut_BelNe_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key0_Value]
GO

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

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

