/***************************************************************************/
/*                                                                         */
/*                      Machines table creation script                     */
/*                                                                         */
/*                             a-cvadai 3/2/95                             */
/*                                                                         */
/***************************************************************************/

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id('dbo.Machines'))
 BEGIN
   PRINT 'Dropping old version of dbo.Machines'
   DROP table dbo.Machines
 END
go

CREATE TABLE Machines
 (
  dwMachineID int NOT NULL,
  ArchitectureKey int NOT NULL
 )
go

INSERT Machines
 select distinct dwMachineID, ArchitectureKey
 from MachineDataTable
go

create unique index Machines_idx on Machines (dwMachineID)
go

IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id('dbo.MachineDataTable_ins'))
 BEGIN
   PRINT 'Dropping old version of dbo.MachineDataTable_ins'
   DROP trigger dbo.MachineDataTable_ins
 END
go

CREATE TRIGGER MachineDataTable_ins ON MachineDataTable FOR insert AS

   if (select distinct m.dwMachineID from Machines m, inserted i where 
         m.dwMachineID = i.dwMachineID) = NULL
       BEGIN
         INSERT Machines (dwMachineID, ArchitectureKey) (select distinct dwMachineID, 
                 ArchitectureKey from inserted)
       END

go
