USE [AIS_S3]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM AttributeValues WHERE Value = 'Allow Multiple Vend')
BEGIN
DECLARE @AttributeNo int
DECLARE @MachineNo int
DECLARE @AttributeValueNo int
select @AttributeNo = AttributeNo from Attributes where Name = 'Machine Setting'
INSERT INTO AttributeValues (AttributeNo, Value, ValueType, ParentAttributeValueNo, ParentEligible, LastModified) VALUES 
(@AttributeNo, 'Allow Multiple Vend', 'bool', NULL, 'False', GETDATE());
select @AttributeValueNo = AttributeValueNo from AttributeValues where Value = 'Allow Multiple Vend'

-- Add default value (False) for Allow Multiple Vend for all the vending machines
Declare c Cursor For Select MachineNo From Machines
Open c
Fetch next From c into @MachineNo
While @@Fetch_Status=0 Begin
   INSERT INTO MachineSettings (MachineNo, AttributeValueNo, Value, LastModified) VALUES 
(@MachineNo, @AttributeValueNo, 'False', GETDATE());
   Fetch next From c into @MachineNo
End
Close c
Deallocate c

-- Debug
-- select * from MachineSettings where AttributeValueNo = @AttributeValueNo

END
GO