Translate

Home > February 2014

February 2014

SQL server attach a series of database using query

Wednesday, February 19, 2014 Category : 0

1 . step one
save all attach database name of you pc sql server using following command.

SELECT * FROM sys.master_files

Save the result to excel sheet.

now if you re-install your pc OS.


use the following. query for resotre all database. you may need some tuning to your database depend on the data found.




declare @dbName nvarchar(max)
declare @dbpath nvarchar(max)

declare dbc cursor for


-- Note F1 condition is used for escap some database . no need to attach those db
-- Note F2 condition is used to load only mdf file

select db_name,path from tablesm where F1>9 and F2=1


open dbc

fetch next from dbc into @dbName,@dbpath

while @@fetch_status = 0

begin

declare @logs nvarchar(max)

-- Note generating log file location from mdf file.
set @logs = substring(@dbpath,0,len(@dbpath)-3) + '.LDF'
--print @logs

declare @SQL  nvarchar(max)

set @SQL ='
CREATE DATABASE '+@dbName+'
 ON
( FILENAME = '''+@dbpath+''' ),
( FILENAME = '''+@logs+''' )
 FOR ATTACH
 '
--print @SQL
exec( @SQL)

fetch next from dbc into @dbName,@dbpath

end

close dbc
deallocate dbc

Microsoft sql server error : 5123 [Solved]

Category : 0

Access denied Error Solution :

open the properties of ldf and mdf file
1.  Check the owner ship status of mdf and ldf file.
2. Check if there a use call  Everyone user added to file group , if not found then added it to and give full permission.










1. if you have a lot file to do this operation you can do it using command prompt.


for taking ownership of all database or file of a directorry

D:
cd D:\SOL ServerDatabase2008\All
pause
takeown /f . /r
pause

save it and give extension of bat file and then execute using admin previlage


for add everyone user of a direcotry

icacls * /t  /grant Everyone:F
pause

must run on same directory




Powered by Blogger.