------------------------------------------------------------
IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_UstFall')
BEGIN
CREATE TABLE [dbo].[ut_UstFall]
(
   [RecId]  integer NOT NULL
  ,[MandId] integer DEFAULT 1
  ,[UstFall] char(3) DEFAULT ' '
  ,[Bez1] char(50) DEFAULT ' '
  ,[Bez2] char(50) DEFAULT ' '
  CONSTRAINT [Pk_UstFall]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UstFall_KeyDel')
  DROP TRIGGER [dbo].[tr_UstFall_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_UstFall_KeyUpd')
  DROP TRIGGER [dbo].[tr_UstFall_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_UstFall_Keys')
  DROP TABLE [dbo].[ut_UstFall_Keys]
GO

IF 0 = (select count(*) from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFall' and sc.[name] = 'UstFall')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ADD [UstFall] char(3) 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_UstFall' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ADD [Bez1] 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_UstFall' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ADD [Bez2] char(50) NOT NULL DEFAULT ' '
END
GO
IF 3 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFall' and sc.[name] = 'UstFall')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ALTER COLUMN [UstFall] char(3)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFall' and sc.[name] = 'Bez1')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ALTER COLUMN [Bez1] char(50)
END
GO
IF 50 > (select length from syscolumns sc inner join sysobjects so
        on sc.[id] = so.[id] and so.[name] = 'ut_UstFall' and sc.[name] = 'Bez2')
BEGIN
  ALTER TABLE [dbo].[ut_UstFall] ALTER COLUMN [Bez2] char(50)
END
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'UstFall')
  DROP VIEW [dbo].[UstFall]
GO
CREATE VIEW [dbo].[UstFall] AS
  SELECT * FROM [dbo].[ut_UstFall]
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_UstFall')
  DROP PROCEDURE [dbo].[up_Add_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Add_UstFall] AS
  DECLARE @NeueId integer
  SET @NeueId = (SELECT MAX(RecId) from [dbo].[ut_UstFall])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UstFall] (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_UstFall')
  DROP PROCEDURE [dbo].[up_Upd_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Upd_UstFall]
  @RI integer
 ,@Parm_UstFall char(3)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
AS
--BEGIN TRANSACTION
UPDATE [dbo].[ut_UstFall] SET
     [UstFall] = RTRIM(@Parm_UstFall)
    ,[Bez1] = RTRIM(@Parm_Bez1)
    ,[Bez2] = RTRIM(@Parm_Bez2)
  WHERE RecId = @RI
--COMMIT TRANSACTION
GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_UstFall')
  DROP PROCEDURE [dbo].[up_Sel_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Sel_UstFall]
  @RI integer
AS
SELECT
     [UstFall]
    ,[Bez1]
    ,[Bez2]
  FROM [dbo].[ut_UstFall]
  WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_UstFall')
  DROP PROCEDURE [dbo].[up_Del_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Del_UstFall]
  @RI integer
AS
  DELETE FROM [dbo].[ut_UstFall] WHERE RecId = @RI
GO
------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_UstFall')
  DROP PROCEDURE [dbo].[up_Reorg_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Reorg_UstFall]
  @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_UstFall] 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_UstFall] 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_UstFall] )
  END

GO

------------------------------------------------------------
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_UstFall')
  DROP PROCEDURE [dbo].[up_Append_UstFall]
GO
CREATE PROCEDURE [dbo].[up_Append_UstFall]
  @Parm_UstFall char(3)
 ,@Parm_Bez1 char(50)
 ,@Parm_Bez2 char(50)
AS
  DECLARE @NeueId    integer
  SET     @NeueId = (SELECT MAX(RecId) FROM [dbo].[ut_UstFall])
  IF @NeueId IS NULL SET @NeueId = 0
  SET @NeueId = @NeueId + 1
  INSERT INTO [dbo].[ut_UstFall] (
    RecId
   ,[UstFall]
   ,[Bez1]
   ,[Bez2]
     ) VALUES (
    @NeueId
   ,RTRIM(@Parm_UstFall)
   ,RTRIM(@Parm_Bez1)
   ,RTRIM(@Parm_Bez2)
       )
  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_UstFall')
  DROP PROCEDURE [dbo].[up_TrSel_UstFall]
