What is the best way to transfer a table or tables from one SQL server to another? - sql

I have been developing in VB.NET and SQL Server 2008 for a while now, but haven't got into live installs yet. In the database system I used be on it had the ability to archive multiple tables into a .dga file, as it was called. I could then restore the .dga file into another database or on another server.
I'm looking for the easiest way to accomplish something similar in SQL Server.

If you want to transfer specific tables, then using Data Transformation Services (right click on the database in SQL Server Management studio and select "Import Data" and it will bring the dialog up for it). Of course, this assumes that you have both databases available to you.
If you are comfortable with replacing the database as a whole, you can easily backup the database and then restore it into a new one through SQL Server Management studio (or through calling the appropriate SP).

I would go for one of the following :
From MS SQL Management Studio, right click on the database / Tasks / Generate scripts
From Visual Studio, in the Server Explorer tab, "publish to provider"
Both will launch a wizard allowing you to export the tables you want the way you want (including data or not, creation scripts or not, etc etc.)

If you want to move tabless without data, the simpliest thing is to script the tables you want and run the script.
We script all our db changes and commit them to subversion and then run them as part of the deplyment process.
If you want to put the whole database on prod including data (scrub out test records first!), then do a backup and restore onthe other server.
For future changes, wescript all our db changes and commit them to subversion and then run them as part of the deployment process. There also are tools that look at the structural differnces bewteen the two servers and creates scripts. REd-Gate's SQL Compare is really good for this.

In addition to HLGEM's suggestions, you can look into SSIS if this is an ongoing process.

Related

SQL to copy database and data on to a dev machine

I'm looking for a way to duplicate an existing database, tables and data onto a different development machine. Using SQL server 2008;
I have several tools i usually use for duplicating databases or i just restore a database dump, but i cannot use any of these. I have no direct access to the database machine i can logon via SQL enterprise manager and run queries stored procedures etc. But i have no access to the file system so i'm unable to create or copy a database dump. I cannot install or connect any tools to the database as i have to logon via a remote desktop. I also only have access to copy text files on and off the network.
I would like to run a script to generate a script to create the tables, views stored procedures etc and a script to populate the tables with some data.
I need to create a test environment so i can run tests and develop new features without affecting the live DB.
Assuming you have access to the database via SSMS, this is the approach I would take, though it could be time consuming:
Right click on the Database
Hover 'Tasks'
Hover 'Generate Scripts' and complete the process, it's self explanatory.
And from there it will generate a script for the Database architecture. This is the quick portion.
Next download Visual Studio and get the Integration Services add on. Using SSIS packages you'll be able to select all data from each source table/database and then insert the data into the destination table/database. Hope this helps you get on the right path.

Automating the Export Data Task

I have a database on one server that I need to copy to another server. I can do this manually using the Export Data task, which is fine for a one time export, but I would like to speed this up as it is going to be repeated.
The database will always contain the same set of tables, I just need to get a copy of this database with it's tables and their data from one server to another.
I'd like to create some sort of reusable tool that allows you to specify the source and target database servers and then copies this specific database from one to another. Is this possible?
The Export Data task in SQL 2005 and later uses SQL Server Integration Services (SSIS) under the hood. You can save the package you're already using and run it on a schedule or on demand. You can also edit it (once it is saved) using the Business Intelligence Development Studio (BIDS).
At the end of the Export wizard (on the "Save and Run Package" screen), you can tick the "Save SSIS Package" check-box to store the package either within SQL server or on the file system. The file system is probably simpler.
Once you have the package you can execute it from the command line using the dtexec tool, or from a SQL Agent job using an Execute SSIS task.
SSIS is too big a subject to cover in full here - there are decent tutorials within SQL server books online if you need more details - alternatively, as another SO question if you get stuck.

How to create SQL Server Express DB from SQL Server DB

