------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Bestelltexte')
BEGIN
CREATE TABLE [dbo].[ut_Bestelltexte]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[NRHW] char(15) DEFAULT ' '
  ,[POSNRHW] char(10) DEFAULT ' '
  ,[POSNRGH] char(10) DEFAULT ' '
  ,[LFDNR] char(2) DEFAULT ' '
  ,[TEXT] char(40) DEFAULT ' '
  CONSTRAINT [Pk_Bestelltexte]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestelltexte_KeyDel')
  DROP TRIGGER [dbo].[tr_Bestelltexte_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestelltexte_KeyUpd')
  DROP TRIGGER [dbo].[tr_Bestelltexte_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Bestelltexte_Keys')
  DROP TABLE [dbo].[ut_Bestelltexte_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestelltexte' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] 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_Bestelltexte' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] 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_Bestelltexte' and sc.[name] = 'POSNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] 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_Bestelltexte' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] ADD [LFDNR] 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_Bestelltexte' and sc.[name] = 'TEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] ADD [TEXT] char(40) 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_Bestelltexte' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] 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_Bestelltexte' and sc.[name] = 'POSNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] 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_Bestelltexte' and sc.[name] = 'POSNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] ALTER COLUMN [POSNRGH] char(10)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestelltexte' and sc.[name] = 'LFDNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] ALTER COLUMN [LFDNR] char(2)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestelltexte' and sc.[name] = 'TEXT')
BEGIN
  ALTER TABLE [dbo].[ut_Bestelltexte] ALTER COLUMN [TEXT] char(40)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Bestelltexte')
  DROP VIEW [dbo].[Bestelltexte]
GO
CREATE VIEW [dbo].[Bestelltexte] AS
  SELECT * FROM [dbo].[ut_Bestelltexte]
GO
CREATE TABLE [dbo].[ut_Bestelltexte_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[KEY0_Value] CHAR(37) NOT NULL
  CONSTRAINT [Pk_ut_Bestelltexte_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Bestelltexte_KeyDel] ON [dbo].[ut_Bestelltexte]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Bestelltexte_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Bestelltexte_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[KEY0_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+convert(char(2),[LFDNR])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Bestelltexte_Keys] WITH (SERIALIZABLE) SET
    [KEY0_Value] = UPPER(convert(char(15),[NRHW])+convert(char(10),[POSNRHW])+convert(char(2),[LFDNR])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Add_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Add_Bestelltexte] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Bestelltexte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestelltexte] (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_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Upd_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Upd_Bestelltexte]
  @RI integer
 ,@Parm_NRHW char(15)
 ,@Parm_POSNRHW char(10)
 ,@Parm_POSNRGH char(10)
 ,@Parm_LFDNR char(2)
 ,@Parm_TEXT char(40)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Bestelltexte] SET
     [NRHW] = RTRIM(@Parm_NRHW)
    ,[POSNRHW] = RTRIM(@Parm_POSNRHW)
    ,[POSNRGH] = RTRIM(@Parm_POSNRGH)
    ,[LFDNR] = RTRIM(@Parm_LFDNR)
    ,[TEXT] = RTRIM(@Parm_TEXT)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Sel_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Sel_Bestelltexte]
  @RI integer
AS
SELECT
     [NRHW]
    ,[POSNRHW]
    ,[POSNRGH]
    ,[LFDNR]
    ,[TEXT]
  FROM [dbo].[ut_Bestelltexte]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Del_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Del_Bestelltexte]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Bestelltexte] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Reorg_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Bestelltexte]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Bestelltexte] 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_Bestelltexte] 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_Bestelltexte] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Bestelltexte')
  DROP PROCEDURE [dbo].[up_Append_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_Append_Bestelltexte]
  @Parm_NRHW char(15)
 ,@Parm_POSNRHW char(10)
 ,@Parm_POSNRGH char(10)
 ,@Parm_LFDNR char(2)
 ,@Parm_TEXT char(40)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Bestelltexte])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestelltexte] (
    RecId
   ,[NRHW]
   ,[POSNRHW]
   ,[POSNRGH]
   ,[LFDNR]
   ,[TEXT]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_NRHW)
   ,RTRIM(@Parm_POSNRHW)
   ,RTRIM(@Parm_POSNRGH)
   ,RTRIM(@Parm_LFDNR)
   ,RTRIM(@Parm_TEXT)
       )
  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_Bestelltexte')
  DROP PROCEDURE [dbo].[up_TrSel_Bestelltexte]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Bestelltexte]
AS
  SELECT   RecId,
      LTRIM(RTRIM([NRHW]))
      +LTRIM(RTRIM([POSNRHW]))
      +LTRIM(RTRIM([POSNRGH]))
      +LTRIM(RTRIM([LFDNR]))
      +LTRIM(RTRIM([TEXT]))
    AS Data
  FROM     [dbo].[ut_Bestelltexte]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_KEY0] ON [dbo].[ut_Bestelltexte_Keys]
( [KEY0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_First_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_Last_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_Next_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_Prior50_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_Next50_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_Bestelltexte_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestelltexte_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_Bestelltexte_KEY0]
GO

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

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

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

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

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

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

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

