------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Bestellungen')
BEGIN
CREATE TABLE [dbo].[ut_Bestellungen]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[PRJNR] char(8) DEFAULT ' '
  ,[NRHW] char(15) DEFAULT ' '
  ,[NRGH] char(15) DEFAULT ' '
  ,[STATUS] char(2) DEFAULT ' '
  ,[KDNRHW] char(10) DEFAULT ' '
  ,[KDNRGH] char(10) DEFAULT ' '
  ,[LKZ] char(2) DEFAULT ' '
  ,[BETREFF] char(50) DEFAULT ' '
  ,[TERMIN] char(10) DEFAULT ' '
  ,[WAEHR] char(6) DEFAULT ' '
  ,[BEARB] char(40) DEFAULT ' '
  ,[INFO1] char(40) DEFAULT ' '
  ,[INFO2] char(40) DEFAULT ' '
  ,[INFO3] char(40) DEFAULT ' '
  ,[INFO4] char(40) DEFAULT ' '
  ,[LADRNR] char(15) DEFAULT ' '
  ,[KzGaeb] integer DEFAULT 0
  ,[AnfrArt] char(1) DEFAULT ' '
  ,[ABNr] char(15) DEFAULT ' '
  ,[VersArt] char(30) DEFAULT ' '
  ,[PrBindDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[AbgabeDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[OZMaske] char(9) DEFAULT ' '
  ,[nTitel] integer DEFAULT 0
  ,[ReplArtNr] tinyint DEFAULT 0
  ,[ReplKtxt1] tinyint DEFAULT 0
  ,[ReplKtxt2] tinyint DEFAULT 0
  ,[ReplVk1M] tinyint DEFAULT 0
  ,[Datum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[WiederVorl] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[NettoSumme] float DEFAULT 0
  ,[AlsZubehoer] tinyint DEFAULT 0
  CONSTRAINT [Pk_Bestellungen]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestellungen_KeyDel')
  DROP TRIGGER [dbo].[tr_Bestellungen_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Bestellungen_KeyUpd')
  DROP TRIGGER [dbo].[tr_Bestellungen_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Bestellungen_Keys')
  DROP TABLE [dbo].[ut_Bestellungen_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] 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_Bestellungen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] 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_Bestellungen' and sc.[name] = 'NRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [NRGH] 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_Bestellungen' and sc.[name] = 'STATUS')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [STATUS] 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_Bestellungen' and sc.[name] = 'KDNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [KDNRHW] 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_Bestellungen' and sc.[name] = 'KDNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [KDNRGH] 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_Bestellungen' and sc.[name] = 'LKZ')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [LKZ] 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_Bestellungen' and sc.[name] = 'BETREFF')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [BETREFF] 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_Bestellungen' and sc.[name] = 'TERMIN')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [TERMIN] 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_Bestellungen' and sc.[name] = 'WAEHR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [WAEHR] 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_Bestellungen' and sc.[name] = 'BEARB')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [BEARB] 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_Bestellungen' and sc.[name] = 'INFO1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [INFO1] 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_Bestellungen' and sc.[name] = 'INFO2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [INFO2] 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_Bestellungen' and sc.[name] = 'INFO3')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [INFO3] 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_Bestellungen' and sc.[name] = 'INFO4')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [INFO4] 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_Bestellungen' and sc.[name] = 'LADRNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [LADRNR] 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_Bestellungen' and sc.[name] = 'KzGaeb')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [KzGaeb] 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_Bestellungen' and sc.[name] = 'AnfrArt')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [AnfrArt] char(1) 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_Bestellungen' and sc.[name] = 'ABNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [ABNr] 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_Bestellungen' and sc.[name] = 'VersArt')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [VersArt] 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_Bestellungen' and sc.[name] = 'PrBindDatum')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [PrBindDatum] 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_Bestellungen' and sc.[name] = 'AbgabeDatum')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [AbgabeDatum] 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_Bestellungen' and sc.[name] = 'OZMaske')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [OZMaske] char(9) 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_Bestellungen' and sc.[name] = 'nTitel')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [nTitel] 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_Bestellungen' and sc.[name] = 'ReplArtNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [ReplArtNr] 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_Bestellungen' and sc.[name] = 'ReplKtxt1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [ReplKtxt1] 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_Bestellungen' and sc.[name] = 'ReplKtxt2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [ReplKtxt2] 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_Bestellungen' and sc.[name] = 'ReplVk1M')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [ReplVk1M] 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_Bestellungen' and sc.[name] = 'Datum')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [Datum] 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_Bestellungen' and sc.[name] = 'WiederVorl')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [WiederVorl] 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_Bestellungen' and sc.[name] = 'NettoSumme')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [NettoSumme] 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_Bestellungen' and sc.[name] = 'AlsZubehoer')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ADD [AlsZubehoer] tinyint 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_Bestellungen' and sc.[name] = 'PRJNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [PRJNR] char(8)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'NRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [NRHW] char(15)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'NRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [NRGH] char(15)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'STATUS')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [STATUS] char(2)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'KDNRHW')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [KDNRHW] char(10)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'KDNRGH')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [KDNRGH] char(10)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'LKZ')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [LKZ] char(2)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'BETREFF')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [BETREFF] char(50)
