------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Feiertage')
BEGIN
CREATE TABLE [dbo].[ut_Feiertage]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[jahr] char(4) DEFAULT ' '
  ,[monat] char(2) DEFAULT ' '
  ,[tag] char(2) DEFAULT ' '
  ,[text] char(50) DEFAULT ' '
  CONSTRAINT [Pk_Feiertage]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Feiertage_KeyDel')
  DROP TRIGGER [dbo].[tr_Feiertage_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Feiertage_KeyUpd')
  DROP TRIGGER [dbo].[tr_Feiertage_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Feiertage_Keys')
  DROP TABLE [dbo].[ut_Feiertage_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Feiertage' and sc.[name] = 'jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ADD [jahr] char(4) 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_Feiertage' and sc.[name] = 'monat')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ADD [monat] 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_Feiertage' and sc.[name] = 'tag')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ADD [tag] 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_Feiertage' and sc.[name] = 'text')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ADD [text] char(50) NOT NULL DEFAULT ' '
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Feiertage' and sc.[name] = 'jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ALTER COLUMN [jahr] char(4)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Feiertage' and sc.[name] = 'monat')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ALTER COLUMN [monat] char(2)
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Feiertage' and sc.[name] = 'tag')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ALTER COLUMN [tag] char(2)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Feiertage' and sc.[name] = 'text')
BEGIN
  ALTER TABLE [dbo].[ut_Feiertage] ALTER COLUMN [text] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Feiertage')
  DROP VIEW [dbo].[Feiertage]
GO
CREATE VIEW [dbo].[Feiertage] AS
  SELECT * FROM [dbo].[ut_Feiertage]
GO
CREATE TABLE [dbo].[ut_Feiertage_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(16) NOT NULL
  ,[Key1_Value] CHAR(18) NOT NULL
  ,[Key2_Value] CHAR(14) NOT NULL
  CONSTRAINT [Pk_ut_Feiertage_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Feiertage_KeyDel] ON [dbo].[ut_Feiertage]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Feiertage_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

    INSERT INTO [dbo].[ut_Feiertage_Keys] WITH (SERIALIZABLE)
    ([DataRecId],[DataMandId],[Key0_Value],[Key1_Value],[Key2_Value])
    (SELECT [RecId],[MandId]
     ,UPPER(convert(char(4),[jahr])+convert(char(2),[monat])+STR(RecId,10))
     ,UPPER(convert(char(4),[jahr])+convert(char(2),[monat])+convert(char(2),[tag])+STR(RecId,10))
     ,UPPER(convert(char(2),[monat])+convert(char(2),[tag])+STR(RecId,10))

    FROM INSERTED)
  END
  ELSE
  BEGIN
    UPDATE [dbo].[ut_Feiertage_Keys] WITH (SERIALIZABLE) SET
    [Key0_Value] = UPPER(convert(char(4),[jahr])+convert(char(2),[monat])+STR([RecId],10))
   ,[Key1_Value] = UPPER(convert(char(4),[jahr])+convert(char(2),[monat])+convert(char(2),[tag])+STR([RecId],10))
   ,[Key2_Value] = UPPER(convert(char(2),[monat])+convert(char(2),[tag])+STR([RecId],10))
    FROM INSERTED WHERE [DataRecId] = [RecId]
  END
GO

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Feiertage')
  DROP PROCEDURE [dbo].[up_Add_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Add_Feiertage] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Feiertage])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Feiertage] (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_Feiertage')
  DROP PROCEDURE [dbo].[up_Upd_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Upd_Feiertage]
  @RI integer
 ,@Parm_jahr char(4)
 ,@Parm_monat char(2)
 ,@Parm_tag char(2)
 ,@Parm_text char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Feiertage] SET
     [jahr] = RTRIM(@Parm_jahr)
    ,[monat] = RTRIM(@Parm_monat)
    ,[tag] = RTRIM(@Parm_tag)
    ,[text] = RTRIM(@Parm_text)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Feiertage')
  DROP PROCEDURE [dbo].[up_Sel_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Sel_Feiertage]
  @RI integer
AS
SELECT
     [jahr]
    ,[monat]
    ,[tag]
    ,[text]
  FROM [dbo].[ut_Feiertage]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Feiertage')
  DROP PROCEDURE [dbo].[up_Del_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Del_Feiertage]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Feiertage] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Feiertage')
  DROP PROCEDURE [dbo].[up_Reorg_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Feiertage]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Feiertage] 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_Feiertage] 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_Feiertage] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Feiertage')
  DROP PROCEDURE [dbo].[up_Append_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_Append_Feiertage]
  @Parm_jahr char(4)
 ,@Parm_monat char(2)
 ,@Parm_tag char(2)
 ,@Parm_text char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Feiertage])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Feiertage] (
    RecId
   ,[jahr]
   ,[monat]
   ,[tag]
   ,[text]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_jahr)
   ,RTRIM(@Parm_monat)
   ,RTRIM(@Parm_tag)
   ,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_Feiertage')
  DROP PROCEDURE [dbo].[up_TrSel_Feiertage]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Feiertage]
AS
  SELECT   RecId,
      LTRIM(RTRIM([jahr]))
      +LTRIM(RTRIM([monat]))
      +LTRIM(RTRIM([tag]))
      +LTRIM(RTRIM([text]))
    AS Data
  FROM     [dbo].[ut_Feiertage]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Feiertage_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_First_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_Last_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_Next_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Feiertage_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Feiertage_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Feiertage_Key0]
GO

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

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

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

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

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

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

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

CREATE INDEX [Ik_Key1] ON [dbo].[ut_Feiertage_Keys]
( [Key1_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_First_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_Last_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_Prior_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_Next_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_Prior50_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_Next50_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_FindL_Feiertage_Key1]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Feiertage_Key1')
  DROP PROCEDURE [dbo].[up_FindF_Feiertage_Key1]
GO

CREATE PROCEDURE [dbo].[up_First_Feiertage_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys] ORDER BY [Key1_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Feiertage_Key1]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys] ORDER BY [Key1_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Feiertage_Key1] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Feiertage_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Feiertage_Key1] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Feiertage_Keys]
  WHERE [Key1_Value] > (SELECT [Key1_Value] FROM [dbo].[ut_Feiertage_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key1_Value]
GO

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

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

CREATE INDEX [Ik_Key2] ON [dbo].[ut_Feiertage_Keys]
( [Key2_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_First_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_Last_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_Prior_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_Next_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_Prior50_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_Next50_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_FindL_Feiertage_Key2]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Feiertage_Key2')
  DROP PROCEDURE [dbo].[up_FindF_Feiertage_Key2]
GO

CREATE PROCEDURE [dbo].[up_First_Feiertage_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys] ORDER BY [Key2_Value]
GO

CREATE PROCEDURE [dbo].[up_Last_Feiertage_Key2]
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys] ORDER BY [Key2_Value] DESC
GO

CREATE PROCEDURE [dbo].[up_Next_Feiertage_Key2] @mRecNo integer
AS
  SELECT TOP 1 [DataRecId] FROM [dbo].[ut_Feiertage_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_Feiertage_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

CREATE PROCEDURE [dbo].[up_Next50_Feiertage_Key2] @mRecNo integer
AS
  SELECT TOP 50 [DataRecId] FROM [dbo].[ut_Feiertage_Keys]
  WHERE [Key2_Value] > (SELECT [Key2_Value] FROM [dbo].[ut_Feiertage_Keys] WHERE [DataRecId] = @mRecNo)
  ORDER BY [Key2_Value]
GO

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

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

