
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

IF EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[qryMMSEntityItem_CopySafetyStockSettings]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[qryMMSEntityItem_CopySafetyStockSettings]
GO

CREATE PROCEDURE [dbo].[qryMMSEntityItem_CopySafetyStockSettings]
	@sourceEntityId BIGINT
	, @destinationEntityId BIGINT
AS

	UPDATE dei SET
		SafetyStockNumber = sei.SafetyStockNumber
		, SafetyStockUnit = sei.SafetyStockUnit
		, SafetyStockAutoCalc = 1
	FROM [dbo].[tbEntityItem] dei WITH(NOLOCK)
	JOIN [dbo].[tbEntityItem] sei WITH(NOLOCK) ON (sei.ItemID = dei.ItemID AND sei.EntityID = @sourceEntityId AND sei.InUse = 1)
	WHERE dei.EntityID = @destinationEntityId
	AND dei.InUse = 1

GO
