MS SQL Server use old log file location after detach/copy/attach -
i create database "test" in folder "d:\test". database files "d:\test\test.mdf" , "d:\test\test_log.ldf". detach database ms sql server 2008 r2, copy files new folder ("d:\test_new"), delete log file ("d:\test_new\test_log.ldf"), , try attach database again new location. when use sql server management studio, , choose "d:\test_new\test.mdf" file, determines log file located in "d:\test\test_log.ldf" (old location). how can attach database rebuilding log in new location? imagine, cannot copy ldf file again new location, , still available there, sql server see anyway. want sql server - "please, forget log file, , create new log file here". it's better if me t-sql script, if steps in management studio - convert script myself.
what had tried already:
1.
create database [test] on ( filename = n'd:\test_new\test.mdf' ) attach_rebuild_log
attaches log file old location (for attach - same)
2.
create database [test] on ( filename = n'd:\test_new\test.mdf' ) log on ( filename = n'd:\test_new\test_log.ldf' ) attach_rebuild_log
returns error: unable open physical file "d:\test_new\test_log.ldf". operating system error 2: "2(file not found.)".
3.
sp_attach_db , sp_attach_single_file_db
was tried too. , had checked source codes - create dynamic sql , call create database ... attach statement.
the question changed to: "is possible?"
update
well, looks it's not possible current versions of sql server. if knows way - please, pleased know too!
edit2: knowledge, not possible sql server recreate log file. can shrink ldf, not create when mdf exists.
when copy files d:\test\
d:\test_new\
, do not delete d:\test_new\test_log.ldf
.
leave log file there, because cannot reattach new db without log file. afterwards, can shrink log minimum size.
so, synthesize:
- copy files
d:\test\
d:\test_new\
, leave log file there. - run
create database
script posted in question (point 2). - run following script shrink log minimum size
.
use test go dbcc shrinkfile(logicalfilename, 1) go
to find out logicalfilename
is, run sp_helpfile
, give logical file name log file:
use test go exec sp_helpfile go
more info here
edit: think need first detach test
database old location:
(you might create script all, following commands)
c:\> osql -e 1> sp_detach_db 'test' 2> go 3> quit c:\>
then copy files new location.
c:\> copy d:\test\* d:\test_new\*
next, attach test
db new path location:
c:\> osql -e 1> sp_attach_db @dbname = n'test', @filename1 = n'd:\test_new\test.mdf', @filename2 = n'd:\test_new\test_log.ldf' 2> go 3> quit c:\>
to test if new database attached:
c:\> osql -e 1> use test 2> go 3> quit c:\>
if there no errors after go
command, ok
hope helps
Comments
Post a Comment