SQL Server: attach database without a log file - sql-server

I have uninstalled my SQL Server 2019 and installed SQL Server 2022 Express. All my databases are under:
C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA
My new location is:
C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA
Attaching a database directly from the old location results in:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName.mdf". Operating system error 5: "5(Access is denied.)".
I then copied the .mdf file to the new location. But trying to attach it from the new location, I get a similar error, but for the log file:
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\DBName_log.ldf". Operating system error 5: "5(Access is denied.)".
I can't copy the log file into the new location because I don't have enough space on my HD.
Any idea?
Thanks

Operating system error 5: "5(Access is denied.)".
SQL Server locks down the data folder with NTFS ACLs. So you can fix this by taking ownership of the folder and files and granting full control to the other SQL Server instance.
SQL Server disables permissions inheritance on the files, so you need to grant full control to the other SQL instance to each file. And if you're connected using Windows Integrated Auth, you'll also need to grant yourself full control over the files.
In my test, after granting full control over the mdf and ldf to 'NT Service\MSSQLSERVER' and to my windows user, I was able to run
CREATE DATABASE [Foo]
ON (FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo.mdf' ),
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\Foo_log.ldf' )
for attach
from the default instance.
You can also move (not copy) the files without needing additional space.

Related

Permissions required for the DATA folder

I have Sql Server 2017 and the Data folder is the default at C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL. The access rights on the folder are:
I have an old database with its MDF file in My SQL Database. I tried to duplicate the same rights to My SQL Database:
I couldn't duplicate those for ALL APPLICATION PACKAGES as I don't know who it is.
With the above rights on My SQL Database I couldn't reattach a database from its MDF file in that folder. It keeps saying Operating system error 5: "5(Access is denied.)" on the MDF file.
What access permissions are required to have database files in another folder other than the default?
Execute the following SELECT:
SELECT * FROM sys.dm_server_services
And check the value of service_account for the servicename thats SQL SERVER:
Then make sure that the folder and the files have the proper access rights for that account.

How to attach a SQL database created under a different account

I created several SQL databases on an external drive using the local admin account on my PC. This PC was then recalled and rebuilt by Head Office.
I have tried and failed to attach these databases to a new install of SSMS.
All of this was done using SQL Server 2016 Developer Edition.
I can created new DBS on the external drive.
I have given my local admin account and the SQL NT Service account (including the SQL Agent one) full access to the folders, the .MDF and the .LDF files.
The error is 'Create file encountered operating system error 5(access is denied.) while attempting to open or create the physical file 'D:|SQLDATA\TESTDB.mdf.' (Microsoft SQL Server, Error: 5123)
Could it be because I didn't detach the databases from the old SQL Server install, which no longer exists?
If anyone can help, I'll be very grateful.
Run Sql as administrator.
or
Right click on file. Propetries > Security.
change the security permissions of ldf , mdf file to full permissions.

Access denied to Sql Server mdf file from Visual Studio 2013 data connections

After a fresh install of Windows 8.1 and VS 2013, I noticed that adding a data connection from VS to a Sql server database file didn't work. I tried an existing mdf file.
I installed VS 2013 ultimate and I assume that this installattion includes Sql server tools to open database files (I included that during installation). But I did not install any Sql server edition or express edition.
Error: "unable to open the physical file", "access denied" and also: "an atttempt to attach an auto-named database for file ...mdb failed" and "a database with the same name exists, or specified file cannot be opened, or it is located on UNC share".
Well my account is an administrative local account and I run VS in administrative mode. I checked the security of the files and the group 'administrators' has all rights.
Also the mdf file is not corrupt or so and on the local hard drive.
you would have to attach the database to an sql instance, can be done in your connection string

SQL Server database export to another computer

I use SQL Server Managment Studio, SQL Server Express. I gererate a script with schema and data from Task -> Generate Script. And I try to import this script in other computer with the same SQL Server but this giving me error :
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf" failed with the operating system error 3 (The system cannot find the path specified.).
What should I have done to import /export entire database from one PC to another .
If you read through the first few lines of the script, you will see code that specifies the name and location of the files of the database. In your case look for 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf' in your script. Then look at the directory structure of the target machine. You will need to either:
1. create a directory 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\' and make sure the sql instance on the machine has RW access to that directory.
or much better,
2. Modify the script to put the file in a location that the target server wants to put files.
In regards to #2, you can have the target server give you a hint as to where to put files by right-clicking on "Databases" in Microsoft SQL Server Management Studio and create a script to create a database. The directory of the files in the create database script from the target-server should show you what to do.

Copying .mdf file from app_data folder to default localhost folder

My friend gave me a database file: record.mdf. I copied that .mdf file to my app_data folder and I can access it.
However, the connection string contains absolute path:
AttachDbFilename="C:\Users\Dell\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\App_Data\record.mdf"
But I want it to connect using:
Data Source=localhost\SQLEXPRESS;
How do I copy .mdf file to SQL Server's local folder, so that the connection string does not use an absolute path to the database?
I am using Visual Studio 2010. I do not have SQL Server Management Studio.
Step 1: you need to find out your SQL Server's data directory. This will be something like
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data
by default (for SQL Server 2008 R2 Express) - it might be slightly different in your case, depending on how you installed your SQL Server Express (and which version you have).
Step 2: copy that record.mdf file to that directory
Step 3: attach it to your SQL Server Express instance - using sqlcmd if you don't have Mgmt Studio at hand:
c:\> sqlcmd -S .\SQLExpress
Then at the sqlcmd prompt, type in:
USE [master]
GO
CREATE DATABASE record ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\record.mdf' )
FOR ATTACH_REBUILD_LOG;
GO
This will attach the .mdf file as your new "logical" database record to your SQL Server Express instance, and it will rebuild the missing transaction log file (.ldf) in the process.
From now on, you can use
server=.\SQLEXPRESS;Database=record;Integrated Security=SSPI;
as your connection string to connect to your database
Rather than copying it to the SQL server local folder, you can access it from the App_Data directory using |DataDirectory|\record.mdf
Documentation: http://msdn.microsoft.com/en-us/library/ms247257(v=vs.80).aspx

Resources