SQL Server : backup database with T-SQL? - sql-server

I am looking for a way to backup a SQL Server database with T-SQL. I do no have root access to this server through the console, as my only access comes through SQL Server Management Studio.
Could someone please show me the SQL that I could use to export the raw SQL for my entire database?

BACKUP DATABASE #strDB TO DISK =#BackupFile WITH RETAINDAYS = 10, NAME = N'MyDataBase_DATA-Full Database Backup', STATS = 10
You must define #BackupFile and #strDB as the database name.
All of this is free in Books Online which you can find online.

If you only have access to the server through Management Studio, where do you expect it to stream your backup? Getting the T-SQL to work is one thing - getting SQL Server to be able to write the backup to a place where you can access it is quite another. You may want to look at Red-Gate's SQL Azure Backup:
http://www.red-gate.com/products/dba/sql-azure-backup/

Related

Database already exists even after deleting the database files

I installed a trading software that uses pair specified and gives me details about it. For initiating the software I have to create a database that it uses to store and retrieve information from. Now I have deleted the database(mdf and ldf file) and now when I create it again it shows that database already exists error. Do I have to delete DSN or something?
I can do create database using another sql server instance, but I want to do on the same one.
I am clueless. Thanks in advance.
The database information is maintained by sql server express. So just deleting the file doesn't remove the database from sql server. You need to detach the database and then your logic should work. Try sql server management studio express 2008. Or try connect to sql server express programmatically and execute sp_detach_db permissions.
Source : MSDN
A Database is not only MDF and LDF files but also a record in the master database. So you should drop this record about your DB using DROP DATABASE command or through MSSQL Enterprise Manager.

Error copying SQL Server 2000 database to SQL Server 2008

I'm trying to upgrade a database from a SQL Server 2000 instance to a SQL Server 2008 instance. I'm doing this by right clicking on the database and selecting copy database. My current issue is that I'm getting this error in the log file:
OnError,AQUE-SQLDEV,NT
AUTHORITY\SYSTEM,aque-db2000_aque-sqldev_sql2008_Transfer Objects
Task,{F0ACDE4D-D023-400C-BE3C-91CD3A537988},{40E67169-0F3F-4F86-AD2E-6E2CB532BA5C},18/10/2011
15:38:37,18/10/2011 15:38:37,0,0x,Script failed for User 'WebUser'.
StackTrace: at
Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(DependencyCollection
depList, SqlSmoObject[] objects) at
Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(SqlSmoObject[]
objects) at
Microsoft.SqlServer.Management.Smo.Transfer.Microsoft.SqlServer.Management.Common.ITransferMetadataProvider.SaveMetadata()
at
Microsoft.SqlServer.Management.Dts.DtsTransferProvider.Configure(ITransferMetadataProvider
metadataProvider) at
Microsoft.SqlServer.Management.Smo.Transfer.GetTransferProvider()
at Microsoft.SqlServer.Management.Smo.Transfer.TransferData() at
Microsoft.SqlServer.Dts.Tasks.TransferObjectsTask.TransferObjectsTask.TransferDatabasesUsingSMOTransfer()
InnerException-->Creating a user without an associated login is not
supported in SQL Server 2008.;
Does anyone know why this might be happening?
Thanks,
Sachin
Based on the error it sounds like you need to create a login first on the SQL 2008 server that matches the same login/user that exists on the SQL 2000 server. You could do a couple of things:
create a new database in 2008 to migrate to
create a new login in 2008 that matches the existing login from sql 2000
map the new login to that new database from step #1
run the copy database wizard
If it was me, I would do as marc_s suggested and perform a full backup of the existing database on sql 2000, then restore to a blank database in sql 2008. If you can, update the compatibility mode in database properties to be 2008. I've done this hundreds of times and works like a charm.
The reason can be that a file with the new Database name already exist on the filesystem. We encountered this when we renamed Database X to X_Old, and tried to copy database Y to X. This cannot be done, because database X_Old is still associated with the filename X.
Either delete the conflicting database, or rename the file on the file system.
See http://codecopy.wordpress.com/2012/01/03/error-while-copying-a-database/

Create/restore database from backup SQL Server Express

