
IF EXISTS(SELECT * FROM SYS.PROCEDURES WHERE NAME='PROC_PPL_SaveCurrentData')
	DROP  PROCEDURE  dbo.PROC_PPL_SaveCurrentData;
GO
	
CREATE PROCEDURE PROC_PPL_SaveCurrentData
	@TableName varchar(100)
	,@FRECORDDATE varchar(20)
	,@FRECORDYEAR INT
	,@FRECORDMONTH INT
AS 
BEGIN
	IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[T_PLAN_PLANPLMRPT]') AND type IN ('U')) 
	BEGIN
		DELETE FROM T_PLAN_PLANPLMRPT WHERE FRECORDDATE = @FRECORDYEAR AND FRECORDMONTH = @FRECORDMONTH
	END

	DECLARE @SQL VARCHAR(2000)
	SET @sql = '
	SELECT *,'+@FRECORDDATE+' AS FRECORDDATE ,'+@FRECORDYEAR+' AS FRECORDYEAR ,'+@FRECORDMONTH+' AS FRECORDMONTH
	INTO T_PLAN_PLANPLMRPT FROM '+@TableName

	EXECUTE(@sql)

END