END
GO
IF 10 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'TERMIN')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [TERMIN] char(10)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'WAEHR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [WAEHR] char(6)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'BEARB')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [BEARB] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'INFO1')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [INFO1] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'INFO2')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [INFO2] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'INFO3')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [INFO3] char(40)
END
GO
IF 40 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'INFO4')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [INFO4] char(40)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'LADRNR')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [LADRNR] char(15)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'AnfrArt')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [AnfrArt] char(1)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'ABNr')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [ABNr] char(15)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'VersArt')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [VersArt] char(30)
END
GO
IF 9 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Bestellungen' and sc.[name] = 'OZMaske')
BEGIN
  ALTER TABLE [dbo].[ut_Bestellungen] ALTER COLUMN [OZMaske] char(9)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Bestellungen')
  DROP VIEW [dbo].[Bestellungen]
GO
CREATE VIEW [dbo].[Bestellungen] AS
  SELECT * FROM [dbo].[ut_Bestellungen]
GO
CREATE TABLE [dbo].[ut_Bestellungen_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[KEY0_Value] CHAR(25) NOT NULL
  ,[KEY1_Value] CHAR(33) NOT NULL
  ,[KEY2_Value] CHAR(27) NOT NULL
  CONSTRAINT [Pk_ut_Bestellungen_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Bestellungen_KeyDel] ON [dbo].[ut_Bestellungen]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Bestellungen_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Bestellungen_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[KEY0_Value],[KEY1_Value],[KEY2_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(15),[NRHW])+STR(RecId,10))
     ,UPPER(convert(char(8),[PRJNR])+convert(char(15),[NRHW])+STR(RecId,10))
     ,UPPER(convert(char(2),[LKZ])+convert(char(15),[NRHW])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Bestellungen_Keys] WITH (SERIALIZABLE) SET
    [KEY0_Value] = UPPER(convert(char(15),[NRHW])+STR([RecId],10))
   ,[KEY1_Value] = UPPER(convert(char(8),[PRJNR])+convert(char(15),[NRHW])+STR([RecId],10))
   ,[KEY2_Value] = UPPER(convert(char(2),[LKZ])+convert(char(15),[NRHW])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Bestellungen')
  DROP PROCEDURE [dbo].[up_Add_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Add_Bestellungen] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Bestellungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestellungen] (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_Bestellungen')
  DROP PROCEDURE [dbo].[up_Upd_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Upd_Bestellungen]
  @RI integer
 ,@Parm_PRJNR char(8)
 ,@Parm_NRHW char(15)
 ,@Parm_NRGH char(15)
 ,@Parm_STATUS char(2)
 ,@Parm_KDNRHW char(10)
 ,@Parm_KDNRGH char(10)
 ,@Parm_LKZ char(2)
 ,@Parm_BETREFF char(50)
 ,@Parm_TERMIN char(10)
 ,@Parm_WAEHR char(6)
 ,@Parm_BEARB char(40)
 ,@Parm_INFO1 char(40)
 ,@Parm_INFO2 char(40)
 ,@Parm_INFO3 char(40)
 ,@Parm_INFO4 char(40)
 ,@Parm_LADRNR char(15)
 ,@Parm_KzGaeb integer
 ,@Parm_AnfrArt char(1)
 ,@Parm_ABNr char(15)
 ,@Parm_VersArt char(30)
 ,@Parm_PrBindDatum datetime
 ,@Parm_AbgabeDatum datetime
 ,@Parm_OZMaske char(9)
 ,@Parm_nTitel integer
 ,@Parm_ReplArtNr tinyint
 ,@Parm_ReplKtxt1 tinyint
 ,@Parm_ReplKtxt2 tinyint
 ,@Parm_ReplVk1M tinyint
 ,@Parm_Datum datetime
 ,@Parm_WiederVorl datetime
 ,@Parm_NettoSumme float
 ,@Parm_AlsZubehoer tinyint
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Bestellungen] SET
     [PRJNR] = RTRIM(@Parm_PRJNR)
    ,[NRHW] = RTRIM(@Parm_NRHW)
    ,[NRGH] = RTRIM(@Parm_NRGH)
    ,[STATUS] = RTRIM(@Parm_STATUS)
    ,[KDNRHW] = RTRIM(@Parm_KDNRHW)
    ,[KDNRGH] = RTRIM(@Parm_KDNRGH)
    ,[LKZ] = RTRIM(@Parm_LKZ)
    ,[BETREFF] = RTRIM(@Parm_BETREFF)
    ,[TERMIN] = RTRIM(@Parm_TERMIN)
    ,[WAEHR] = RTRIM(@Parm_WAEHR)
    ,[BEARB] = RTRIM(@Parm_BEARB)
    ,[INFO1] = RTRIM(@Parm_INFO1)
    ,[INFO2] = RTRIM(@Parm_INFO2)
    ,[INFO3] = RTRIM(@Parm_INFO3)
    ,[INFO4] = RTRIM(@Parm_INFO4)
    ,[LADRNR] = RTRIM(@Parm_LADRNR)
    ,[KzGaeb] = @Parm_KzGaeb
    ,[AnfrArt] = RTRIM(@Parm_AnfrArt)
    ,[ABNr] = RTRIM(@Parm_ABNr)
    ,[VersArt] = RTRIM(@Parm_VersArt)
    ,[PrBindDatum] = @Parm_PrBindDatum
    ,[AbgabeDatum] = @Parm_AbgabeDatum
    ,[OZMaske] = RTRIM(@Parm_OZMaske)
    ,[nTitel] = @Parm_nTitel
    ,[ReplArtNr] = @Parm_ReplArtNr
    ,[ReplKtxt1] = @Parm_ReplKtxt1
    ,[ReplKtxt2] = @Parm_ReplKtxt2
    ,[ReplVk1M] = @Parm_ReplVk1M
    ,[Datum] = @Parm_Datum
    ,[WiederVorl] = @Parm_WiederVorl
    ,[NettoSumme] = @Parm_NettoSumme
    ,[AlsZubehoer] = @Parm_AlsZubehoer
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Bestellungen')
  DROP PROCEDURE [dbo].[up_Sel_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Sel_Bestellungen]
  @RI integer
