USE [AIS_S3]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM AttributeValues WHERE Value = 'Remove first X digits - Card')
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, 'Remove first X digits - Card', 'string', NULL, 'False', GETDATE());
select @AttributeValueNo = AttributeValueNo from AttributeValues where Value = 'Remove first X digits - Card'

-- Add default value 0 for Remove first X digits - Card 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, '0', GETDATE());
   Fetch next From c into @MachineNo
End
Close c
Deallocate c
-- Debug
-- select * from MachineSettings where AttributeValueNo = @AttributeValueNo

END
GO

IF NOT EXISTS (SELECT * FROM AttributeValues WHERE Value = 'Remove last X digits - Card')
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, 'Remove last X digits - Card', 'string', NULL, 'False', GETDATE());
select @AttributeValueNo = AttributeValueNo from AttributeValues where Value = 'Remove last X digits - Card'

-- Add default value 0 for Remove last X digits - Card 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, '0', GETDATE());
   Fetch next From c into @MachineNo
End
Close c
Deallocate c
-- Debug
-- select * from MachineSettings where AttributeValueNo = @AttributeValueNo

END
GO


IF NOT EXISTS (SELECT * FROM AttributeValues WHERE Value = 'Remove first X digits - Barcode')
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, 'Remove first X digits - Barcode', 'string', NULL, 'False', GETDATE());
select @AttributeValueNo = AttributeValueNo from AttributeValues where Value = 'Remove first X digits - Barcode'

-- Add default value 0 for Remove first X digits - Barcode 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, '0', GETDATE());
   Fetch next From c into @MachineNo
End
Close c
Deallocate c
-- Debug
-- select * from MachineSettings where AttributeValueNo = @AttributeValueNo

END
GO



IF NOT EXISTS (SELECT * FROM AttributeValues WHERE Value = 'Remove last X digits - Barcode')
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, 'Remove last X digits - Barcode', 'string', NULL, 'False', GETDATE());
select @AttributeValueNo = AttributeValueNo from AttributeValues where Value = 'Remove last X digits - Barcode'

-- Add default value 0 for Remove last X digits - Barcode 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, '0', GETDATE());
   Fetch next From c into @MachineNo
End
Close c
Deallocate c
-- Debug
-- select * from MachineSettings where AttributeValueNo = @AttributeValueNo

END
GO
