What's the wrong with this code? - sql

I'm new in SSMS, and I'm trying to execute this code:
use master
create database SQL20145Db2
on primary
( name = Sql2015Data2, filename='C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Sql2015Data2',
size=4MB, MaxSize=15, FileGrowth= 20%
)
log on
(Name=Sql2015Log2, filename='C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Sql2015Log2',
size= 1MB,MaxSize=5Mb,filegrowth=1MB
)
But the messages pane displays this error:
Msg 5123, Level 16, State 1, Line 2
CREATE FILE encountered operating system error 5(failed to retrieve text for this error. Reason: 15105) while attempting to open or create the physical file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Sql2015Data2'.
Msg 1802, Level 16, State 4, Line 2
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

This error happen because the registry value DefaultData and DefaultLog (which correspond to default data directory) are either empty or does not exists.
See documentation for more information.
Most of the time, these registry values does not exists because they actually need to be accessed as Admin. So, to fix this issue, simply run whatever application you are using to execute the sql as an administrator.

Related

Error Msg 5105, Level 16 A file activation error occurred

I'm trying to create database manually with below command but I am getting the following error even though my file path is correct:
CREATE DATABASE QuanLySinhVien
ON PRIMARY(
NAME = QLSV_data,
FILENAME = 'E:\HQTCSDL\DaoDuyDan\QLSV_dat.mdf',
SIZE = 10MB,
MAXSIZE = 50MB,
FILEGROWTH = 2MB)
LOG ON(
NAME = QLSVlog,
FILENAME = '‪E:\HQTCSDL\DaoDuyDan\QLSV_log.ldf',
SIZE = 5MB,
MAXSIZE = 20MB,
FILEGROWTH = 1MB)
And it shows the following error:
Msg 5105, Level 16, State 2, Line 2
A file activation error occurred. The physical file name E:\HQTCSDL\DaoDuyDan\QLSV_log.ldf may be incorrect. Diagnose and correct additional errors, and retry the operation.
I've looked but can't find a fix. Any answer is appreciated. Thanks everyone
Had the same problem when running the statement on windows.
My Solution was to replace the / with \.
So while /var/opt/mssql/data/test_data.mdf worked on linux, c:/mssql/data/test_data.mdf didn't worked on windwos.
But c:\mssql\data\test_data.mdf worked on windows.
Hope it helps:
Check any whitespaces in the URL in FILENAME=' .\.\'
Check if you have correct folder directory
Check for permission

Getting Error while creating database in SQL Server 2014

I am using SQL Server 2014 and I am trying to create a database with this command
CREATE DATABASE [EFSDB]
ON (NAME = N'EFSDB_Data', FILENAME = N'$MDF', SIZE = 2, FILEGROWTH = 10)
LOG ON (NAME = N'EFSDB_Log', FILENAME = N'$LDF' , SIZE = 2, FILEGROWTH = 10%)
However, I am getting this error:
A file activation error occurred. The physical file name '$MDF' may be incorrect. Diagnose and correct additional errors, and retry the operation.
Msg 1802, Level 16, State 1, Line 42
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
This command works in SQL Server 2008 but doesn't work in SQL Server 2014.
This command is saved in sample.sql file and that file gets executed using the below command:
C:\Program Files\Microsoft SQL Server\120\Tools\Binn\OSQL.EXE" -S ServerName -E -i Sample.sql

verifyonly error message

I use following sql statement
RESTORE VERIFYONLY FROM DISK = 'F:\testBackup\test_2013-01-18.BAK'
GO
I get this error while doing so:
Attempting to restore this backup may encounter storage space problems. Subsequent messages will provide details.
The path specified by "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test.mdf"
is not in a valid directory.
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test_log.ldf"
failed with the operating system error 3(failed to retrieve text for this error. Reason: 15100).
Msg 3189, Level 16, State 1, Line 1
Damage to the backup set was detected.
Msg 3013, Level 16, State 1, Line 1
VERIFY DATABASE is terminating abnormally.
How do I fix this?
Try to use this:
RESTORE VERIFYONLY
FROM DISK = 'F:\testBackup\test_2013-01-18.BAK'
WITH NORECOVERY,
MOVE 'test_Data'
TO '<YourPath>\test.mdf',
MOVE 'test_Log'
TO '<YourPath>\test_log.ldf';
And you can check MSDN site to further advices.