AS
SELECT
     [PRJNR]
    ,[NRHW]
    ,[NRGH]
    ,[STATUS]
    ,[KDNRHW]
    ,[KDNRGH]
    ,[LKZ]
    ,[BETREFF]
    ,[TERMIN]
    ,[WAEHR]
    ,[BEARB]
    ,[INFO1]
    ,[INFO2]
    ,[INFO3]
    ,[INFO4]
    ,[LADRNR]
    ,[KzGaeb]
    ,[AnfrArt]
    ,[ABNr]
    ,[VersArt]
    ,[PrBindDatum]
    ,[AbgabeDatum]
    ,[OZMaske]
    ,[nTitel]
    ,[ReplArtNr]
    ,[ReplKtxt1]
    ,[ReplKtxt2]
    ,[ReplVk1M]
    ,[Datum]
    ,[WiederVorl]
    ,[NettoSumme]
    ,[AlsZubehoer]
  FROM [dbo].[ut_Bestellungen]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Bestellungen')
  DROP PROCEDURE [dbo].[up_Del_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Del_Bestellungen]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Bestellungen] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Bestellungen')
  DROP PROCEDURE [dbo].[up_Reorg_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Bestellungen]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Bestellungen] 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_Bestellungen] 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_Bestellungen] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Bestellungen')
  DROP PROCEDURE [dbo].[up_Append_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_Append_Bestellungen]
  @Parm_PRJNR char(8)
 ,@Parm_NRHW char(15)
 ,@Parm_NRGH char(15)
 ,@Parm_STATUS char(2)
 ,@Parm_KDNRHW char(10)
 ,@Parm_KDNRGH char(10)
 ,@Parm_LKZ char(2)
 ,@Parm_BETREFF char(50)
 ,@Parm_TERMIN char(10)
 ,@Parm_WAEHR char(6)
 ,@Parm_BEARB char(40)
 ,@Parm_INFO1 char(40)
 ,@Parm_INFO2 char(40)
 ,@Parm_INFO3 char(40)
 ,@Parm_INFO4 char(40)
 ,@Parm_LADRNR char(15)
 ,@Parm_KzGaeb integer
 ,@Parm_AnfrArt char(1)
 ,@Parm_ABNr char(15)
 ,@Parm_VersArt char(30)
 ,@Parm_PrBindDatum datetime
 ,@Parm_AbgabeDatum datetime
 ,@Parm_OZMaske char(9)
 ,@Parm_nTitel integer
 ,@Parm_ReplArtNr tinyint
 ,@Parm_ReplKtxt1 tinyint
 ,@Parm_ReplKtxt2 tinyint
 ,@Parm_ReplVk1M tinyint
 ,@Parm_Datum datetime
 ,@Parm_WiederVorl datetime
 ,@Parm_NettoSumme float
 ,@Parm_AlsZubehoer tinyint
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Bestellungen])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Bestellungen] (
    RecId
   ,[PRJNR]
   ,[NRHW]
   ,[NRGH]
   ,[STATUS]
   ,[KDNRHW]
   ,[KDNRGH]
   ,[LKZ]
   ,[BETREFF]
   ,[TERMIN]
   ,[WAEHR]
   ,[BEARB]
   ,[INFO1]
   ,[INFO2]
   ,[INFO3]
   ,[INFO4]
   ,[LADRNR]
   ,[KzGaeb]
   ,[AnfrArt]
   ,[ABNr]
   ,[VersArt]
   ,[PrBindDatum]
   ,[AbgabeDatum]
   ,[OZMaske]
   ,[nTitel]
   ,[ReplArtNr]
   ,[ReplKtxt1]
   ,[ReplKtxt2]
   ,[ReplVk1M]
   ,[Datum]
   ,[WiederVorl]
   ,[NettoSumme]
   ,[AlsZubehoer]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_PRJNR)
   ,RTRIM(@Parm_NRHW)
   ,RTRIM(@Parm_NRGH)
   ,RTRIM(@Parm_STATUS)
   ,RTRIM(@Parm_KDNRHW)
   ,RTRIM(@Parm_KDNRGH)
   ,RTRIM(@Parm_LKZ)
   ,RTRIM(@Parm_BETREFF)
   ,RTRIM(@Parm_TERMIN)
   ,RTRIM(@Parm_WAEHR)
   ,RTRIM(@Parm_BEARB)
   ,RTRIM(@Parm_INFO1)
   ,RTRIM(@Parm_INFO2)
   ,RTRIM(@Parm_INFO3)
   ,RTRIM(@Parm_INFO4)
   ,RTRIM(@Parm_LADRNR)
   ,@Parm_KzGaeb
   ,RTRIM(@Parm_AnfrArt)
   ,RTRIM(@Parm_ABNr)
   ,RTRIM(@Parm_VersArt)
   ,@Parm_PrBindDatum
   ,@Parm_AbgabeDatum
   ,RTRIM(@Parm_OZMaske)
   ,@Parm_nTitel
   ,@Parm_ReplArtNr
   ,@Parm_ReplKtxt1
   ,@Parm_ReplKtxt2
   ,@Parm_ReplVk1M
   ,@Parm_Datum
   ,@Parm_WiederVorl
   ,@Parm_NettoSumme
   ,@Parm_AlsZubehoer
       )
  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_Bestellungen')
  DROP PROCEDURE [dbo].[up_TrSel_Bestellungen]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Bestellungen]
