-- @sday, @eday 원하는 시작일자, 종료일자로 수정.
-- @filename1, @filename2의 D:\MI_LOG\ 경로를 로그가 위치한 경로로 수정.
DECLARE @SDate DATETIME, @EDate DATETIME, @query1 NVARCHAR(4000)
DECLARE @sday VARCHAR(8)
DECLARE @Eday VARCHAR(8)
SET @sday = '20180711'
SET @eday = '20181231'
SET @SDate = CONVERT(datetime , @sday)
SET @EDate = CONVERT(datetime , @eday)
WHILE DATEDIFF(dd, @SDate, @EDate)>=0
BEGIN
SET @sday = CONVERT(VARCHAR, @SDate, 112)
SET @query1 = '
EXEC sp_attach_db @dbname = ''MI_LOG_' + SUBSTRING(@sday,0,5) + SUBSTRING(@sday,5,2) + SUBSTRING(@sday,7,2) + '_000000'',
@filename1 = ''H:\아이백업\2018\MI_LOG_' + SUBSTRING(@sday,0,5) + SUBSTRING(@sday,5,2) + SUBSTRING(@sday,7,2) + '_000000.mdf'',
@filename2 = ''H:\아이백업\2018\MI_LOG_' + SUBSTRING(@sday,0,5) + SUBSTRING(@sday,5,2) + SUBSTRING(@sday,7,2) + '_000000_log.ldf'' '
SET @SDate = DATEADD(DD,+1,@SDate)
--PRINT @query1
exec sp_executesql @query1
END