DROP PROC IN_INVENTPHISICAL2InsProc
GO
CREATE PROC IN_INVENTPHISICAL2InsProc
( @SITEID		UD_IDSITE,
  @INVTIDSKU		UD_IDCORPORATIVO,
  @STOCKPHISICAL	UD_STOCK	
)
AS
BEGIN

	BEGIN TRAN

	DECLARE @CLASSID	UD_CATEGORY
	DECLARE @CLASSUBID	CHAR(5)

	SELECT @CLASSID=CLASSID,@CLASSUBID=CLASSUBID FROM IN_INVENTORY WHERE INVTIDSKU=@INVTIDSKU

	IF(EXISTS(SELECT * FROM IN_INVENTPHISICAL WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU))
	BEGIN

		--DELETE FROM IN_INVENTPHISICAL WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU

		UPDATE IN_INVENTPHISICAL SET STOCKPHISICAL=STOCKPHISICAL + @STOCKPHISICAL WHERE SITEID=@SITEID AND INVTIDSKU=@INVTIDSKU

		IF(@@ERROR!=0)
		BEGIN
			RAISERROR 20000 'Error Actualizando en Tabla de Inventario Físico'
			ROLLBACK
			RETURN(1)
		END 

	END 
	ELSE
	BEGIN
		INSERT 	INTO IN_INVENTPHISICAL(SITEID,INVTIDSKU,CLASSID,CLASSUBID,STOCKPHISICAL)	
			VALUES(@SITEID,@INVTIDSKU,@CLASSID,@CLASSUBID,@STOCKPHISICAL)

		IF(@@ERROR!=0)
		BEGIN
			RAISERROR 20000 'Error insertando en Tabla de Inventario Físico'
			ROLLBACK
			RETURN(1)
		END 
	END

	COMMIT TRAN 
	RETURN(0)
END

EXEC IN_INVENTPHISICALInsProc '57   ','019201',0

SELECT * FROM IN_INVENTORY WHERE INVTIDSKU='019201'