EXEC sp_attach_single_file_db @dbname = 'SPDB',
@physname = 'D:\SPDB.mdf'
This same problem occurs when the owners of the file have been deleted. When this happens, if you go to the file's properties, you will see a SID rather than a user name. Take ownership of the file (giving yourself FULL CONTROL). Once that is done you can do whatever you need to do with the file.
I've had this work when logging in as the administrator didn't do the trick.
As other suggested running as administrator will help.
However this only if the windows user is actually an admisnitrator on the machine which sql server runs.
For example when using SSMS from a remote machine it will not help using "run as administartor" if the user is only a administrator on the machine runing SSMS but not on the machine running SQL Server.
1.copy your --.MDF,--.LDF files to pate this location
For 2008 server
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
2.In sql server 2008 use ATTACH and select same location for add
I had this issue on Windows 2003 with SQL 2005. I had to take ownership of the files as my Windows user account and I got the database to attache that way.
You have to right click on the file, select Properties, click OK to get past the information screen, click the Advanced button, select your account from the listing of available accounts or groups, apply that change, and Click OK on the Properties screen. Once you have done all that you will be able to manage the file permissions.
I logged into SSMS with Windows Authentication and I was able to attach the database without error.
Cheers!
If you are already running as an adminstrator, make sure the user you are using has the proper server roles.
Login as sa (if you can)
Expand the Security folder
Expand the Logins Folder
Right click on the user you would like to use
Select Properties
Select Server Roles
Select all the server roles
Click OK
Restart SSMS
Login with the modified user
In my case I have got the error when trying to create a databae on a new drive.
To overcome the problem I created a new folder in that drive and set the user properties Security to full control on it(It may be sufficient to set Modify ).
Conclusion:
SET the Drive/Folder Properties Security for users to "Modify".
My solution was slightly more complicated. After verifying the user the service was running as, running MSSMS as local and domain administrator, and checking folder permissions, I was still getting this error. My solution?
Folder ownership was still maintained by local account.
Properties > Security > Advanced > Owner > (domain/local user/group SQL services are running as)
This resolved the issue for me.
Opening SSMS as Administrator and running as SQL Auth vs Windows Auth did not work.
What worked was to just change my filename to the same location where the LDF and MDF files are located.
alter database MyDB
add file ( name = N'FileStreamName',
filename = N'D:\SQL Databases\FileStreamSpace' )
to filegroup DocumentFiles;
I got this error when restoring a database that was backed up on another server. After a long struggle this is what I did
Enabled Instant File Initialization,
Granted permissions (full control) on the folder to the service account and my own windows account,
Restarted the SQL service.
Database restored after that.
The key is "operating system error 5". Microsoft helpfully list the various error codes and values on their site
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.
copy your --.MDF
,--.LDF
files to pate this location For 2008 server C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA 2
.
In sql server 2008 use ATTACH and select same location for add
–
Here is what happened in my case.
I was asked to attach files for a database. I was given the file names as follows
devdb.mdf and devdb.ldf
I proceeded to attach the files and kept getting the files are in use by another process.
I ran a query against the system view select name, physical_name from sys.master_files; and saw that the exact file names were already in use by another database, thus each time I tried to attach the files, I kept getting the error the files are in use by another process(sql server)
Thus if you are getting such a message, then also query against the system view sys.master_files and see which database may already be using the same name files.
Hereafter you will figure out what to do.
thanks.
–
If facing similar issue in SQL Server running on docker container, fix by granting appropriate permissions to volumes.
Login to container as root user
docker exec -it -u root <container id> bash
Grant appropriate permissions to all volumes path e.g. /var/opt/sqlserver/data
chmod 777 </var/opt/sqlserver/data>
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.