GO
CREATE PROCEDURE [dbo].[up_TrSel_UstFall]
AS
  SELECT   RecId,
      LTRIM(RTRIM([UstFall]))
      +LTRIM(RTRIM([Bez1]))
      +LTRIM(RTRIM([Bez2]))
    AS Data
  FROM     [dbo].[ut_UstFall]
  WHERE    RecId > 0
  ORDER BY RecId
GO

IF 0 = (select count(*)
   from       sysindexes si
   inner join sysobjects so on
   so.[xtype]    = 'U'
   and so.[name] = 'ut_UstFall'
   and so.[id]   = si.[id]
   and si.[name] = 'Ik_Key00')
CREATE INDEX [Ik_Key00] ON [dbo].[ut_UstFall]
(UstFall,RecId)
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_First_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_Last_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_Prior_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_Next_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_Next50_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_FindL_UstFall_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_UstFall_Key00')
  DROP PROCEDURE [dbo].[up_FindF_UstFall_Key00]
GO

------------------------------------------------------------
/* ersten Datensatz aus UstFall in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_First_UstFall_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFall] ORDER BY UstFall,RecId
GO

------------------------------------------------------------
/* letzten Datensatz aus UstFall in Folge Key00 ermittln */
CREATE PROCEDURE [dbo].[up_Last_UstFall_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_UstFall] ORDER BY UstFall DESC,RecId DESC
GO

------------------------------------------------------------
/* naechsten Datensatz in Folge ORDER BY UstFall,RecId ermitteln */
CREATE PROCEDURE [dbo].[up_Next_UstFall_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldUstFall char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT UstFall FROM [dbo].[ut_UstFall] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldUstFall
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
                   WHERE UstFall = @oldUstFall AND RecId > @oldRecId
                   ORDER BY UstFall,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
                   WHERE UstFall > @oldUstFall
                   ORDER BY UstFall,RecId
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* vorherigen Datensatz in Folge ORDER BY UstFall DESC,RecId DESC ermitteln */
CREATE PROCEDURE [dbo].[up_Prior_UstFall_Key00] @oldRecId integer AS
  DECLARE @newRecId integer
  DECLARE @oldUstFall char(3)

  /* alte Daten ermitteln */
  DECLARE curTmp CURSOR FOR SELECT UstFall FROM [dbo].[ut_UstFall] WHERE RecId=@oldRecId
  OPEN    curTmp
  FETCH NEXT FROM curTmp INTO @oldUstFall
  IF (@@FETCH_STATUS <> 0)
  BEGIN
    CLOSE      curTmp
    DEALLOCATE curTmp
    SET @newRecId = 0
    SELECT @newRecId AS RecId
    RETURN
  END
  CLOSE      curTmp
  DEALLOCATE curTmp

  /* Folgesaetze aufsuchen */
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
                   WHERE UstFall = @oldUstFall AND RecId < @oldRecId
                   ORDER BY UstFall DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END
  SET @newRecId = (SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
                   WHERE UstFall < @oldUstFall
                   ORDER BY UstFall DESC,RecId DESC
                  )
  IF (NOT (@newRecId IS NULL))
  BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  /* keine weiteren Datensaetze */
  SET @newRecId = 0
  SELECT @newRecId AS RecId
GO

------------------------------------------------------------
/* ersten Datensatz in Folge ORDER BY UstFall,RecId suchen */
CREATE PROCEDURE [dbo].[up_FindF_UstFall_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
      WHERE SUBSTRING(UstFall,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY UstFall,RecId
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
      WHERE UstFall = SUBSTRING(@xKey,1,3)
      ORDER BY UstFall,RecId
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

------------------------------------------------------------
/* letzten Datensatz in Folge ORDER BY UstFall DESC,RecId DESC suchen */
CREATE PROCEDURE [dbo].[up_FindL_UstFall_Key00] @xKey char(128),@xLen integer AS

  IF @xLen < 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
      WHERE SUBSTRING(UstFall,1,@xLen) = SUBSTRING(@xKey,1,@xLen)
      ORDER BY UstFall DESC,RecId DESC
    RETURN
  END

  IF @xLen = 3
  BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_UstFall]
      WHERE UstFall = SUBSTRING(@xKey,1,3)
      ORDER BY UstFall DESC,RecId DESC
    RETURN
  END

  SELECT CONVERT(integer,0) AS RecId
GO

