------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Sollstundenschema')
BEGIN
CREATE TABLE [dbo].[ut_Sollstundenschema]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[bsgruppe] char(2) DEFAULT ' '
  ,[jahr] char(4) DEFAULT ' '
  ,[monat] char(2) DEFAULT ' '
  ,[tag] char(2) DEFAULT ' '
  ,[sstunden] float DEFAULT 0
  CONSTRAINT [Pk_Sollstundenschema]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Sollstundenschema_KeyDel')
  DROP TRIGGER [dbo].[tr_Sollstundenschema_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Sollstundenschema_KeyUpd')
  DROP TRIGGER [dbo].[tr_Sollstundenschema_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Sollstundenschema_Keys')
  DROP TABLE [dbo].[ut_Sollstundenschema_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Sollstundenschema' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] ADD [bsgruppe] 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_Sollstundenschema' and sc.[name] = 'jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] 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_Sollstundenschema' and sc.[name] = 'monat')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] 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_Sollstundenschema' and sc.[name] = 'tag')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] 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_Sollstundenschema' and sc.[name] = 'sstunden')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] ADD [sstunden] float NOT NULL DEFAULT 0
END
GO
IF 2 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Sollstundenschema' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] ALTER COLUMN [bsgruppe] char(2)
END
GO
IF 4 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Sollstundenschema' and sc.[name] = 'jahr')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] 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_Sollstundenschema' and sc.[name] = 'monat')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] 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_Sollstundenschema' and sc.[name] = 'tag')
BEGIN
  ALTER TABLE [dbo].[ut_Sollstundenschema] ALTER COLUMN [tag] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Sollstundenschema')
  DROP VIEW [dbo].[Sollstundenschema]
GO
CREATE VIEW [dbo].[Sollstundenschema] AS
  SELECT * FROM [dbo].[ut_Sollstundenschema]
GO
CREATE TABLE [dbo].[ut_Sollstundenschema_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(20) NOT NULL
  CONSTRAINT [Pk_ut_Sollstundenschema_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Sollstundenschema_KeyDel] ON [dbo].[ut_Sollstundenschema]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Sollstundenschema_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Sollstundenschema')
  DROP PROCEDURE [dbo].[up_Add_Sollstundenschema]
GO
CREATE PROCEDURE [dbo].[up_Add_Sollstundenschema] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Sollstundenschema])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Sollstundenschema] (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_Sollstundenschema')
  DROP PROCEDURE [dbo].[up_Upd_Sollstundenschema]
GO
CREATE PROCEDURE [dbo].[up_Upd_Sollstundenschema]
  @RI integer
 ,@Parm_bsgruppe char(2)
 ,@Parm_jahr char(4)
 ,@Parm_monat char(2)
 ,@Parm_tag char(2)
 ,@Parm_sstunden float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Sollstundenschema] SET
     [bsgruppe] = RTRIM(@Parm_bsgruppe)
    ,[jahr] = RTRIM(@Parm_jahr)
    ,[monat] = RTRIM(@Parm_monat)
    ,[tag] = RTRIM(@Parm_tag)
    ,[sstunden] = @Parm_sstunden
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

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

GO

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

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Sollstundenschema_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_First_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Last_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Next_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Sollstundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Sollstundenschema_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Sollstundenschema_Key0]
GO

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

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

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

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

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

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

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

