IF EXISTS(SELECT * FROM SYS.PROCEDURES WHERE NAME='PROC_PPL_AutoSaveCurrentData')
	DROP  PROCEDURE  dbo.PROC_PPL_AutoSaveCurrentData;
GO
	
CREATE PROCEDURE PROC_PPL_AutoSaveCurrentData
	@TableName varchar(100)
	,@FRECORDDATE datetime
	,@STARTDATE date
	,@ENDDATE date
	,@FRECORDYEAR INT
	,@FRECORDMONTH INT
AS 
BEGIN
	EXEC PROC_INQUIRPLMTMPRTP @TableName,@STARTDATE,@ENDDATE,2052,' ROW_NUMBER() OVER(ORDER BY t0.FID ) FIDENTITYID '

	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)

	execute(N'drop table'+ @TableName)
END