
IF EXISTS(SELECT * FROM SYS.PROCEDURES WHERE NAME='PROC_INQUIRE_PLANPLMRPT')
	DROP  PROCEDURE  dbo.PROC_INQUIRE_PLANPLMRPT;
GO
	
CREATE PROCEDURE PROC_INQUIRE_PLANPLMRPT
	@TableName varchar(100)
	,@LCID int
	,@SeqFld varchar(200)
	,@year int
	,@month int
	,@HasId int out
AS 
BEGIN
	set @hasId = 0;
	IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[T_PLAN_PLANPLMRPT]') AND type IN ('U')) 
		BEGIN
			SELECT 
				@HasId = COUNT(A.FID) 
			FROM 
				T_PLAN_PLANPLMRPT A
			WHERE
				A.FRECORDMONTH = @month AND A.FRECORDYEAR = @year
		END
	if @hasId > 0
		begin
			SELECT 
				* 
				into #TMPMPS
			FROM 
				T_PLAN_PLANPLMRPT A
			WHERE
				A.FRECORDMONTH = @month AND A.FRECORDYEAR = @year

			DECLARE @SQL VARCHAR(2000)
			SET @sql = '
			select 
			t0.*
			,'+@SeqFld+'
			into '+ @TableName +' From #TMPMPS t0 '

			EXECUTE(@sql)
			DROP TABLE #TMPMPS
		end
END