------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_WartungsVertraege')
BEGIN
CREATE TABLE [dbo].[ut_WartungsVertraege]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[Ident] integer DEFAULT 0
  ,[Nr] char(20) DEFAULT ' '
  ,[Beginn] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Ende] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[Summe] float DEFAULT 0
  ,[Aktiv] tinyint DEFAULT 0
  ,[KdNr] char(15) DEFAULT ' '
  ,[VertrArt] char(1) DEFAULT ' '
  ,[Artnr] char(20) DEFAULT ' '
  ,[Menge] float DEFAULT 0
  ,[Einheit] char(6) DEFAULT ' '
  ,[VkLohn] float DEFAULT 0
  ,[VkMat] float DEFAULT 0
  ,[PreisJe] float DEFAULT 0
  ,[EP] float DEFAULT 0
  ,[Ktxt] char(250) DEFAULT ' '
  ,[VerlAm] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[VerlUm] integer DEFAULT 0
  ,[VerlUnit] char(1) DEFAULT ' '
  ,[SerienNr] char(50) DEFAULT ' '
  ,[Freigabe] char(50) DEFAULT ' '
  ,[AbrPeriode] integer DEFAULT 0
  ,[AbrUnit] char(1) DEFAULT ' '
  ,[AbgerBis] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[RechnAn] char(15) DEFAULT ' '
  ,[VertrDatum] datetime DEFAULT CONVERT(datetime,'1900-01-01',120)
  ,[PrPeriode] integer DEFAULT 0
  ,[RabattVH] float DEFAULT 0
  CONSTRAINT [Pk_WartungsVertraege]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsVertraege_KeyDel')
  DROP TRIGGER [dbo].[tr_WartungsVertraege_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_WartungsVertraege_KeyUpd')
  DROP TRIGGER [dbo].[tr_WartungsVertraege_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_WartungsVertraege_Keys')
  DROP TABLE [dbo].[ut_WartungsVertraege_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'Ident')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Ident] 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_WartungsVertraege' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Nr] 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_WartungsVertraege' and sc.[name] = 'Beginn')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Beginn] 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_WartungsVertraege' and sc.[name] = 'Ende')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Ende] 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_WartungsVertraege' and sc.[name] = 'Summe')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Summe] 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_WartungsVertraege' and sc.[name] = 'Aktiv')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Aktiv] 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_WartungsVertraege' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [KdNr] 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_WartungsVertraege' and sc.[name] = 'VertrArt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VertrArt] 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_WartungsVertraege' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Artnr] 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_WartungsVertraege' and sc.[name] = 'Menge')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Menge] 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_WartungsVertraege' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Einheit] 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_WartungsVertraege' and sc.[name] = 'VkLohn')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VkLohn] 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_WartungsVertraege' and sc.[name] = 'VkMat')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VkMat] 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_WartungsVertraege' and sc.[name] = 'PreisJe')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [PreisJe] 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_WartungsVertraege' and sc.[name] = 'EP')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [EP] 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_WartungsVertraege' and sc.[name] = 'Ktxt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Ktxt] char(250) 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_WartungsVertraege' and sc.[name] = 'VerlAm')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VerlAm] 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_WartungsVertraege' and sc.[name] = 'VerlUm')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VerlUm] 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_WartungsVertraege' and sc.[name] = 'VerlUnit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VerlUnit] 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_WartungsVertraege' and sc.[name] = 'SerienNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [SerienNr] 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_WartungsVertraege' and sc.[name] = 'Freigabe')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [Freigabe] 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_WartungsVertraege' and sc.[name] = 'AbrPeriode')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [AbrPeriode] 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_WartungsVertraege' and sc.[name] = 'AbrUnit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [AbrUnit] 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_WartungsVertraege' and sc.[name] = 'AbgerBis')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [AbgerBis] 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_WartungsVertraege' and sc.[name] = 'RechnAn')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [RechnAn] 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_WartungsVertraege' and sc.[name] = 'VertrDatum')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [VertrDatum] 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_WartungsVertraege' and sc.[name] = 'PrPeriode')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [PrPeriode] 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_WartungsVertraege' and sc.[name] = 'RabattVH')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ADD [RabattVH] float 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_WartungsVertraege' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [Nr] char(20)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'KdNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [KdNr] char(15)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'VertrArt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [VertrArt] char(1)
