------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Stundenschema')
BEGIN
CREATE TABLE [dbo].[ut_Stundenschema]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[bsgruppe] char(2) DEFAULT ' '
  ,[tag1] float DEFAULT 0
  ,[tag2] float DEFAULT 0
  ,[tag3] float DEFAULT 0
  ,[tag4] float DEFAULT 0
  ,[tag5] float DEFAULT 0
  ,[tag6] float DEFAULT 0
  ,[tag7] float DEFAULT 0
  ,[tag8] float DEFAULT 0
  ,[tag9] float DEFAULT 0
  ,[tag10] float DEFAULT 0
  ,[tag11] float DEFAULT 0
  ,[tag12] float DEFAULT 0
  ,[tag13] float DEFAULT 0
  ,[tag14] float DEFAULT 0
  CONSTRAINT [Pk_Stundenschema]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Stundenschema_KeyDel')
  DROP TRIGGER [dbo].[tr_Stundenschema_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Stundenschema_KeyUpd')
  DROP TRIGGER [dbo].[tr_Stundenschema_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Stundenschema_Keys')
  DROP TABLE [dbo].[ut_Stundenschema_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_Stundenschema' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] 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_Stundenschema' and sc.[name] = 'tag1')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag1] 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_Stundenschema' and sc.[name] = 'tag2')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag2] 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_Stundenschema' and sc.[name] = 'tag3')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag3] 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_Stundenschema' and sc.[name] = 'tag4')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag4] 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_Stundenschema' and sc.[name] = 'tag5')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag5] 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_Stundenschema' and sc.[name] = 'tag6')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag6] 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_Stundenschema' and sc.[name] = 'tag7')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag7] 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_Stundenschema' and sc.[name] = 'tag8')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag8] 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_Stundenschema' and sc.[name] = 'tag9')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag9] 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_Stundenschema' and sc.[name] = 'tag10')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag10] 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_Stundenschema' and sc.[name] = 'tag11')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag11] 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_Stundenschema' and sc.[name] = 'tag12')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag12] 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_Stundenschema' and sc.[name] = 'tag13')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag13] 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_Stundenschema' and sc.[name] = 'tag14')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ADD [tag14] 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_Stundenschema' and sc.[name] = 'bsgruppe')
BEGIN
  ALTER TABLE [dbo].[ut_Stundenschema] ALTER COLUMN [bsgruppe] char(2)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Stundenschema')
  DROP VIEW [dbo].[Stundenschema]
GO
CREATE VIEW [dbo].[Stundenschema] AS
  SELECT * FROM [dbo].[ut_Stundenschema]
GO
CREATE TABLE [dbo].[ut_Stundenschema_Keys]
(
   [DataRecId]  integer NOT NULL
  ,[DataMandId] integer DEFAULT 1
  ,[Key0_Value] CHAR(12) NOT NULL
  CONSTRAINT [Pk_ut_Stundenschema_Keys]  PRIMARY KEY NONCLUSTERED ( [DataRecId] )
)
GO
CREATE TRIGGER [dbo].[tr_Stundenschema_KeyDel] ON [dbo].[ut_Stundenschema]
FOR DELETE
AS
  SET NOCOUNT ON
  DELETE FROM [dbo].[ut_Stundenschema_Keys] WITH (SERIALIZABLE)
  WHERE [DataRecId] IN (SELECT [RecId] FROM DELETED)
GO

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

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

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

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


