﻿CREATE PROCEDURE [dbo].[spCreateTableByName]
	@logSummaryTableName nvarchar(200),
	@logResultTableName nvarchar(200)
AS	
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @logSummaryTableName)
		BEGIN		
			DECLARE @summaryQuery nvarchar(4000)= N'
			CREATE TABLE [dbo].['+@logSummaryTableName+'](
				[No] [bigint] IDENTITY(0,1) NOT NULL,
				[StationName] [nvarchar](128) NULL,
				[TestType] [nvarchar](8) NOT NULL,
				[Version] [nvarchar](4) NOT NULL,
				[ProdCode] [nvarchar](4) NOT NULL,
				[TestListFileNo] [int] NULL,
				[TestListVariantNo] [int] NULL,
				[TestListCntID] [nvarchar](256) NULL,
				[StepVersion] [int] NULL,
				[HostID] [nvarchar](64) NOT NULL,
				[Section] [nvarchar](64) NOT NULL,
				[ProdNo_C] [nvarchar](32) NOT NULL,
				[ProdNo_P] [nvarchar](32) NOT NULL,
				[Testcode] [nvarchar](16) NOT NULL,
				[TestListFileName] [nvarchar](256) NOT NULL,
				[ProductID] [nvarchar](64) NOT NULL,
				[Result] [nvarchar](16) NOT NULL,
				[Duration] [nvarchar](16) NOT NULL,
				[TestDT] [datetime2](7) NOT NULL,
				CONSTRAINT [PK_'+@logSummaryTableName+'] PRIMARY KEY CLUSTERED 
			(
				[No] ASC
			)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
			UNIQUE NONCLUSTERED 
			(
				[No] ASC
			)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
			) ON [PRIMARY]			

			ALTER TABLE [dbo].['+@logSummaryTableName+'] ADD  DEFAULT (''-'') FOR [StationName]
			
			ALTER TABLE [dbo].['+@logSummaryTableName+'] ADD  DEFAULT ((-1)) FOR [TestListFileNo]
			
			ALTER TABLE [dbo].['+@logSummaryTableName+'] ADD  DEFAULT ((-1)) FOR [TestListVariantNo]
			
			ALTER TABLE [dbo].['+@logSummaryTableName+'] ADD  DEFAULT ((-1)) FOR [StepVersion]
			
			ALTER TABLE [dbo].['+@logSummaryTableName+'] ADD  DEFAULT (getdate()) FOR [TestDT]					

			CREATE INDEX ['+@logSummaryTableName+'_2] ON '+@logSummaryTableName+' (TestDT, No);			

			CREATE INDEX ['+@logSummaryTableName+'_3] ON '+@logSummaryTableName+' (TestDT);			
			';

			EXEC(@summaryQuery)
		END
			
	
	IF NOT EXISTS(SELECT * FROM sys.tables WHERE NAME = @logResultTableName)
		BEGIN		
		DECLARE @resultQuery nvarchar(4000)= N'
			CREATE TABLE [dbo].['+@logResultTableName+'](
				[No] [bigint] NOT NULL,
				[TestDT] [datetime2](7) NULL,
				[LogData] [nvarchar](max) NOT NULL,
			 CONSTRAINT [PK_'+@logResultTableName+'] PRIMARY KEY CLUSTERED 
			(
				[No] ASC
			)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
			) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]		

			ALTER TABLE [dbo].['+@logResultTableName+'] ADD  DEFAULT (getdate()) FOR [TestDT]					

			CREATE INDEX ['+@logResultTableName+'_2] ON '+@logResultTableName+' (TestDT, No);			

			CREATE INDEX ['+@logResultTableName+'_3] ON '+@logResultTableName+' (TestDT);			
			';
			EXEC(@resultQuery)
		END
RETURN 0
