-- =============================================
-- Author:		<Dirk Strauss>
-- Create date:	<2 June 2017>
-- Description:	<Add or Update the selected preferred suppliers for a property>
-- =============================================
CREATE PROCEDURE [dbo].[sproc_PROP1_PreferredSuppliers_AddUpdate]
	@entityId int,
	@preferredSuppliers nvarchar(max)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
     SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
	BEGIN TRANSACTION;	   
	-- Try update
	   UPDATE [dbo].[prop1_PreferredSuppliers]
		 SET [preferredSuppliers] = @preferredSuppliers		    
	    WHERE [entityId] = @entityId;
	    
	IF @@ROWCOUNT = 0
	BEGIN
	-- If nothing updated, try insert
	   INSERT INTO [dbo].[prop1_PreferredSuppliers]
			([entityId]
			,[preferredSuppliers])
	    VALUES
			(@entityId
			,@preferredSuppliers)
	END
		
	COMMIT TRANSACTION;
END