END
GO
IF 20 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'Artnr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [Artnr] char(20)
END
GO
IF 6 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'Einheit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [Einheit] char(6)
END
GO
IF 250 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'Ktxt')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [Ktxt] char(250)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'VerlUnit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [VerlUnit] char(1)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'SerienNr')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [SerienNr] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'Freigabe')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [Freigabe] char(50)
END
GO
IF 1 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'AbrUnit')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [AbrUnit] char(1)
END
GO
IF 15 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_WartungsVertraege' and sc.[name] = 'RechnAn')
BEGIN
  ALTER TABLE [dbo].[ut_WartungsVertraege] ALTER COLUMN [RechnAn] char(15)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'WartungsVertraege')
  DROP VIEW [dbo].[WartungsVertraege]
GO
CREATE VIEW [dbo].[WartungsVertraege] AS
  SELECT RecId
    ,[Ident]
    ,[Nr]
    ,[Beginn]
    ,[Ende]
    ,[Summe]
    ,[Aktiv]
    ,[KdNr]
    ,[VertrArt]
    ,[Artnr]
    ,[Menge]
    ,[Einheit]
    ,[VkLohn]
    ,[VkMat]
    ,[PreisJe]
    ,[EP]
    ,[Ktxt]
    ,[VerlAm]
    ,[VerlUm]
    ,[VerlUnit]
    ,[SerienNr]
    ,[Freigabe]
    ,[AbrPeriode]
    ,[AbrUnit]
    ,[AbgerBis]
    ,[RechnAn]
    ,[VertrDatum]
    ,[PrPeriode]
    ,[RabattVH]
  FROM [dbo].[ut_WartungsVertraege]
GO
CREATE TABLE [dbo].[ut_WartungsVertraege_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key00_Value] CHAR(18) NOT NULL
  ,[Key01_Value] CHAR(38) NOT NULL
  ,[Key02_Value] CHAR(33) NOT NULL
  ,[Key03_Value] CHAR(53) NOT NULL
  CONSTRAINT [Pk_ut_WartungsVertraege_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_WartungsVertraege_KeyDel] ON [dbo].[ut_WartungsVertraege]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_WartungsVertraege_Keys] /*WITH (SERIALIZABLE)*/
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

CREATE TRIGGER [dbo].[tr_WartungsVertraege_KeyUpd] ON [dbo].[ut_WartungsVertraege]
FOR INSERT, UPDATE
AS
  SET NOCOUNT ON
  IF UPDATE([RecId]) OR UPDATE([MandId])
  BEGIN
    DELETE FROM [dbo].[ut_WartungsVertraege_Keys] /*WITH (SERIALIZABLE)*/
    WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)

    INSERT INTO [dbo].[ut_WartungsVertraege_Keys] /*WITH (SERIALIZABLE)*/
    ([DataRecId],[DataMandId],[Key00_Value],[Key01_Value],[Key02_Value],[Key03_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(STR([Ident],8,0)+STR(RecId,10))
     ,UPPER(convert(char(20),[Nr])+STR([Ident],8,0)+STR(RecId,10))
     ,UPPER(convert(char(15),[KdNr])+STR([Ident],8,0)+STR(RecId,10))
     ,UPPER(convert(char(15),[KdNr])+convert(char(20),[Nr])+STR([Ident],8,0)+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_WartungsVertraege_Keys] /*WITH (SERIALIZABLE)*/ SET
    [Key00_Value] = UPPER(STR([Ident],8,0)+STR([RecId],10))
   ,[Key01_Value] = UPPER(convert(char(20),[Nr])+STR([Ident],8,0)+STR([RecId],10))
   ,[Key02_Value] = UPPER(convert(char(15),[KdNr])+STR([Ident],8,0)+STR([RecId],10))
   ,[Key03_Value] = UPPER(convert(char(15),[KdNr])+convert(char(20),[Nr])+STR([Ident],8,0)+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF OBJECT_ID (N'itcfunc_NextRecId_WartungsVertraege', N'FN') IS NOT NULL
	DROP FUNCTION dbo.itcfunc_NextRecId_WartungsVertraege
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WartungsVertraege() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WartungsVertraege] WITH (READUNCOMMITTED)),0) + 1
END
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Sel_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Sel_WartungsVertraege]
  @RI integer
