﻿CREATE PROCEDURE [dbo].[spCreateTableMonth]
	
AS	
	--현재 월 HIST_TestResult 테이블 생성
	DECLARE @current_yymm varchar(4) = CONVERT(CHAR(4), GETDATE(), 12)

	DECLARE @current_summaryTableName varchar(40) = N'HIST_LogSummary_'+@current_yymm
	DECLARE @current_summary_query varchar(200) = N' SELECT * INTO ' + @current_summaryTableName + N' FROM HIST_LogSummary'
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @current_summaryTableName)
		BEGIN		
			EXEC(@current_summary_query)
		END

	DECLARE @current_resultTableName varchar(40) = N'HIST_TestResult_'+@current_yymm
	DECLARE @current_result_query varchar(200) = N' SELECT * INTO ' + @current_resultTableName + N' FROM HIST_TestResult'
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @current_resultTableName)
		BEGIN		
			EXEC(@current_result_query)
		END

	--다음 월 HIST_TestResult 테이블 생성
	DECLARE @next_yymm varchar(4) = CONVERT(CHAR(4), DATEADD(MONTH, 1, GETDATE()), 12)

	DECLARE @next_summaryTableName varchar(40) = N'HIST_LogSummary_'+@next_yymm
	DECLARE @next_summary_query varchar(200) = N' SELECT * INTO ' + @next_summaryTableName + N' FROM HIST_LogSummary'
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @next_summaryTableName)
		BEGIN		
			EXEC(@next_summary_query)
		END

	DECLARE @next_resultTableName varchar(40) = N'HIST_TestResult_'+@next_yymm
	DECLARE @next_result_query varchar(200) = N' SELECT * INTO ' + @next_resultTableName + N' FROM HIST_TestResult'
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @next_resultTableName)
		BEGIN		
			EXEC(@next_result_query)
		END
RETURN 0