I don't have SQL Server Management Studio on my machine.
I have a database backup (SQL Server 2008 R2). There is SQL Server Express that installed with Visual studio 2010 ultimate installed on my machine.
How can I restore this back up on a database and attaching it to SQL Server Express?
Is there any solution wihout using SQL Managment Studio Express ?
Even with SQL Server Management Studio Express, you won't be able to restore this backup. The Express edition being installed with Visual Studio 2010 is version 2008 of SQL Server - your backup is one from a SQL Server 2008 R2 release - those backups cannot be restore onto a 2008 version.
You will need to download the SQL Server 2008 R2 Express version, and while you're at it - get the version with the Management Studio! Install this, and then you'll be able to restore your database backup.
If you really really want to restore your database without using Mgmt Studio - you can of course use a RESTORE statement in T-SQL and run this using the sqlcmd command line tool:
RESTORE DATABASE YourDatabaseName
FROM DISK = N'(path to your BAK file)'
WITH FILE = 1,
MOVE N'(your DB name)' TO N'(your SQL path)database.MDF',
MOVE N'(your DB name)_LOG' TO N'(your SQL path)database_LOG.LDF',
NOUNLOAD,
REPLACE,
STATS = 10
GO
(and of course, there's also a BACKUP command which you can use in a similar fashion - the MSDN Books Online are your friend for details about the syntax!!).
Late one but hopefully useful to others who are viewing this….
If the problem is restoring without management studio this can be easily solved by using sqlcmd as marc_s already pointed out.
However if the real problem is restoring 2008 R2 backup on SQL 2008 without using SSMS then only solution is using third party tools that can read backup and generate scripts for creating schema and inserting data.
Idea is to create empty database on SQL 2008 and use a third party tool to compare it to backup generated with 2008 R2.
I’ve used ApexSQL Diff and ApexSQL Data Diff with good success in my previous job but there are also good tools from Red Gate (SQL Compare) and Idera (Comparison toolset). If you look up online you’ll probably find a lot more….
Disclaimer: I’m not affiliated with any of the companies mentioned above…
Why don't you get SQL Managment Studio Express? It is free, and should allow you to administer local sql express instances.
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796
BACKUP DATABASE [AdventureWorks] TO
DISK = N'\\nas\Backup\L40\SQL2005\AdventureWorks_backup_200702120215.bak'
WITH NOFORMAT, NOINIT, NAME = N'AdventureWorks-Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
RESTORE DATABASE [AdventureWorksNew]
FROM DISK = N'\\nas\Backup\L40\SQL2005\AdventureWorks_backup_200702120215.bak'
WITH FILE = 1,
MOVE N'AdventureWorks_Data' TO N'C:\Data\MSSQL.1\MSSQL\Data\AdventureWorksNew_Data.mdf',
MOVE N'AdventureWorks_Log' TO N'C:\Data\MSSQL.1\MSSQL\Data\AdventureWorksNew_Log.ldf',
NOUNLOAD, STATS = 10
Following link provides similar solution as marc_s, and it goes even deeper. I was able to successfully create a new DB from Backup file with the solution provided in the link below.
http://codeonaboat.wordpress.com/2012/02/16/sql-server-2008-creating-a-database-from-a-bak-file/
The solution below is copied from above link:
In SSMS, open a query window in the master database of the database server. That’s where you will run the following queries.
See what the “LogicalName” of the database that has been backed up in the .bak file is
RESTORE FILELISTONLY FROM DISK =
'c:\DatabaseBackups\dev2012021601.bak'
This will give you the logical name of the database and its associated log file. Lets assume that the names are “dbname” and “dbname_log”
Now run the following restore command. Make sure that a database with the name you are trying to create doesn’t already exist (in the code sample below, dbForSocialMigration doesn’t exist).
RESTORE DATABASE dbForSocialMigration
FROM DISK = 'c:\DatabaseBackups\dev20120307.bak'
WITH
MOVE 'dbname' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\dbForSocialMigration.mdf',
MOVE 'dbname_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\dbForSocialMigration_log.mdf'
C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA is the directory where SQL Express usually keeps its data files. You can find the directory your database server is using by selecting a database from it, right clicking and opening the properties dialog and selecting the “Files” option from the left.
That should work, and at that point you should be able to access the database “dbForSocialMigration” populated with all the tables and data from the .bak file.

How do I backup the data in SQL Server 2008 on a third party host?

I have a SQL Server 2008 database that is hosted by a third party host (heart internet).
How would I go about backing this up?
I used SQL Server Management Studio Express 2008 to create the tables within the database, but the backup options within this app seem to be only of use if you have direct access to the server machine (which I don't)
It's also worth noting that I am using change tracking - I presume this data would be lost should any backup be restored?
Thanks In Advance!
(PS - SQL Server 2008 novice here!)
If they allow you to run backups to a particular folder that you have access to you can just do it with the regular backup command:
backup database dbname to disk = 'y:\users\YourHomePath'
If they do not do that you might want to use the Database Publishing Wizard to script out your database (depending on the size this might be very slow)
You can create a .DAT file into a shared disk and to download it through FTP.

Backup MS SQL Server 2005?

I have a MS SQL Server 2005. It is very easy to backup individual databases, just right click on database then Tasks->Backup. My question is how to back up SQL server database objects outside the databases?
For example, under the Security->Logins, there are list of login users; under the SQL Server Agent->Jobs, there are list of jobs, and under the Server Objects->Linked Servers; ans so on.
Is there any way to do full-backup of SQL server? in TSQL? I tried to find out from SQL Server Management Studio but I could not find any.
Those items are stored in the system databases -- mostly master and msdb (under databases | System Databases). You can either back those up individually (like you do other databases) or, better yet, create a Maintenance plan (Management | Maintenance Plans) to do so on a regular schedule.
I recommend using this script. I spent much time looking after maint plan failures before it.
http://blog.ola.hallengren.com/
http://msdn.microsoft.com/en-us/library/ms187048.aspx
has more info on the standard procedure.
If you're responsible for these databases, I recommend to practice restoring the system databases on a new server, so when the time comes you are confident. The restore is not as simple as rightclicking restore.
database logins are stored in the user database, server logins are stored in master. msdb stores sql jobs and history. the simplest full backup you can do in T-SQL is:
BACKUP DATABASE dbname
TO DISK='C:\backupfile.bak' WITH INIT
WITH INIT means that it will overwrite the file.
Restoring the master database requires restart sql server in single user mode. You do this from the command line in the sql server directory with: sqlservr.exe -c - m
then connecting with SSMS or sqlcmd and run a restore.

Resources