AS
SELECT
     [Ident]
    ,[Nr]
    ,[Beginn]
    ,[Ende]
    ,[Summe]
    ,[Aktiv]
    ,[KdNr]
    ,[VertrArt]
    ,[Artnr]
    ,[Menge]
    ,[Einheit]
    ,[VkLohn]
    ,[VkMat]
    ,[PreisJe]
    ,[EP]
    ,[Ktxt]
    ,[VerlAm]
    ,[VerlUm]
    ,[VerlUnit]
    ,[SerienNr]
    ,[Freigabe]
    ,[AbrPeriode]
    ,[AbrUnit]
    ,[AbgerBis]
    ,[RechnAn]
    ,[VertrDatum]
    ,[PrPeriode]
    ,[RabattVH]
  FROM [dbo].[ut_WartungsVertraege]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Sel2_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Sel2_WartungsVertraege]
  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[WartungsVertraege]
  WHERE RecId = @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'up_Append_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Append_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Append_WartungsVertraege]
  @Parm_Ident integer
 ,@Parm_Nr char(20)
 ,@Parm_Beginn datetime
 ,@Parm_Ende datetime
 ,@Parm_Summe float
 ,@Parm_Aktiv tinyint
 ,@Parm_KdNr char(15)
 ,@Parm_VertrArt char(1)
 ,@Parm_Artnr char(20)
 ,@Parm_Menge float
 ,@Parm_Einheit char(6)
 ,@Parm_VkLohn float
 ,@Parm_VkMat float
 ,@Parm_PreisJe float
 ,@Parm_EP float
 ,@Parm_Ktxt char(250)
 ,@Parm_VerlAm datetime
 ,@Parm_VerlUm integer
 ,@Parm_VerlUnit char(1)
 ,@Parm_SerienNr char(50)
 ,@Parm_Freigabe char(50)
 ,@Parm_AbrPeriode integer
 ,@Parm_AbrUnit char(1)
 ,@Parm_AbgerBis datetime
 ,@Parm_RechnAn char(15)
 ,@Parm_VertrDatum datetime
 ,@Parm_PrPeriode integer
 ,@Parm_RabattVH float
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_WartungsVertraege()
      INSERT INTO [dbo].[ut_WartungsVertraege] (RecId
       ,[Ident]
       ,[Nr]
       ,[Beginn]
       ,[Ende]
       ,[Summe]
       ,[Aktiv]
       ,[KdNr]
       ,[VertrArt]
       ,[Artnr]
       ,[Menge]
       ,[Einheit]
       ,[VkLohn]
       ,[VkMat]
       ,[PreisJe]
       ,[EP]
       ,[Ktxt]
       ,[VerlAm]
       ,[VerlUm]
       ,[VerlUnit]
       ,[SerienNr]
       ,[Freigabe]
       ,[AbrPeriode]
       ,[AbrUnit]
       ,[AbgerBis]
       ,[RechnAn]
       ,[VertrDatum]
       ,[PrPeriode]
       ,[RabattVH]
         ) VALUES (@NeueId
       ,@Parm_Ident
       ,RTRIM(@Parm_Nr)
       ,@Parm_Beginn
       ,@Parm_Ende
       ,@Parm_Summe
       ,@Parm_Aktiv
       ,RTRIM(@Parm_KdNr)
       ,RTRIM(@Parm_VertrArt)
       ,RTRIM(@Parm_Artnr)
       ,@Parm_Menge
       ,RTRIM(@Parm_Einheit)
       ,@Parm_VkLohn
       ,@Parm_VkMat
       ,@Parm_PreisJe
       ,@Parm_EP
       ,RTRIM(@Parm_Ktxt)
       ,@Parm_VerlAm
       ,@Parm_VerlUm
       ,RTRIM(@Parm_VerlUnit)
       ,RTRIM(@Parm_SerienNr)
       ,RTRIM(@Parm_Freigabe)
       ,@Parm_AbrPeriode
       ,RTRIM(@Parm_AbrUnit)
       ,@Parm_AbgerBis
       ,RTRIM(@Parm_RechnAn)
       ,@Parm_VertrDatum
       ,@Parm_PrPeriode
       ,@Parm_RabattVH
           )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END
  SELECT @NeueId AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Upd_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Upd_WartungsVertraege]
  @RI integer
 ,@Parm_Ident integer
 ,@Parm_Nr char(20)
 ,@Parm_Beginn datetime
 ,@Parm_Ende datetime
 ,@Parm_Summe float
 ,@Parm_Aktiv tinyint
 ,@Parm_KdNr char(15)
 ,@Parm_VertrArt char(1)
 ,@Parm_Artnr char(20)
 ,@Parm_Menge float
 ,@Parm_Einheit char(6)
 ,@Parm_VkLohn float
 ,@Parm_VkMat float
 ,@Parm_PreisJe float
 ,@Parm_EP float
 ,@Parm_Ktxt char(250)
 ,@Parm_VerlAm datetime
 ,@Parm_VerlUm integer
 ,@Parm_VerlUnit char(1)
 ,@Parm_SerienNr char(50)
 ,@Parm_Freigabe char(50)
 ,@Parm_AbrPeriode integer
 ,@Parm_AbrUnit char(1)
 ,@Parm_AbgerBis datetime
 ,@Parm_RechnAn char(15)
 ,@Parm_VertrDatum datetime
 ,@Parm_PrPeriode integer
 ,@Parm_RabattVH float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_WartungsVertraege] SET
     [Ident] = @Parm_Ident
    ,[Nr] = RTRIM(@Parm_Nr)
    ,[Beginn] = @Parm_Beginn
    ,[Ende] = @Parm_Ende
    ,[Summe] = @Parm_Summe
    ,[Aktiv] = @Parm_Aktiv
    ,[KdNr] = RTRIM(@Parm_KdNr)
    ,[VertrArt] = RTRIM(@Parm_VertrArt)
    ,[Artnr] = RTRIM(@Parm_Artnr)
    ,[Menge] = @Parm_Menge
    ,[Einheit] = RTRIM(@Parm_Einheit)
    ,[VkLohn] = @Parm_VkLohn
    ,[VkMat] = @Parm_VkMat
    ,[PreisJe] = @Parm_PreisJe
    ,[EP] = @Parm_EP
    ,[Ktxt] = RTRIM(@Parm_Ktxt)
    ,[VerlAm] = @Parm_VerlAm
    ,[VerlUm] = @Parm_VerlUm
    ,[VerlUnit] = RTRIM(@Parm_VerlUnit)
    ,[SerienNr] = RTRIM(@Parm_SerienNr)
    ,[Freigabe] = RTRIM(@Parm_Freigabe)
    ,[AbrPeriode] = @Parm_AbrPeriode
    ,[AbrUnit] = RTRIM(@Parm_AbrUnit)
    ,[AbgerBis] = @Parm_AbgerBis
    ,[RechnAn] = RTRIM(@Parm_RechnAn)
    ,[VertrDatum] = @Parm_VertrDatum
    ,[PrPeriode] = @Parm_PrPeriode
    ,[RabattVH] = @Parm_RabattVH
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Upd2_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Upd2_WartungsVertraege]
  @Param_RecId varchar(20)
 ,@Param_Ident varchar(20)
 ,@Param_Nr varchar(20)
 ,@Param_Beginn varchar(10)
 ,@Param_Ende varchar(10)
 ,@Param_Summe varchar(20)
 ,@Param_Aktiv varchar(10)
 ,@Param_KdNr varchar(15)
 ,@Param_VertrArt varchar(1)
 ,@Param_Artnr varchar(20)
 ,@Param_Menge varchar(20)
 ,@Param_Einheit varchar(6)
 ,@Param_VkLohn varchar(20)
 ,@Param_VkMat varchar(20)
 ,@Param_PreisJe varchar(20)
 ,@Param_EP varchar(20)
 ,@Param_Ktxt varchar(250)
 ,@Param_VerlAm varchar(10)
 ,@Param_VerlUm varchar(20)
 ,@Param_VerlUnit varchar(1)
 ,@Param_SerienNr varchar(50)
 ,@Param_Freigabe varchar(50)
 ,@Param_AbrPeriode varchar(20)
 ,@Param_AbrUnit varchar(1)
 ,@Param_AbgerBis varchar(10)
 ,@Param_RechnAn varchar(15)
 ,@Param_VertrDatum varchar(10)
 ,@Param_PrPeriode varchar(20)
 ,@Param_RabattVH varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)

  UPDATE [dbo].[ut_WartungsVertraege] SET
     [Ident] = dbo.itcfunc_float(@Param_Ident)
    ,[Nr] = UPPER(RTRIM(@Param_Nr))
    ,[Beginn] = dbo.itcfunc_datetime(@Param_Beginn)
    ,[Ende] = dbo.itcfunc_datetime(@Param_Ende)
    ,[Summe] = dbo.itcfunc_float(@Param_Summe)
    ,[Aktiv] = dbo.itcfunc_integer(@Param_Aktiv)
    ,[KdNr] = UPPER(RTRIM(@Param_KdNr))
    ,[VertrArt] = UPPER(RTRIM(@Param_VertrArt))
    ,[Artnr] = UPPER(RTRIM(@Param_Artnr))
    ,[Menge] = dbo.itcfunc_float(@Param_Menge)
    ,[Einheit] = RTRIM(@Param_Einheit)
    ,[VkLohn] = dbo.itcfunc_float(@Param_VkLohn)
    ,[VkMat] = dbo.itcfunc_float(@Param_VkMat)
    ,[PreisJe] = dbo.itcfunc_float(@Param_PreisJe)
    ,[EP] = dbo.itcfunc_float(@Param_EP)
    ,[Ktxt] = RTRIM(@Param_Ktxt)
    ,[VerlAm] = dbo.itcfunc_datetime(@Param_VerlAm)
    ,[VerlUm] = dbo.itcfunc_float(@Param_VerlUm)
    ,[VerlUnit] = UPPER(RTRIM(@Param_VerlUnit))
    ,[SerienNr] = RTRIM(@Param_SerienNr)
    ,[Freigabe] = RTRIM(@Param_Freigabe)
    ,[AbrPeriode] = dbo.itcfunc_float(@Param_AbrPeriode)
    ,[AbrUnit] = UPPER(RTRIM(@Param_AbrUnit))
    ,[AbgerBis] = dbo.itcfunc_datetime(@Param_AbgerBis)
    ,[RechnAn] = UPPER(RTRIM(@Param_RechnAn))
    ,[VertrDatum] = dbo.itcfunc_datetime(@Param_VertrDatum)
    ,[PrPeriode] = dbo.itcfunc_float(@Param_PrPeriode)
    ,[RabattVH] = dbo.itcfunc_float(@Param_RabattVH)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_WartungsVertraege] @mRI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_TrSel_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_TrSel_WartungsVertraege]
