How to set data and log file location for SQL Database? - sql-server

I'm trying to create a database where the data and log files are saved to the E drive but not sure how to do so. I've tried:
CREATE DATABASE TachographDataContent_Archive
[ON E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data]
[LOG ON {D:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data}]
And I'm getting this error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TachographDataContent_Archive'.

To create database using query, you need to mention .mdf and .ldf file. So try below script.
Like this
CREATE DATABASE [TachographDataContent_Archive]
ON PRIMARY (NAME = 'TachographDataContent', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent.mdf')
LOG ON
(NAME = 'TachographDataContent_log', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent_log.ldf')
GO
good luck...

Related

How to fix could not use view or function because of binding errors

I have Table-valued function when trying to execute I am getting below error
Msg 208, Level 16, State 1, Procedure IPTRate, Line 19 Invalid object
name 'DBName.dbo.AccessTable'. Msg 4413, Level 16, State 1, Line 3
Could not use view or function 'dbo.ExchangeRate' because of binding
errors.
Actually the database name recently renamed from DBName to DBNameNew. Is the error because of database rename? how to fix this. please suggest.
Thanks.
Error message is clear..
Msg 208, Level 16, State 1, Procedure IPTRate, Line 19 Invalid object name 'DBName.dbo.AccessTable
you will need to rename all the views

How to restore my backup to a different database?

I have a backup and I want it to restore it to a new
Database Wss_Content3
The backfile is a backup of the Wss_Content database.
When I run the following code:
USE Master;
GO
RESTORE DATABASE Wss_Content3 FROM DISK = 'f:\Fill_Wss_Content.bak'
WITH
MOVE 'Wss_Content3' TO 'E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content3.mdf',
---------------------------------------^^^^^
MOVE 'Wss_Content3_log' TO 'F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content3_log.ldf';
I get:
Msg 1834, Level 16, State 1, Line 3 The file
'E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content.mdf' cannot be
overwritten. It is being used by database 'WSS_Content'. Msg 3156,
Level 16, State 4, Line 3 File 'WSS_Content' cannot be restored to
'E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content.mdf'. Use WITH
MOVE to identify a valid location for the file. Msg 1834, Level 16,
State 1, Line 3 The file
'F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content_log.ldf' cannot
be overwritten. It is being used by database 'WSS_Content'. Msg 3156,
Level 16, State 4, Line 3 File 'WSS_Content_log' cannot be restored to
'F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content_log.ldf'. Use
WITH MOVE to identify a valid location for the file. Msg 3119, Level
16, State 1, Line 3 Problems were identified while planning for the
RESTORE statement. Previous messages provide details. Msg 3013, Level
16, State 1, Line 3 RESTORE DATABASE is terminating abnormally.
It's like to restore on the database Wss_Content but I want to do it on a new database Wss_Content3.
How can I do it?
EDIT:
With:
USE Master;
GO
RESTORE DATABASE Wss_Content3 FROM DISK = 'f:\Fill_Wss_Content.bak'
WITH
MOVE 'Wss_Content' TO 'E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content3.mdf',
---------------------------------------^^^^^
MOVE 'Wss_Content_log' TO 'F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content3_log.ldf';
I get:
Msg 3154, Level 16, State 4, Line 2
The backup set holds a backup of a database other than the existing 'Wss_Content3' database.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
EDIT2:
USE Master;
GO
RESTORE FILELISTONLY FROM DISK = 'f:\Fill_Wss_Content.bak'
WSS_Content E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content.mdf D PRIMARY 163158360064 35184372080640 1 0 0 140B6DD4-DCA0-4FD8-BA65-59390D5FC3B5 0 0 162484191232 512 1 NULL 133075000000938400215 F50C4B9C-B575-4158-B5D6-F44B92FE0380 0 1 NULL
WSS_Content_log F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\WSS_Content_log.ldf L NULL 22918791168 2199023255552 2 0 0 C83B63C2-B68E-4101-A52B-07EFC08446D7 0 0 0 512 0 NULL 0 00000000-0000-0000-0000-000000000000 0 1 NULL
EDIT3:
I've tried this (with Wss_Content5.mdf and Wss_Content5_log.ldf
) non existing:
RESTORE DATABASE Wss_Content3 FROM DISK = 'f:\Fill_Wss_Content.bak'
WITH
MOVE 'Wss_Content' TO 'E:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content5.mdf',
---------------------------------------^^^^^
MOVE 'Wss_Content_log' TO 'F:\MSSQL\MSSQL11.SHP2013PROD\MSSQL\Data\Wss_Content5_log.ldf
Unfortunately I get this error:
Msg 3154, Level 16, State 4, Line 5
The backup set holds a backup of a database other than the existing 'Wss_Content3' database.
Msg 3013, Level 16, State 1, Line 5
RESTORE DATABASE is terminating abnormally.
You need to use the following option:
WITH REPLACE
If you're using the wizard from SQL Management Studio to restore the database rather than the command line, there is a corresponding option that needs to be checked: "Overwrite the existing database (WITH REPLACE)"

Error in SQL Server updating Image datatype from a linked server

This is a table in a Microft Dynamics 2009 database. Our Test database is missing a bunch of Image data, so I would like to update the table in test with the data in production. I'm using this SQL for this update. When I execute this, I get this error:
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 306, Level 16, State 2, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Query:
UPDATE INVENTTABLE
SET
Z_IMAGE = i2.Z_IMAGE,
Z_IMAGEMIMETYPE = i2.Z_IMAGEMIMETYPE
FROM INVENTTABLE i1
JOIN [PRODSQLSERVER].[DAX2009DB].[dbo].INVENTTABLE i2
ON i1.RECID = i2.RECID
WHERE i2.Z_IMAGE IS NOT NULL
I can't see a place where I'm attempting to compare or sort the Image data.
Try changing UPDATE INVENTTABLE to UPDATE i1.

Creating database error

I want to create a database from my local computer through a stored procedure it is not allowing to create the database .
I'm getting this error:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "D:\SHA_dat\SHA.dat" failed with the operating system error 2(The system cannot find the file specified.).
Msg 1802, Level 16, State 1, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
And my code is here:
SELECT #cSQL = 'CREATE DATABASE '+#cDBName+' ON ( NAME = '''+#cDBName+'_data'''+',
FILENAME = ' + quotename(#cDbPath)+ ') LOG ON ( NAME = '''+#cDBName+'_Log'',
FILENAME = ' + quotename(#cLogPath)+ ')'
select #cSQL
where #cDBName refers for the Datbase name to create and #cDBPath and #cLogPath refers to netork paths to create the .dat and .log files
Can anyone help me?
You write that #cDBPath and #cLogPath are network paths. This won't work. While the network path is available for you and you are submitting the CREATE DATABASE, the SQL Server service is processing the CREATE DATABASE. The service is running in it's own logon session with it's own user account and it does not have the same drives mapped as you have. So you cannot use a network drive as you do it.
BTW, the same applies when you want to recover from a backup. The backup needs to be available on a local drive so that the SQL Server service can access it.

Restore Database and change the location for MDF File

I wont restore my database but the location path is not the same. How can i change this path(partition)?
RESTORE DATABASE [MY_DATABASE]
FROM DISK = 'C:\Content.bak'
WITH FILE = 1,
NOUNLOAD,
STATS = 10
Error Message:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "F:....\Content01.mdf" failed with the operating system error 3(failed to retrieve text for this error.
Reason: 15100).
Msg 3156, Level 16, State 3, Line 1
File 'Content01' cannot be restored to 'F:....\Content01.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 "H:....\Content01_log.LDF" failed with the operating system error 3(failed to retrieve text for this
error. Reason: 15105).
Msg 3156, Level 16, State 3, Line 1
File 'Content01_log' cannot be restored to 'H:....\Content01_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.
THANKS.
RESTORE DATABASE [My_Database]
FROM DISK = 'C:\Content.bak'
WITH MOVE 'MyDatabase_Data' TO 'C:\Data\MyDatabase_Data.mdf',
MOVE 'MyDatabase_Log' TO 'C:\Data\MyDatabase_Log.ldf',
REPLACE,
STATS=10
Use the WITH MOVE command of the restore as discussed in this SO Question.
If anyone is here because they are restoring a database with multiple files, each destination file needs a new name. Using SQL Server 2008 R2, the gui does not provide an obvious clue nor does it solve it automatically.

Resources