I have a SQL Server 2008 DB. I want to extract SOME tables (and associated schema, constraints, indexes, etc) and create a SQL Server Express DB. It isn't a sync of the target, we stomp on it.
We ONLY need to do this in the file system (not across the wire). We are not fond of the synchronization stuff and at this point don't know how to run SSIS. We are a C# shop and a little code is ok. Like using the C# bulk import stuff, but that won't create the schema.
Suggestions?
My suggestion:
Back up the database
Restore under new name and file
Detach restored database from SQL Server
You now have a standalone file that you could use with SQL Server Express.
We use a tool from Red-Gate called SQL Compare to generate schema-complete SQL scripts. It's about $400, but well worth it. You pick the objects you want (users, tables, views, functions - whatever) you want, and it will generate a SQL Script to re-create them in your new database. Essentially, it's the same as Right-Click -> "Script To... New Window" in SSMS, but all at once, and it has a number of other features your shop might find useful as well.
As Scott pointed out (I couldn't figure out how to comment on his post), you can do a backup and restore, detach and attach from one server version to another assuming that the database is less than 4GB.

Is it possible to restore Sql Server 2008 backup in sql server 2005

Is it possible to restore a backup of a SQL Server 2008 database onto an instance of SQL Server 2005?
I need to work on an sample application for which database backup is in sql server 2008.
But I'll not be able to install 2008. So is it possible to restore that back up in 2005?
No. It is not possible to restore a database from a backup of a newer version.
If you are dead set on it, I think your best option is to selet the database in the Object Explorer in SQL 2008,
right-click, select Tasks->Generate Scripts.In the options dialog emable about everything, including Script Data.
And make sure you select "Script for SQL 2005".
Source
When importing the objects into your target server, if the objects are large you may find that you can't open the SQL file via Management Studio (with a completely useless "The operation could not be completed" error, no less). That's okay, just load the file via sqlcmd.
One important thing is missing in all answers and that is the fact that Generate Scripts in SSMS doesn’t order the scripts correctly.
Scripts have to be ordered in the correct dependency order so that child tables are created after parent tables and such.
This is not an issue for small databases where its easy to reorder the scripts manually but it can be a huge issue when dealing with databases that have 100+ objects.
My experience is that its most convenient to use third party tools that can read backup and generate scripts in the correct order. I’m using ApexSQL Diff and Data Diff from ApexSQL but you can’t go wrong with any popular vendor.
No, not directly. SQL Server 2008 database backups are not backward compatible with SQL Server 2005. However, with SQL Server 2008 Management Studio, you can script data and schemas in SQL Server 2005 mode. This article describes the process in detail.
Yes it is possible
Using the export in the SQL Server 2008. Go to All Programs --> Microsoft SQL Server 2008 --> Import and Export Data
Then SQL Server Import AND Export Wizard window will be opened. Press Next
Choose a Data source (in your case from SQL Server 2008). Choose a Destination( in your case from SQL Server 2005).
Select Copy data from one or more tables or view
Select the source's tables and destination's tables
Click Next & Finish
to complete.
I have had this problem for a long while.
You cannot restore SQL2008 backups onto an SQL2005 instance.
And for me, workarounds like import/export wizzard or to script the database from SQL2008 using the generate scripts with the for SQL2005 option won't work.
Because simply, my databases cross-reference each other inside their views and stored procedures or udfs. They do not befall to my responsibility completely and so I cannot consolidate them into 1 database.
They are a set of 6 dbs that refer to each other directly inside their views and stored procedures.
When I transfer them from one SQL2005 instance onto another, I usually do full-backup/restore.
If I were to script them, even with the with dependencies option I would get errors at re-creation time as db1 will not find views inside db3 because it so happened that I executed the create db1 script first. If I tried db3 first I get similar exceptions.
The only way to script them so that I won't have such dependency exceptions, is to figure out a sequence that works and script them partially in that manner: say: db1_tables followed by db2_tables followed by db2_views followed by db1_views, sp, udfs etc.
I have such a sequence. And when I need to create a new set of such 6 dbs, I executed the smaller partial scripts in sequence.
This explains why the generate scripts, with dependencies and with data and set to SQL2005 version scripts, will just not work for me.
The import/export wizzard is better in my case because it will copy tables and then you still have to script all views, sp, udfs etc.
What I really need is a conversion tool for SQL2008 backup files, to convert them to SQL2005 format. Then my problem will go away.
Or some kind of a tool that would allow restore from SQL2008 full-backup files, without asking me too many questions.
If anyone knows such tools and have used them, let me know.
You can use DBSave, it's a great freeware tool to backup and restore ms sql server on different machines.
It's verry simple to setup and to use.
No you can't, but tools like red gate's SQL Compare/Data Compare can read backup files directly & transfer the info across to a live database, dealing with any syntax or settings that aren't compatible on SS2005
Having had no luck with the Import/Export stuff (flat file exports failed on import claiming charset mapping issues [even though same charset used throughout] and/or truncation issues [even though source and destination had exact same structure]), and having had no luck with using the "generate scripts" option suggested by Garry Shutler (it generated a script with syntax errors), I was finally able to copy the big table I wanted to copy from 2008 to 2005 using the SQL Server bcp utility. So that's another option for this situation, although for an entire database it would be table-by-table and probably doesn't help with views and such.
The steps I used:
On the source server, use "Script Table As...CREATE" to get the structure, run that on the target server.
On the target server, create a bcp format file using your newly-created table:
bcp database.owner.table format nul -f table.fmt -n
(If you're not using Windows auth, you may need the -U and -P options to specify username and password.)
Copy that format file to the source server (if necessary).
Export the data to file on the source server:
bcp database.owner.table out table.dat -f table.fmt
(Again, possibly with -U and -P.)
Copy the data file to the target server (if necessary).
Import the data on the target server:
bcp database.owner.table in table.dat -f table.fmt
(Again, possibly with -U and -P.)
In hopes that proves useful to someone else.

How do I backup database from ServerGrid.com?

I have a hosting account with servergrid.com. I want to backup my database, they say I have to use Sql Server Integration Service to backup the database and I would need a commercial version of Sql Server management studio.
I have Sql Server 2005 Developer Edition. I have no idea how to do SSIS backup. I tried playing around with the Sql Server Integration Services Project in VS2005 .. but I failed.
Google also seems to have no step-by-step guide for Non-DBA's like me.
Can anybody point me to/write me a Step-by-Step instructions on how to backup database using SSIS? and also if possible how to restore the database again to the host
SSIS is not really a database backup tool.
If you have a fixed number of tables, you can export the data from SQL Server to comma-separated or raw files - i.e. do the data export using SSIS. It is easy to create such package using SSIS project if you have small number of tables. But if you have a lot of tables it is all manual process. Also it could be hard to automate this - every time you add/remove/change a table or a column, you have to regenerate the SSIS package used to export your data.
SSIS does only data export, so you need a separate tool to do metadata backup (table and stored proc definitions). You can export the metadata using Management Studio (simply script database to text file).
One can call these two tools together a poor man backup tool, but if you metadata changes often you'll probably want to use the real SQL backup tool. Not sure why the hosting guys say you have to use SSIS - maybe they don't want to give you backup permissions to ensure DB safety, but SSIS is not the simplest way to do database backup.
You have a local drive on this machine, correct?
Get access to the sql prompt, through their tools or sqlcmd.
BACKUP DATABASE MYDB TO DISK = 'E:\backups\mydb.bak'
Then FTP that file offsite.
Otherwise, forget these creeps that make you use SSIS to perform tasks that can be done with one line of text.