AS
  SELECT   RecId,
      LTRIM(RTRIM([Nr]))
      +LTRIM(RTRIM([KdNr]))
      +LTRIM(RTRIM([VertrArt]))
      +LTRIM(RTRIM([Artnr]))
      +LTRIM(RTRIM([Einheit]))
      +LTRIM(RTRIM([Ktxt]))
      +LTRIM(RTRIM([VerlUnit]))
      +LTRIM(RTRIM([SerienNr]))
      +LTRIM(RTRIM([Freigabe]))
      +LTRIM(RTRIM([AbrUnit]))
      +LTRIM(RTRIM([RechnAn]))
    AS Data
  FROM     [dbo].[ut_WartungsVertraege]
  WHERE    RecId > 0
  ORDER BY RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Add_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Add_WartungsVertraege] AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_WartungsVertraege()
  INSERT INTO [dbo].[ut_WartungsVertraege] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Del_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Del_WartungsVertraege]
  @RI integer
AS
  DELETE FROM [dbo].[ut_WartungsVertraege] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_WartungsVertraege')
  DROP PROCEDURE [dbo].[up_Reorg_WartungsVertraege]
GO
CREATE PROCEDURE [dbo].[up_Reorg_WartungsVertraege]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_WartungsVertraege] 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_WartungsVertraege] 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_WartungsVertraege] )
  END
