Create Script of database with SQL query - sql

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

Related

How to read backup name of a database in SQL Server

When running the BACKUP command, like this:
BACKUP DATABASE dbname TO DISK ... etc.
You can specify a name for the backup with this option:
NAME = N'something'
I have many backup files and need to read the NAME property if there was specified one. How do I do it?
I think I understand your question now.
You can get all the data (including the name) of the backups made in the server by querying the backupset table in msdb database.
Try this:
SELECT *
FROM msdb.dbo.backupset
Note: This table contains only data of successful backup operations:
Contains a row for each backup set. A backup set contains the backup from a single, successful backup operation.
If you want to find the name of backup files use this T-SQL syntax:
RESTORE HEADERONLY FROM DISK = 'C:\BackupfilePath\BackUpfileName.bak'
GO
This code will give information of your backup file.
Name will comes as BackUpName field.
You can this code also with Power Shell.

Backup SQL Server database using WITH FORMAT

I'm having a bit of trouble understanding the purpose of the WITH FORMAT option of the SQL Server BACKUP DATABASE command.
Using the MSDN example:
USE AdventureWorks2012;
GO
BACKUP DATABASE AdventureWorks2012
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2012';
GO
I understand this deletes any existing backups and creates a new one.
If I was to omit the WITH FORMAT line, and I already had a backup at Z:\SQLServerBackups\AdventureWorks2012.Bak, what does this mean to my backup?
Is it incremental or does it overwrite with a full one?
It's a full backup, essentially appended to the file if it exists already, you'll see a File number 2 reference when you run it, and you'll see the backup file double in size. Each backup is independent of each other and either can be restored.

SQL server 2008 r2 loading the backup to another database

i have the following query from msdn to restore a SQL Server database backup
RESTORE DATABASE <DBName> FROM DISK = '<BackupFilePath>\<BackupFileName>'
GO
What if you want to load the backup to a new database with new name without restoring it to replace the current one?
In the interface I would select Restore Files and Filegroups and change the to database name then it would create a new one with the backup data in it, is this possible with a simple query?
The restore command above can be used to specify a new name, but when the old database still exists on the same machine you'll get an error because it will try to use the same filenames for the data. You can use the WITH MOVE clause to get around that:
RESTORE DATABASE TestDB
FROM DISK = 'C:\Backup.BAK',
WITH MOVE 'AdventureWorks2012_Data' TO 'C:\MySQLServer\testdb.mdf',
MOVE 'AdventureWorks2012_Log' TO 'C:\MySQLServer\testdb.ldf';
GO
The above example came from the MSDN documentation on the restore command:
http://msdn.microsoft.com/en-us/library/ms186858.aspx

Convert a MS SQL 2008 .mdf file to .bak file

I try to convert a .mdf file to a .bak file, I have been reading this guide: http://forums.asp.net/p/1448416/3870094.aspx
BACKUP DATABASE [NameOfDatabase] TO DISK = N'D:\path\filename.bak' WITH NOFORMAT, NOINIT, NAME = N'NameOfDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
Replace NameOfDatabase with.... name of your database.
Replace D:\path\filename.bak with place you want to backup with
Replace Name = N database name for cosmetic indexing reasons...
But I get an error message when I run it.
In Visual Studio 2010 in Server Explorer I select Data Connection/myDatabase.mdf and rightclick Stored Procedure and choose "add new stored Procedure". There I write:
BACKUP DATABASE [myDatabase] TO DISK = N'C:\db\dbCopy.bak' WITH NOFORMAT, NOINIT, NAME = N'dbCopy-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
When I select the text and run selection I get this error message:
Executing selected script from mssql:://Home\e1d5110c-dd6f-4d/C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF/dbo/Stored Procedure/dbo/StoredProcedure1
Database 'myDatabase' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
No rows affected.
(0 row(s) returned)
What am I doing wrong? If I change 'myDatabase' to 'C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF' I get this message:
Database 'C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
No rows affected.
(0 row(s) returned)
You need to attach the database first before you can back it up. Do you have an LDF file as well? You might need one, or it might be happy to build its own afresh - I'm not sure.
The simplest way to do this all is through SQL Server Management Studio. If you don't have it (e.g. you're using SQL Server Express) then you should definitely download it and install it. Log in to your SQL Server instance in Management Studio then
right-click on 'user databases', 'attach' then select your MDF file
right-click on the attached database and 'tasks', 'back up'; use 'remove' and 'add' in the destination box if you want to change the filename.
I know this post has been for a long time but i thought also someone might still need an answer that worked for me, so The bak file is just a backup file of your SQL Server database.
You can download the MS SQL Server Express Management Console (for your version of SQL Server or SQL Server Express) and do this
http://msdn.microsoft.com/en-us/library/ms187510.aspx

Selectively restoring a database

I have been using this query:
BACKUP DATABASE RentalEase
TO DISK = 'C:\RentalEaseBackup\RentalEase.bak'
WITH COPY_ONLY;
GO
To backup my database. Someone deleted something so now I have to restore it from a previous point in time, however I don't want to overwrite new changes (other than the deletions).
What I was thinking I could do it attached the backup to the SQL Server as a new database and then perform the necessary queries to move the few deleted rows over. However, it won't attach the RentalEase.bak file because it says it isn't a primary database file.
How can I attach the database backup so I can run the SQL queries against it?
You have to RESTORE the DB, you can't attach a backup file
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
Full syntax here
Restore the database to a different database name, and then you can do whatever you want between the two databases (good luck!)
create a new database named RentalEase2, A restore would look like this
RESTORE DATABASE [RentalEase2] FROM DISK = N'C:\RentalEaseBackup\RentalEase.bak'
WITH FILE = 1, NOUNLOAD, STATS = 10
GO