------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_BestellAbruf')
BEGIN
CREATE TABLE [dbo].[ut_BestellAbruf]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NRHW] char(15) DEFAULT ' '
  ,[LfdNr] integer DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  CONSTRAINT [Pk_BestellAbruf]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BestellAbruf_KeyDel')
  DROP TRIGGER [dbo].[tr_BestellAbruf_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_BestellAbruf_KeyUpd')
  DROP TRIGGER [dbo].[tr_BestellAbruf_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_BestellAbruf_Keys')
  DROP TABLE [dbo].[ut_BestellAbruf_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbruf' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbruf] 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_BestellAbruf' and sc.[name] = 'LfdNr')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbruf] ADD [LfdNr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbruf' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbruf] ADD [Datum] datetime NOT NULL DEFAULT CONVERT(datetime,'1900-01-01',120)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_BestellAbruf' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_BestellAbruf] ALTER COLUMN [NRHW] char(15)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'BestellAbruf')
  DROP VIEW [dbo].[BestellAbruf]
GO
CREATE VIEW [dbo].[BestellAbruf] AS
  SELECT * FROM [dbo].[ut_BestellAbruf]
GO
CREATE TABLE [dbo].[ut_BestellAbruf_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[KEY0_Value] CHAR(33) NOT NULL
  CONSTRAINT [Pk_ut_BestellAbruf_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_BestellAbruf_KeyDel] ON [dbo].[ut_BestellAbruf]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_BestellAbruf_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_BestellAbruf')
  DROP PROCEDURE [dbo].[up_Add_BestellAbruf]
GO
CREATE PROCEDURE [dbo].[up_Add_BestellAbruf] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_BestellAbruf])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BestellAbruf] (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_BestellAbruf')
  DROP PROCEDURE [dbo].[up_Upd_BestellAbruf]
GO
CREATE PROCEDURE [dbo].[up_Upd_BestellAbruf]
  @RI integer
 ,@Parm_NRHW char(15)
 ,@Parm_LfdNr integer
 ,@Parm_Datum datetime
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_BestellAbruf] SET
     [NRHW] = RTRIM(@Parm_NRHW)
    ,[LfdNr] = @Parm_LfdNr
    ,[Datum] = @Parm_Datum
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

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

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_BestellAbruf')
  DROP PROCEDURE [dbo].[up_Append_BestellAbruf]
GO
CREATE PROCEDURE [dbo].[up_Append_BestellAbruf]
  @Parm_NRHW char(15)
 ,@Parm_LfdNr integer
 ,@Parm_Datum datetime
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_BestellAbruf])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_BestellAbruf] (
    RecId
   ,[NRHW]
   ,[LfdNr]
   ,[Datum]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NRHW)
   ,@Parm_LfdNr
   ,@Parm_Datum
       )
  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_BestellAbruf')
  DROP PROCEDURE [dbo].[up_TrSel_BestellAbruf]
GO
CREATE PROCEDURE [dbo].[up_TrSel_BestellAbruf]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NRHW]))
    AS Data
  FROM     [dbo].[ut_BestellAbruf]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_KEY0] ON [dbo].[ut_BestellAbruf_Keys]
( [KEY0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_First_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_Last_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_Next_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_Prior50_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_Next50_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_BestellAbruf_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_BestellAbruf_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_BestellAbruf_KEY0]
GO

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

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

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

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

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

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

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