------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Stundenschema')
  DROP PROCEDURE [dbo].[up_Add_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Add_Stundenschema] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_Stundenschema])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Stundenschema] (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_Stundenschema')
  DROP PROCEDURE [dbo].[up_Upd_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Upd_Stundenschema]
  @RI integer
 ,@Parm_bsgruppe char(2)
 ,@Parm_tag1 float
 ,@Parm_tag2 float
 ,@Parm_tag3 float
 ,@Parm_tag4 float
 ,@Parm_tag5 float
 ,@Parm_tag6 float
 ,@Parm_tag7 float
 ,@Parm_tag8 float
 ,@Parm_tag9 float
 ,@Parm_tag10 float
 ,@Parm_tag11 float
 ,@Parm_tag12 float
 ,@Parm_tag13 float
 ,@Parm_tag14 float
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_Stundenschema] SET
     [bsgruppe] = RTRIM(@Parm_bsgruppe)
    ,[tag1] = @Parm_tag1
    ,[tag2] = @Parm_tag2
    ,[tag3] = @Parm_tag3
    ,[tag4] = @Parm_tag4
    ,[tag5] = @Parm_tag5
    ,[tag6] = @Parm_tag6
    ,[tag7] = @Parm_tag7
    ,[tag8] = @Parm_tag8
    ,[tag9] = @Parm_tag9
    ,[tag10] = @Parm_tag10
    ,[tag11] = @Parm_tag11
    ,[tag12] = @Parm_tag12
    ,[tag13] = @Parm_tag13
    ,[tag14] = @Parm_tag14
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Stundenschema')
  DROP PROCEDURE [dbo].[up_Sel_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Sel_Stundenschema]
  @RI integer
AS
SELECT
     [bsgruppe]
    ,[tag1]
    ,[tag2]
    ,[tag3]
    ,[tag4]
    ,[tag5]
    ,[tag6]
    ,[tag7]
    ,[tag8]
    ,[tag9]
    ,[tag10]
    ,[tag11]
    ,[tag12]
    ,[tag13]
    ,[tag14]
  FROM [dbo].[ut_Stundenschema]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Stundenschema')
  DROP PROCEDURE [dbo].[up_Del_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Del_Stundenschema]
  @RI integer
AS
  DELETE FROM [dbo].[ut_Stundenschema] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Stundenschema')
  DROP PROCEDURE [dbo].[up_Reorg_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Stundenschema]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Stundenschema] 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_Stundenschema] 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_Stundenschema] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Stundenschema')
  DROP PROCEDURE [dbo].[up_Append_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_Append_Stundenschema]
  @Parm_bsgruppe char(2)
 ,@Parm_tag1 float
 ,@Parm_tag2 float
 ,@Parm_tag3 float
 ,@Parm_tag4 float
 ,@Parm_tag5 float
 ,@Parm_tag6 float
 ,@Parm_tag7 float
 ,@Parm_tag8 float
 ,@Parm_tag9 float
 ,@Parm_tag10 float
 ,@Parm_tag11 float
 ,@Parm_tag12 float
 ,@Parm_tag13 float
 ,@Parm_tag14 float
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_Stundenschema])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_Stundenschema] (
    RecId
   ,[bsgruppe]
   ,[tag1]
   ,[tag2]
   ,[tag3]
   ,[tag4]
   ,[tag5]
   ,[tag6]
   ,[tag7]
   ,[tag8]
   ,[tag9]
   ,[tag10]
   ,[tag11]
   ,[tag12]
   ,[tag13]
   ,[tag14]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_bsgruppe)
   ,@Parm_tag1
   ,@Parm_tag2
   ,@Parm_tag3
   ,@Parm_tag4
   ,@Parm_tag5
   ,@Parm_tag6
   ,@Parm_tag7
   ,@Parm_tag8
   ,@Parm_tag9
   ,@Parm_tag10
   ,@Parm_tag11
   ,@Parm_tag12
   ,@Parm_tag13
   ,@Parm_tag14
       )
  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_Stundenschema')
  DROP PROCEDURE [dbo].[up_TrSel_Stundenschema]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Stundenschema]
AS
  SELECT   RecId,
      LTRIM(RTRIM([bsgruppe]))
    AS Data
  FROM     [dbo].[ut_Stundenschema]
  WHERE    RecId > 0
  ORDER BY RecId
GO

CREATE INDEX [Ik_Key0] ON [dbo].[ut_Stundenschema_Keys]
( [Key0_Value],[DataRecId] )
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_First_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Last_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Next_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Stundenschema_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Stundenschema_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Stundenschema_Key0]
GO

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

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

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

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

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

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

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

