declare @path nvarchar(100)='C:\Mikro\v16xx' declare @preDBName nvarchar(100)='MikroDB_V16_' --EXEC master.dbo.sp_detach_db @dbname = N'MikroDB_V15_02._MS' SELECT replace( 'EXEC master.dbo.sp_attach_db ' + '@dbname = '+'N"'+@preDBName+DB_kod+'",'+ '@filename1 = '+'N"'+@path+'\'+DB_kod+'\'+'DATA\'+@preDBName+DB_kod+'._MS'+'",'+ '@filename2 = '+'N"'+@path+'\'+DB_kod+'\'+'DATA\'+@preDBName+DB_kod+'_log'+'.ldf'+'"' ,'"',char(39)) as _DATA, replace( 'EXEC master.dbo.sp_attach_db ' + '@dbname = '+'N"'+@preDBName+DB_kod+'_WORKDATA",'+ '@filename1 = '+'N"'+@path+'\'+DB_kod+'\'+'WORKDATA\'+@preDBName+DB_kod+'_WORKDATA._MS'+'",'+ '@filename2 = '+'N"'+@path+'\'+DB_kod+'\'+'WORKDATA\'+@preDBName+DB_kod+'_WORKDATA_log'+'.ldf'+'"' ,'"',char(39)) as _WORKDATA, replace( 'EXEC master.dbo.sp_attach_db ' + '@dbname = '+'N"'+@preDBName+DB_kod+'_LOGDATA",'+ '@filename1 = '+'N"'+@path+'\'+DB_kod+'\'+'LOG\'+@preDBName+DB_kod+'_LOGDATA._MS'+'",'+ '@filename2 = '+'N"'+@path+'\'+DB_kod+'\'+'LOG\'+@preDBName+DB_kod+'_LOGDATA_log'+'.ldf'+'"' ,'"',char(39)) as _LOGDATA, replace( 'EXEC master.dbo.sp_attach_db ' + '@dbname = '+'N"'+@preDBName+DB_kod+'_LOGDATA",'+ '@filename1 = '+'N"'+@path+'\'+DB_kod+'\'+'FILE\'+@preDBName+DB_kod+'_FILE._MS'+'",'+ '@filename2 = '+'N"'+@path+'\'+DB_kod+'\'+'FILE\'+@preDBName+DB_kod+'_FILE_log'+'.ldf'+'"' ,'"',char(39)) as _FILE FROM VERI_TABANLARI /* EXEC master.dbo.sp_attach_db @dbname = N'MikroDB_V15_09',@filename1= N'C:\Mikro\v15xx\09\DATA\MikroDB_V15_09._MS',@filename2 = N'C:\Mikro\v15xx\09\DATA\MikroDB_V15_09_log.ldf' EXEC master.dbo.sp_attach_db @dbname = N'MikroDB_V15_09',@filename1 = N'C:\Mikro\v15xx\09\DATA\MikroDB_V15_09._MS',@filename2 = N'C:\Mikro\v15xx\09\DATA\MikroDB_V15_09_log._ldf' */