IF EXISTS (SELECT Name FROM SysIndexes  WITH (NOLOCK) WHERE Name = 'IX_ArrangementLines_PacketItemTime')
BEGIN
  DROP INDEX [IX_ArrangementLines_PacketItemTime] ON [dbo].[ArrangementLines]
END
GO

IF NOT EXISTS (SELECT Name FROM SysIndexes  WITH (NOLOCK) WHERE Name = 'IX_ArrangementLines_PacketItemTime2')
BEGIN
  CREATE NONCLUSTERED INDEX [IX_ArrangementLines_PacketItemTime2] ON [dbo].[ArrangementLines]
  (
	[PacketItemTime] ASC,
	[PacketItemNumber] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
END
GO

DECLARE @PacketItemNumber INT
DECLARE @Time CHAR(5)
DECLARE @cnt INT
DECLARE @Systemdate CHAR(8)

SELECT @Systemdate = SYSTEMDATE FROM SYSTEM (NOLOCK)

SELECT @cnt = COUNT(*) FROM ArrangementLines WITH (NOLOCK) 
INNER JOIN PacketItem_NEW WITH (NOLOCK) ON Arrangementlines.PacketItemNumber = PacketItem_NEW.PacketItemNumber
WHERE ISNULL(Arrangementlines.PacketItemNumber, 0) > 0 
  AND ISNULL(Arrangementlines.PacketItemTime, '') = ''
  AND Arrangementlines.Date >= @Systemdate

IF (@cnt > 0)
BEGIN
  IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.ArrangementLineIllegalWare') AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
    DISABLE TRIGGER ArrangementLineIllegalWare ON dbo.ArrangementLines

  DECLARE TmpPKCursor CURSOR FOR
    SELECT PacketItemNumber, Time FROM PacketItem_NEW (NOLOCK)
  OPEN TmpPKCursor
  FETCH NEXT FROM TmpPKCursor INTO @PacketItemNumber, @Time
  WHILE (@@FETCH_STATUS <> -1)
  BEGIN
    UPDATE ArrangementLines
    SET PacketItemTime = @Time
    WHERE PacketItemNumber = @PacketItemNumber
      AND ISNULL(PacketItemTime, '') = ''
	  AND Date >= @Systemdate
    FETCH NEXT FROM TmpPKCursor INTO @PacketItemNumber, @Time
  END;

  IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.ArrangementLineIllegalWare') AND OBJECTPROPERTY(id, N'IsTrigger') = 1)
    ENABLE TRIGGER ArrangementLineIllegalWare ON dbo.ArrangementLines

  CLOSE TmpPKCursor
  DEALLOCATE TmpPKCursor
END
