22

I'm trying to execute the following script in SQL Server Management Studio:

USE [master]
GO

CREATE DATABASE [test1] ON PRIMARY ( NAME = N'test1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1.mdf', SIZE = 70656KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB) LOG ON ( NAME = N'test1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1_log.ldf', SIZE = 164672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)

But I'm getting the error:

Msg 5123, Level 16, State 1, Line 2
CREATE FILE encountered operating system error 5 (Access is denied.)
while attempting to open or create the physical file
'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\test1.mdf'.

Msg 1802, Level 16, State 4, Line 2
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Already have all role permissions for my user, any ideas on what's wrong?

thiagocfb
  • 323
  • 1
  • 2
  • 4

4 Answers4

25

You are getting a permissions error. The account which is running the SQL Server doesn't have the needed rights on the folder which will contain the database files.

You need to give the account which is running SQL Server (not your account) full control of C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA.

mrdenny
  • 27,106
  • 2
  • 44
  • 81
7

Based on our comment thread it sounds like you may have gotten yourself a bit sideways during install. The installer allows you to choose your default data directory and (I would assume) sets the appropriate permissions on that directory for the service account that you specified.

In your CREATE DATABASE statement you're specifying a location, but was that location the one that was specified in the original setup? Has the service account changed?

A way to test this is to just run a generic

CREATE DATABASE [test1]; 
GO

If you get the same error then perhaps the service account has changed or something about the NTFS permissions has changed.

A resolution path (also based on comment string) is to confirm that the service that is running SQL Server has R/W permissions on the path that you're specifying. To do this:

Start->Run->services.msc->scroll through the list of services until you find SQL Server->right-click->properties->Log On tab

Now go and ensure that account has the appropriate permission on that directory to do what it needs to do.

swasheck
  • 10,755
  • 5
  • 48
  • 89
2

Seems there are incorrect number of spaces in the supplied path, so, it is not matching folders tree.
Sql server won't create an non existing path.

Edit:
Your original post says:

...\Microsoft SQL         Server\...
...\Microsoft SQL     Server\...

and I guess this are not existing paths, and as they are sorounded by colons, it is relevant how much spaces are there.

Saic Siquot
  • 143
  • 4
0

The above script that you post in your question section is correct. It might be possible that the file path that you mention in FILENAME could be wrong.

Please use the script given below. It simply works then make sure the file path that you use in your script.

Use Master
go

CREATE DATABASE test1 ON PRIMARY ( NAME = N'test1', FILENAME = N'D:\test1.mdf', SIZE = 70656KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1014KB) LOG ON ( NAME = N'test1_log', FILENAME = N'D:\test1_log.ldf', SIZE = 164671KB , MAXSIZE = 1048GB , FILEGROWTH = 10%) GO

JP Chauhan
  • 1,361
  • 2
  • 10
  • 20