Restore database in SQL Server 2005

BACKUP DATABASE [MPRM] TO DISK = N'\\rauf\shared\MPRM_15_5_10.BAK'
WITH NOFORMAT, NOINIT, NAME = N'MPRM-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
The backup process worked, and I got a file named MPRM_15_5_10.BAK in my shared folder (D:\shared\). This is a backup created from another machine.
When I try to restore the backup, using the following script
RESTORE DATABASE [MPRM]
FROM DISK = N'\\rauf\shared\MPRM_15_5_10.BAK'
WITH FILE = 1, NOUNLOAD, STATS = 10
I get the following errors
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "E:\DATABASES\MPRM.mdf" failed with the operating system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Line 1
File 'MPRM' cannot be restored to 'E:\DATABASES\MPRM.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "E:\DATABASES\MPRM_log.ldf" failed with the operating system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Line 1
File 'MPRM_log' cannot be restored to 'E:\DATABASES\MPRM_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Why the system asks about *.mdf, *.ldf files ? Is it anything related with Backup option rather than Restore script ?
I logged in with Windows Authentication
You need to specify where to store the physical files when you restore a .bak if your target server doesn't have the same disk/directory layout as the original source server. The backup file contains the logical SQL Server files along with the original location on the source server (the full physical path where the .mdf and .ldf where located).
So you need to use something like this:
RESTORE DATABASE [MPRM]
FROM DISK = N'\\rauf\shared\MPRM_15_5_10.BAK'
WITH FILE = 1,
MOVE N'MPRM' TO N'D:\MSSQL\Data\MPRM.mdf',
MOVE N'MPRM_Log' TO N'D:\MSSQL\Data\MPRM_Log.ldf',
NOUNLOAD, REPLACE,
STATS = 10
This command here:
MOVE N'MPRM' TO N'D:\MSSQL\Data\MPRM.mdf',
specifies that the logical file called MPRM (that's the default when you didn't specify anything else when creating your SQL Server database) should be moved during restore to the physical location D:\MSSQL\Data\MPRM.mdf (adapt this as needed)
To just see what is contained inside a backup file, you can use this command here:
RESTORE FILELISTONLY
FROM DISK = N'\\rauf\shared\MPRM_15_5_10.BAK'
This will show you all the logical files inside your backup, along with their original physical file that they were backed up from (on the source server, where you ran the backup command).

SQL - How to attach FileStream enabled db without log file

I'm trying to attach a FileStream enabled database without a log file. My SQL looks something like this:
USE master
CREATE DATABASE MyDB
ON PRIMARY(NAME = N'MyDB', FILENAME = 'C:\myDB.MDF' ),
FILEGROUP myFileGroup CONTAINS FILESTREAM ( NAME = myData, FILENAME = 'C:\myFileGroup')
For Attach
Here is the error I'm receiving:
Msg 5173, Level 16, State 3, Line 2
One or more files do not match the primary file of the database.
If you are attempting to attach a database, retry the operation with the correct files.
If this is an existing database, the file may be corrupted and should be restored from a backup.
Does anyone know if it's possible to attach a FileStream enabled database without the original log file?
Try this blog post:
http://blog.sqlauthority.com/2010/04/26/sql-server-attach-mdf-file-without-ldf-file-in-database/
I would personally go with this one:
CREATE DATABASE TestDb ON
(FILENAME = N'C:\Database\Test\TestDb.mdf')
FOR ATTACH_REBUILD_LOG
GO
And when you have your log rebuilt, you can enable filestream; or try to reattach with the filestream location.