GO


CREATE INDEX [Ik_Key00] ON [dbo].[ut_WartungsVertraege_Keys]
( [Key00_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_First_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_Last_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_Prior_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_Next_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_FindL_WartungsVertraege_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsVertraege_Key00')
  DROP PROCEDURE [dbo].[up_FindF_WartungsVertraege_Key00]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key01] ON [dbo].[ut_WartungsVertraege_Keys]
( [Key01_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_First_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_Last_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_Prior_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_Next_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_FindL_WartungsVertraege_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsVertraege_Key01')
  DROP PROCEDURE [dbo].[up_FindF_WartungsVertraege_Key01]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key02] ON [dbo].[ut_WartungsVertraege_Keys]
( [Key02_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_First_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_Last_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_Prior_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_Next_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_FindL_WartungsVertraege_Key02]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsVertraege_Key02')
  DROP PROCEDURE [dbo].[up_FindF_WartungsVertraege_Key02]
GO

CREATE PROCEDURE [dbo].[up_First_WartungsVertraege_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys] ORDER BY [Key02_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WartungsVertraege_Key02]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys] ORDER BY [Key02_Value] DESC
GO

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

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

CREATE PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key02] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys]
  WHERE [Key02_Value] > (SELECT [Key02_Value] FROM [dbo].[ut_WartungsVertraege_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key02_Value]
GO

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

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

CREATE INDEX [Ik_Key03] ON [dbo].[ut_WartungsVertraege_Keys]
( [Key03_Value],[DataRecId] )
GO
IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_WartungsVertraege'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key03')
CREATE INDEX [Ik_Key03] ON [dbo].[ut_WartungsVertraege]
(KdNr,Nr,Ident,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_First_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_Last_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_Prior_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_Next_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_Prior50_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_FindL_WartungsVertraege_Key03]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_WartungsVertraege_Key03')
  DROP PROCEDURE [dbo].[up_FindF_WartungsVertraege_Key03]
GO

CREATE PROCEDURE [dbo].[up_First_WartungsVertraege_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys] ORDER BY [Key03_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_WartungsVertraege_Key03]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys] ORDER BY [Key03_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_WartungsVertraege_Key03] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_WartungsVertraege_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_WartungsVertraege_Key03] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_WartungsVertraege_Keys]
  WHERE [Key03_Value] > (SELECT [Key03_Value] FROM [dbo].[ut_WartungsVertraege_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key03_Value]
GO

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

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

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