AS
  SELECT   RecId,
      LTRIM(RTRIM([PRJNR]))
      +LTRIM(RTRIM([NRHW]))
      +LTRIM(RTRIM([NRGH]))
      +LTRIM(RTRIM([STATUS]))
      +LTRIM(RTRIM([KDNRHW]))
      +LTRIM(RTRIM([KDNRGH]))
      +LTRIM(RTRIM([LKZ]))
      +LTRIM(RTRIM([BETREFF]))
      +LTRIM(RTRIM([TERMIN]))
      +LTRIM(RTRIM([WAEHR]))
      +LTRIM(RTRIM([BEARB]))
      +LTRIM(RTRIM([INFO1]))
      +LTRIM(RTRIM([INFO2]))
      +LTRIM(RTRIM([INFO3]))
      +LTRIM(RTRIM([INFO4]))
      +LTRIM(RTRIM([LADRNR]))
      +LTRIM(RTRIM([AnfrArt]))
      +LTRIM(RTRIM([ABNr]))
      +LTRIM(RTRIM([VersArt]))
      +LTRIM(RTRIM([OZMaske]))
    AS Data
  FROM     [dbo].[ut_Bestellungen]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_KEY0] ON [dbo].[ut_Bestellungen_Keys]
( [KEY0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_First_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_Last_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_Next_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_Next50_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_FindL_Bestellungen_KEY0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellungen_KEY0')
  DROP PROCEDURE [dbo].[up_FindF_Bestellungen_KEY0]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_KEY1] ON [dbo].[ut_Bestellungen_Keys]
( [KEY1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_First_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_Last_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_Next_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_Next50_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_FindL_Bestellungen_KEY1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellungen_KEY1')
  DROP PROCEDURE [dbo].[up_FindF_Bestellungen_KEY1]
GO

CREATE PROCEDURE [dbo].[up_First_Bestellungen_KEY1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys] ORDER BY [KEY1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Bestellungen_KEY1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys] ORDER BY [KEY1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Bestellungen_KEY1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys]
  WHERE [KEY1_Value] > (SELECT [KEY1_Value] FROM [dbo].[ut_Bestellungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Bestellungen_KEY1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys]
  WHERE [KEY1_Value] > (SELECT [KEY1_Value] FROM [dbo].[ut_Bestellungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY1_Value]
GO

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

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

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

CREATE INDEX [Ik_KEY2] ON [dbo].[ut_Bestellungen_Keys]
( [KEY2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_First_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_Last_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_Prior_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_Next_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_Prior50_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_Next50_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_FindL_Bestellungen_KEY2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Bestellungen_KEY2')
  DROP PROCEDURE [dbo].[up_FindF_Bestellungen_KEY2]
GO

CREATE PROCEDURE [dbo].[up_First_Bestellungen_KEY2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys] ORDER BY [KEY2_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Bestellungen_KEY2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys] ORDER BY [KEY2_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Bestellungen_KEY2] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys]
  WHERE [KEY2_Value] > (SELECT [KEY2_Value] FROM [dbo].[ut_Bestellungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY2_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Bestellungen_KEY2] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Bestellungen_Keys]
  WHERE [KEY2_Value] > (SELECT [KEY2_Value] FROM [dbo].[ut_Bestellungen_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [KEY2_Value]
GO

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

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

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

