	
	--UserSettingsTypes
	DECLARE @UserSettingsTypes TABLE (RowId uniqueidentifier NOT NULL,[Name] [nvarchar](500) NOT NULL,SettingsType int NOT NULL);

	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('41288138-4780-4EAF-A850-284B8903B20E', N'TeleHealthEnabled',3);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('8B4685B2-C1FB-4B69-A9F2-CBAC9548AC81', N'Weight',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('A5E7BDF1-BF7A-43A6-AA6B-4DCBE76A96E2', N'Height',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('0E01A38D-1276-4DAF-976E-2E1054DFB097', N'BloodPressure',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('0C09EA34-4243-4E5E-B4EB-18A49C5D001F', N'Temperature',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('7789986A-E1C8-4CEB-92BD-946BDD322200', N'SugarLevel',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('330AB01B-978F-49C6-9BA3-A59A17B6F01D', N'HeartRate',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('690F0D29-A393-41AB-AD1C-AD06F396EA46', N'Saturation',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('690F0D29-A393-41AB-AD1C-AD06F396ECC1', N'BaseWeight',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('690F0D29-A393-41AB-AD1C-AD06F396EDC2', N'BaseHeight',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('690F0D29-A393-41AB-AD1C-AD06F396EDC3', N'PrimaryCarePhysician',2);
	INSERT into @UserSettingsTypes (RowId, [Name], SettingsType) VALUES ('C6330BB2-B82D-433F-987A-4AABECB2C229', N'SmartCallEnabled',3);

	MERGE INTO dbo.[UserSettingsTypes] AS TARGET  
		USING @UserSettingsTypes AS SOURCE  
		ON TARGET.RowId = SOURCE.RowId	
		WHEN MATCHED THEN    
			UPDATE SET	[Name] = SOURCE.[Name],
						[SettingsType] = SOURCE.[SettingsType]
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (RowId, [Name], SettingsType) VALUES (SOURCE.RowId, SOURCE.[Name], SOURCE.SettingsType)  
	OUTPUT $action, SOURCE.*;



	--UsersSettings
	DECLARE @UserSettings TABLE (CmsUserId int NOT NULL, SettingTypeId int NOT NULL, Value nvarchar(1024) NULL);

	--Get the settings DB Id
	declare @TeleHealthEnabledIdId int
	declare @WeightId int
	declare @HeightId int
	declare @BloodPressureId int
	declare @TemperatureId int
	declare @SugarLevelId int
	declare @HeartRateId int
	declare @SaturationId int
	declare @VirtualCallEnabledId int

	select @TeleHealthEnabledIdId = Id from UserSettingsTypes where RowId='41288138-4780-4EAF-A850-284B8903B20E'
	select @WeightId = Id from UserSettingsTypes where RowId='8B4685B2-C1FB-4B69-A9F2-CBAC9548AC81'
	select @HeightId = Id from UserSettingsTypes where RowId='A5E7BDF1-BF7A-43A6-AA6B-4DCBE76A96E2'
	select @BloodPressureId = Id from UserSettingsTypes where RowId='0E01A38D-1276-4DAF-976E-2E1054DFB097'
	select @TemperatureId = Id from UserSettingsTypes where RowId='0C09EA34-4243-4E5E-B4EB-18A49C5D001F'
	select @SugarLevelId = Id from UserSettingsTypes where RowId='7789986A-E1C8-4CEB-92BD-946BDD322200'
	select @HeartRateId = Id from UserSettingsTypes where RowId='330AB01B-978F-49C6-9BA3-A59A17B6F01D'
	select @SaturationId = Id from UserSettingsTypes where RowId='690F0D29-A393-41AB-AD1C-AD06F396EA46'
	select @VirtualCallEnabledId = Id from UserSettingsTypes where RowId='C6330BB2-B82D-433F-987A-4AABECB2C229'

	--Add to ALL existing users
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@TeleHealthEnabledIdId,'false' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@WeightId,'Kgs' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@HeightId,'Cm' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@BloodPressureId,'kPa' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@TemperatureId,'Celsius' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@SugarLevelId,'MmolL' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@HeartRateId,'Bpm' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@SaturationId,'Percent' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId
	INSERT into @UserSettings select CmsUserTbl.CmsUserId ,@VirtualCallEnabledId,'false' from CmsUserTbl where SecurityLevelId <> 1 order by CmsUserId

	MERGE INTO dbo.[UserSettings] AS TARGET  
		USING @UserSettings AS SOURCE  
		ON TARGET.CmsUserId = SOURCE.CmsUserId and TARGET.SettingTypeId = SOURCE.SettingTypeId	
		WHEN MATCHED THEN    
			UPDATE SET	[Value] = SOURCE.[Value],
						[CreatedUtc] = GetUtcDate()
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (CmsUserId, SettingTypeId, [Value],CreatedUtc) VALUES (SOURCE.CmsUserId, SOURCE.SettingTypeId, SOURCE.[Value],GetUtcDate())  
	OUTPUT $action, SOURCE.*;
		
