Creating database error - sql-server

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.

Related

Why the path of my file is not correct when I trying to create database?

I just started learning SQL. I downloaded the mdf file and moved it to the SQL folder. The course instructor told us to enter these commands to display the finished database. It throws me the error that my file doesn't exist even though I entered the correct path name. What is the matter? Sorry if the question is too stupid I just started learning.
Here is some pictures of the problem.
Your .mdf file should be in this path:
FILENAME = N'C:\MSSQL\SQLData\<database name>.mdf'
To attach a database there are some ways to do it like :
The one i've showed you:
USE [master]
GO
create database <DatabaseName> ON
(name='LogicalName of the Data file', FileName='Data File Name'),
(name='LogicalName of the Log file', FileName='Log File Name')
FOR ATTACH;
Or this way:
USE [master]
GO
EXEC sp_attach_db #dbname = N'DatabaseName',
#filename1 = '<Location of the database file>',
#filename2 = '<Location of the Log file>';

Understanding SQL Server Database name vs file location

I'm confused by all the different names the same database is given. Take this code for example:
CREATE DATABASE DB_nameA
ON
( NAME = DB_nameB, FILENAME = 'C:\myDatabases\DB_nameC.mdf')
LOG ON
( NAME = DB_log, FILENAME = 'C:\myDatabases\DB_log.ldf' ) ;
GO
What's the difference between DB_nameA, DB_nameB, and DB_nameC? How does SQL Server keep track of which names are associated with each other? E.g. Somehow when I write "DROP DATABASE DB_nameA", SQL knows to delete the "DB_nameC.mdf" file.
DB_nameA is how I'd refer to a database in SQL (e.g. DROP DATABASE DB_nameA). And DB_nameC is the file name I'd see in Windows Explorer. But when is DB_nameB ever used or seen again?
CREATE DATABASE <logical db name>
ON
( NAME = <logical data filename>, FILENAME = <physical data file path>)
LOG ON
( NAME = <logical log filename>, FILENAME = <physical log file path>);
GO
DB_nameA is your Database Name
For each database, MDF and LDF files are required.
MDF - Master Database File(Primary data file)
LDF - Log Database File(Transaction File)
So whenever creating a database,we have to specify "mdf" and "ldf" names along with the respective file path. All the data and transaction log files will be stored in respective files.
ex :
CREATE DATABASE DB_nameA
ON
( NAME = DB_nameA_Data, FILENAME = 'C:\myDatabases\DB_nameA_Data.mdf')
LOG ON
( NAME = DB_nameA_log, FILENAME = 'C:\myDatabases\DB_nameA_log.ldf' ) ;
GO

Cloning Standard:S0 database to a Basic edition (test developmen)

really simple doubt, guess it is a bug, or something I got wrong
I have a databse in Azure, as Standard:S0 Tier, now 178 mb, and I want to make a copy (in a master's procedure) but with result database in BASIC pricing tier
Tought as:
CREATE DATABASE MyDB_2 AS COPY OF MyDB ( EDITION = 'Basic')
With unhappier result :
Database is created as pricing tier Standard:S0
Then tried:
CREATE DATABASE MyDB_2 AS COPY OF MyDB ( SERVICE_OBJECTIVE = 'Basic' )
or
CREATE DATABASE MyDB_2 AS COPY OF MyDB ( EDITION = 'Basic', SERVICE_OBJECTIVE = 'Basic' )
With even unhappy result :
ERROR:: Msg 40808, Level 16, State 1, The edition 'Standard' does not support the service objective 'Basic'.
tried also:
CREATE DATABASE MyDB_2 AS COPY OF MyDB ( MAXSIZE = 500 MB, EDITION = 'Basic', SERVICE_OBJECTIVE = 'Basic' )
with unhappier result :
ERROR:: Msg 102, Level 15, State 1, Incorrect syntax near 'MAXSIZE'.
.
May I be doing something not allowed ?
But if you copy your database via portal, you'd notice that basic tier is not available with message'A database can only be copied within the same tier as the original database.'. The behavior is documented here:'You can select the same server or a different server, its service tier and performance level, a different performance level within the same service tier (edition). After the copy is complete, the copy becomes a fully functional, independent database. At this point, you can upgrade or downgrade it to any edition. The logins, users, and permissions can be managed independently.'

restore database from backup

I have a database called IND_Master which I have backed up in a file named "IND_Master.bak".
I would like to restore this into another database called 'IND_test" so that they are identical. both the data and structure needs to be identical.
Can someone either give me the script or tell me how to do this from sql server..even if it means creating another IND_master and changing the name to IND_test.
The following script will restore your backup file to a new database called IND_test and rename the logical file names accordingly. Obviously you'll need to alter the paths though.
USE [master]
RESTORE DATABASE [IND_test]
FROM DISK = N'C:\SQL\Backups\IND_Master.bak' WITH FILE = 1,
MOVE N'IND_master' TO N'C:\SQL\Data\IND_test.mdf',
MOVE N'IND_master_log' TO N'C:\SQL\Logs\IND_test_log.ldf'
GO
ALTER DATABASE [IND_test]
MODIFY FILE (NAME = 'IND_master', NEWNAME = 'IND_test')
GO
ALTER DATABASE [IND_test]
MODIFY FILE (NAME = 'IND_master_log', NEWNAME = 'IND_test_log')
GO

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

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...

Resources