File does not exist in database - SQL Server Snapshot - sql

I am attempting to create a database snapshot with SQL Server 2008 R2 using the following T-SQL code.
CREATE DATABASE SNAP_myDB_0900
ON
(NAME = myDB, FILENAME = 'C:\myDB_0900.SNAP')
AS SNAPSHOT OF myDB
I get the following error:
The file 'myDB' does not exist in database 'myDB'
This code works with other databases in the same instance but not this one. I have double checked the file name and it is correct.
Why am I getting this error?

Verify the database file name that you are trying to create the snapshot based off of:
select name, physical_name
from myDB.sys.database_files;
The NAME you give you snapshot file(s) needs to match the source database file name.
In other words, if myDB's data file has a name of datafile1, then you will have to use ... NAME = 'datafile1' ... when creating your snapshot.

Related

How to attach database in SQL Server 2008 R2 without index.idx file

I have a database test (test.mdf, test_ldf.log and test_idx.idx). By mistake I deleted the test_idx.idx file and due to file was over 20 GB.
I have used all type of recovery software but can't find the file.
Unfortunately that database is not backed up, and now SQL Server 2008 R2 doesn't let me attach the database.
Is there any way to attach the database test.mdf file without index file?
Error when I try to attach database:
Unable to open the physical file ".idx". Operating system error 2: "2(failed to retrieve text for this error. Reason: 15105)". (Microsoft SQL Server, Error: 5120)
create a dummy database (name is not important) with the same file structures. ie a data file, log file and index file.
take the index file and database offline like so:
alter database <dbname> modify file(name = '<name of your index file>', offline)
alter database <dbname> set offline
You'll not be able to recover the offline file (to my knowledge) so dont give it the same logical name as your original file - you'll see why in a moment.
now overwrite the dummy database files with what you have left of your broken database (obviously keep the copies somewhere safe!)
bring the database back online
alter database <dbname> set online
At this point your data should be accessible.
create new filegroup/file with the same logical names as your original and rebuild indexes
Good Luck!

Create Script of database with SQL query

I have purchased space online on godaddy on which my SQL database is stored but now I want to copy that database to my local pc but I cannot take backup of SQL database as it is giving error at taking backup and I cannot create a script too. So, anybody have any idea how to create script of database with SQL query?
Error : Index was out of bound array
USE databasename;
GO
BACKUP DATABASE databasename
TO DISK = 'C:\SQLServerBackups\databasename.Bak'
WITH FORMAT,
MEDIANAME = 'C_SQLServerBackups',
NAME = 'Full Backup of databasename';
GO
login in Godaddy-> select and launch your host- > select database->options -> backup
here you can see where it will take. then open file manager, move to that location, download .bak file

Copy Sql Server 2008 Databse schema only to Another Sql server 2008 Database on same machine

I have a Sql server 2008 Database
Is there any way to create new database and copy the schema of existing database objects to newly created database using Sql query or Stored Procedure
First you'll need to make a full backup of the database you want to copy:
BACKUP DATABASE Database1
TO DISK = 'X:\FullPath\AdvWorksData.bak'
WITH FORMAT;
where X:\FullPath is the full path to a location you can backup to on disk.
Next you'll need to create the new database (if you haven't already):
CREATE DATABASE Database2;
And then finally you'll need to restore over the top of that database:
RESTORE FILELISTONLY
FROM Database1;
RESTORE DATABASE Database2
FROM Database1
WITH MOVE 'Database1_Data' TO 'X:\FullPath\Database2.mdf',
MOVE 'Database1_Log' TO 'X:\FullPath\Database2.ldf';
GO
where X:\FullPath is the full path to where you're second databases files exist.
Some Caveats
MOVE 'Database1_Data' and 'Database1_Log' are the default logical file names. Those can differ. You can find those logical names under the properties of the database.
'X:\FullPath\Database2.mdf' and 'X:\FullPath\Database2.ldf' may not both exist in the same location and can differ from the default locations. You can find those locations under the properties of the database.

SQL Server 2005: Attach database using sp_attach_db or CREATE DATABASE with multiple filegroups and full text catalog

Having issues attaching a Database with multiple filegroups and a full text catalog from server A to server B with either "sp_attach_db" or CREATE DATABASE in SQL Server 2005 SP3. All the database files (primary data, secondary data, log, full-text catalog) have been copied from server A to server B.
I had a similar issue (SQL Server 2005: Attach database using sp_attach_db with full text catalog) but this example only had a primary data file, primary log file and a full text catalog
Now I am faced with adding in more filegroups and when I try to run the example command below it fails
CREATE DATABASE DBNAME ON
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME_data.mdf'),
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME_indexes.ndf'),
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME1_data.ndf'),
(FILENAME = 'C:\Databases\DBNAME\Logs\DBNAME_log.ldf'),
(FILENAME = 'C:\Databases\DBNAME\Data\FTData')
FOR ATTACH;
GO
Error Message:
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "C:\Databases\DBNAME\Data\FTData". Operating system error 5: "5(Access is denied.)".
I have used this Microsoft page (http://msdn.microsoft.com/en-us/library/ms176061.aspx) as my guide but it doesn't have an example like this. I know this has to be possible, what am I missing!?
Try the information in this question/answer: SQL Server 2005: Attach database using sp_attach_db with full text catalog
Figured out what I needed to do. Whether this is the right way or a work-around way I am not sure. This is what I did....first I removed the fulltext catalog from the create database list since it was having issues with it (no matter how I had it) and was able to create/attach the database (though I did get a warning)
CREATE DATABASE DBNAME ON
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME_data.mdf'),
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME_indexes.ndf'),
(FILENAME = 'C:\Databases\DBNAME\Data\DBNAME1_data.ndf'),
(FILENAME = 'C:\Databases\DBNAME\Logs\DBNAME_log.ldf')
FOR ATTACH;
GO
Then I got the logical name and path to the fulltext catalog from this...
SELECT * FROM SYS.DATABASE_FILES WHERE TYPE_DESC = 'FULLTEXT'
Then brought the database offline, repathed the fulltext catalog and then brought the database back online with this...
ALTER DATABASE DBNAME SET OFFLINE
ALTER DATABASE DBNAME MODIFY FILE ( NAME = sysft_cat_full_text , FILENAME = 'C:\Databases\DBNAME\Data\FTData\cat_full_text')
ALTER DATABASE DBNAME SET ONLINE
So far I have not had to rebuild the catalogs yet

Restore a database using SQL Server 2005

I have backed up a database into a file using SQL Server from my old server.
Now i would like to restore that file into a new database on my new server.
I created a DB with the same name , I am getting an error saying :
"The Backup set holds a backup of the database other than the existing '*****' database"
Any thoughts?
Thanks
Add a WITH REPLACE option to your restore:
Specifies that SQL Server should
create the specified database and its
related files even if another database
already exists with the same name
Drop the new database - it's sitting in the way of the one you want to restore.
THen when you try to restore your old database, select the file to restore from, and the name will magically appear in the "to database" destination field in SSMS.
When you restore a database from backup, you are creating a new database on the SQL instance. If a database by that name is already present on that SQL instance, you will get an error--unless you select the option to overwrite any existing database, in which case the old database will be wiped out and replaced.
I was having the same issue, but even when putting WITH REPLACE, the error occurred. I had an empty database with the same name as the back up, but the problem was my .trn file I was using to backup from had two backup sets and I was choosing to restore from the full database AND the transaction log. I chose only the Full Database and it worked.