When I try to I get the following error in SQL Server Management Studio:
TITLE: Microsoft SQL Server Management Studio
Attach database failed for Server 'AHAKEEM'.
(Microsoft.SqlServer.Smo)
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or
batch. (Microsoft.SqlServer.ConnectionInfo)
Unable to open the physical file "C:\SQL Server 2000 Sample
Databases\northwnd.mdf". Operating system error 5: "5(failed to
retrieve text for this error. Reason: 15105)". (Microsoft SQL Server,
Error: 5120)
This is a fresh version of Northwinds mdf which just came from Microsoft's installer.
Error 5120 is a sharing violation on the file you're opening. Try starting SQL Management Studio as Administrator and make sure that the db isn't attached already.
The error occurs when the mdf or ldf file is missing, if its an ldf we can recreate the same using the below listed scripts:
Method 1: To recreate all the log files
EXECUTE sp_attach_single_file_db #dbname = 'SAMPLEDB',
#physname = N'D:\MSSQL\DATA\SAMPLEDB.mdf'
GO
Method 2: If one or more log files are missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
(FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH_REBUILD_LOG
GO
Method 3: If only one file is missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
( FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH
GO
I tried to install Northwind and pubs Sample Databases for SQL Server 2000 and attach both databases in SQL Server 2014, and gave me an error because they were compatible version.
These are the steps to successful install the Sample Database in your SQL Server 2014:
After you download the Sample Databases in your PC, then open SQL Server 2014 Management Studio. After successful connection, your SQL Server instance should be listed in the Object Explorer.
Now under File Menu select Open > File.
Find the instnwnd.sql in your computer and select it. Click Open
SQL script should open in the main window.
Do not click on Execute yet. If you do, you will get the following error:
Could not find stored procedure ‘sp_dboption’.
Around the line 20, remove the following two lines:
exec sp_dboption 'Northwind','trunc. log on chkpt.','true' exec
sp_dboption 'Northwind','select into/bulkcopy','true'
Replace them with this line as shown below:
alter database Northwind set recovery simple
To attach 'pubs' database, repeat the step 2 above and this time open instpubs.sql file. Replace the sp_dboption as you did before with:
alter database Pubs set recovery simple
All that is left is to execute the script, so click on Execute in both scripts.
Note: the folder location for your sample database by default is "C:\SQL Server 2000 Sample Databases"
Related
I Generated an SQL File to backup a database using MSSMS:
Right click on database
Generate scripts
Marked "Schema and data" on "Types of data to script"
As a result I got a complete SQL that effectively seems to contain all the needed information to rebuild my db from scratch, data included.
To test it, I tried to run it locally, and found myself stuck because MSSMS is throwing errors on the SQL Script that it itself made.... I does not make any sense :(
Am I missing sth?:
This is the first lines of the sql file just as MSSMS made it:
Any ideas?
EDIT 1
This is the error I get after removing all "ALTER DATABASE" (I assumed those were the configuration specifications that were causing trouble)
It now says the tables are already created though they are not present in the database (it did create the database tough, It's a progress!)
The script you are trying to execute contains elements that are not valid because the edition of SQL you are running locally is not the same as the edition of SQL used to generate the script.
In your case, the script was generated from azure SQL, but you are running it on sql server express edition (localdb)
Remove the azure SQL specific elements and it will work.
The default scripting option in SSMS object explorer is the same database engine type and edition as the source database. It seems the source database is Azure SQL Database based on the CREATE DATABASE syntax so the script will not run against a stand-alone instance like your LocalDB instance.
You can either modify the script or generate a new script appropriate for your target instance. To generate a script for SQL Express LocalDB, click on the Advanced button on the Set Scripting Options page of the Generate Scripts wizard. Select these options:
Script for the database engine edition: Microsoft SQL Server Express Edition
Script for the database engine type: Stand-alone instance
Issue:
OS hard disk was broke before DB backing up, the original .mdf and .ldf files can be transferred but those .mdf and .ldf files can not be attached by SQL Server Management Studio directly.
Solution:
Assuming the DB name is ‘Sample’ and DB files path is ‘D:\DB’
Step 1: Open Microsoft SQL Server Management Studio to create a New Database with
Database name: Sample
Path: D:\DB
Step2: Stop the DB Server
Step3: Delete Sample.mdf and Sample.ldf files from folder D:\DB
Step4: Copy the old Sample.mdf to the folder D:\DB
Step5: Start the DB Server
Step6: execute the following sql to detach DB
exec sp_detach_db Sample,'true'
Step7: execute the following sql to attach DB and then refresh
exec sp_attach_single_file_db 'Sample','D:/DB/Sample.mdf'
I create database in SQL Server Management Studio. I tried open database .mdf file in Visual Studio but when I click "test connection", I get error message
Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf"
Operating system error 5: "5(Access denied)An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf failed.
A database with the same name exists or specified file cannot be opened, or it is located on UNC share
I of course run it administrator, but it not work.
If you are trying to add a connection to an existing SQL Server database in Visual Studio, then you should not select the option that says Attach a database file. Also, make sure to input the correct Server Name and authentication details.
[
You can add a table in Visual Studio just by connecting to database and then right clicking on Tables under the database you have connected. Look at screen shot below.
You've created the database on the server - where it belongs. Just use it on the server - after all, SQL Server is a server-based solution!
Do NOT fiddle around with .mdf files - let SQL Server handle those. SQL Server is NOT a file-based database like Access, SQLite or FileMaker or any of those - it's server-based (and that's a good thing!).
To talk to your database, just define the necessary parameters:
server=your-machine-name\SQLEXPRESS;database=database-you-created;integrated security=SSPI;
and let SQL Server deal with all the nitty-gritty details. Just use the database as it's intended to be used - as a database under the control of the SQL Server database engine
.mdf is stand for master database file. If we select authentication to be handled by Visual Studio itself, then .mdf file and all codes for creating and handling the authentication related tables will be auto generated by
visual studio itself (for example you can see this auto generated code by creating a web api by selecting individual account authentication). Auto generated tables are as given below.
_MigrationHistory
AspNetRoles
AspNetUserClaims
AspNetUserLogins
AspNetUserRoles
AspNetUsers
And we can go to server explorer to see this table either by double clicking on the created .mdf file or View > Server Explorer. After that Server explorer will come then right click in the any table and select show table data option as the screen shot given below.
Then we can see the already created table in mdf database and we can edit the table also if we want. Screen shot given below.
I tried to attach a database (.mdf) file and I got this error:
Attach database failed for Server '...\SQLINSTANCE'. (Microsoft.SqlServer.Smo)
Additional information:
An exception occured while executing a Transact-SQL statement or batch.
(Microfost.SqlServer.ConnectionInfo)
The database 'DBName' cannot be opened because it is version 782. This server supports version 706 or earlier. A downgrade path is not supported.
Could not open new database 'DBName'. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)
P.S. I have installed SQL Server 2012 Express, and database 'DBName' has been made in Visual Studio 2012.
If you have source control
Rollback the mdf file
Rollback the idf file (log file) to the same version
I had a similar problem and this solution fixed is.
If you do not have source control and you are using Code First
Backup the mdf and idf files
Delete the mdf and idf files in the solution explorer
Create a new database from project->add new item->data->SQL server database
In the package manager console write "Enable-migrations" then "Add-migration init" then "Update-database" to create your tables in the database.
For me, if things got too complicated, I would just recreate the project and copy the code :)
I am getting SQL restore error , please help.
Im using Managment studio. Im trying to restore my database(say for example database name XYZ) taken backup from SQL 2000 server and trying to restore to 2008 R2 database XYZ. The exact error string is during Restore failed is
Restore failed for Server 'MACHINENAME\INSTANCENAME'. (Microsoft.SqlServer.SmoExtended)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1617.0+((KJ_RTM_GDR).110422-1125+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476
Program Location:
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
at Microsoft.SqlServer.Management.SqlManagerUI.SqlRestoreDatabaseOptions.RunRestore()
===================================
System.Data.SqlClient.SqlError: There is already an object named 'sysnsobjs' in the database. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1617.0+((KJ_RTM_GDR).110422-1125+)&LinkId=20476
Program Location:
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection queries, ServerMessageEventHandler dbccMessageHandler, Boolean errorsAsMessages)
at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries)
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv
ODBC SQL server Driver ERROR There is a already an object named
'sysnobjs' in database
While restoring SQL Server 2000 user database to SQL Server 2008 R2 version.
Please let me know whether Microsoft SQL Server 2000 database backup and restore it to SQL Server 2008 is allowed?
If it is not so , is there any work-around for this issue?
In options, while you are in the box that has you choose where your backup is to restore, choose overwrite existing database option. That should get rid of that error and allow you to restore that backup.
After selecting the Backup File to restore in the General Tab, Go to the Options Tab.
Select "Överwrite the existing database (WITH REPLACE)"
Then, in the "Restore the database files as:" Table, Go to the [Restore As] column
Notice the string in the first row (for Data) and the string in the second row (for Images) are exactly the same. This will result in some errors.
Change the string in the first row to something else (like adding a date), for example
C:................\YourFileName.mdf
C:................\YourFileName20151124.mdf
Problem solved
Here's my full Script:
RESTORE DATABASE [Makhado_Indigents] FROM DISK = N'C:\IndiCheck\Makhado_Indigents.bak'
WITH FILE = 1, MOVE N'New_Indigents'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.INDICHECK\MSSQL\DATA\Makhado_Indigents.mdf',
MOVE N'New_Indigents_Images'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.INDICHECK\MSSQL\DATA\Makhado_Indigents1.mdf',
MOVE N'New_Indigents_log'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.INDICHECK\MSSQL\DATA\Makhado_Indigents.ldf',
MOVE N'New_Indigents_Docs'
TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.INDICHECK\MSSQL\DATA\Makhado_Indigents.New_Indigents_Docs',
NOUNLOAD, REPLACE, STATS = 10
GO
You may also need to Switch Filestreaming on by running the following script:
EXEC sp_configure filestream_access_level, 2 RECONFIGURE
Restart the server:
Start > All Programs > Microsoft SQL Server 2008 R2 > Configuration Tools > SQL Server Configuration Manager
Right Click on the Server and select Restart