
--Vendor Setting Type
	
	DECLARE @VendorSettingsTypes TABLE (RowId uniqueidentifier NOT NULL,[Name] [nvarchar](500) NOT NULL,SettingsType int NOT NULL);

	INSERT into @VendorSettingsTypes (RowId, [Name], SettingsType) VALUES ('12812740-4D0F-46C6-8492-C9F32B9A652B', N'MaxUnitOrganizationRanks',2);

	MERGE INTO dbo.[VendorSettingsTypes] AS TARGET  
		USING @VendorSettingsTypes 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.*;


	insert into VendorSettings (VendorId,SettingTypeId,value)
	selecT vendorid,(select top 1 id from VendorSettingsTypes where rowid='12812740-4D0F-46C6-8492-C9F32B9A652B'),'5'
	from VendorTbl v
	where 0 = (select count(1) from VendorSettings vs join VendorSettingsTypes t on t.Id=vs.SettingTypeId where VendorId=v.VendorId and t.rowid='12812740-4D0F-46C6-8492-C9F32B9A652B' )
