How to copy data and database tables from one database to another - sql-server

Is there any way that I can copy all the data and database tables (and other objects) from one server table to other server?
To make those things as automated. so that every time we used to get that production data to my local database.
Thanks

You could also try out ApexSQL Script, a database migration tool, to script objects and data from one instance and execute it on another instance.
Also you can use ApexSQL Diff and ApexSQL Data Diff, SQL schema and data compare tools, to copy structure and data from a source to destination.
Disclaimer: I work for ApexSQL
Hope this helps

You could:
backup and restore the database
detach and re-attach the database
use tools like Red-Gate SQL Compare for structural and Red-Gate SQL Data Compare for data comparison and syncing
you could roll your own, using SQL statements and/or SMO (SQL Server Management Objects)
probably a gazillion other ways to do this.....

You can use SSIS import and export to do that.
It asks for the source server name, source dbName and Destination server name, destination dbName.
OR
you can use the back-up and restore process.
But I always prefer SSIS.

Alright, I found it, completely in visual studio. This can be from local (.mdf) files to server files and visa versa.
Go to Sql Server Object Explorer and find your source database. Or in Server Explorer, right click on your source database and click on Browse in Sql Server Object Explorer
When in Sql Server Object Explorer, right click on your source database and click on Data Comparison
A dialog will popup, showing you a Source Database and Target Database. Your Source Database should already be selected. Now select your Target Database, you probably have to add a new connection, just do it like you would usually do in Server Explorer. If you want the target database to be a local (.mdf) file and you cannot click on Change, than add it first to the server explorer and it will popup in the combobox.
Choose what you want to compare by checking the checkboxes and click next.
It will connect to the servers and show you another dialog what to compare, check the checkboxes and click on Next.
Here you will have an overview of your changes from source to target.
At this point, you will be able to kind of 'mirror' the source database with the target database. For instance: If you have added more records to the Target database then there are in the source database, then these records will be deleted once you click Update Target Take a close look at what will happen when you hit that button!
If you are satisfied with the result, click Update Target. If you are not satisfied, next to the button there is a little icon to export it to a .sql file, so you can modify what will happen to the target database.
Unfortunately, after writing this, I found out that this is only available in VS Ultimate and Premium, that is what they say on the following link about visual studio 2010, I have tested this in Visual Studio 2015 Enterprise.
MSDN: https://msdn.microsoft.com/en-us/library/aa833428(v=vs.100).aspx

I suggest you to take a look at "Visual Studio 2010 SQL Server Database Project". It provides you with great features to manage database team work. It allows you to create a local database and import the production database into the local database. From now on, you and your team can work on local database and when you are ready to deploy your changes to production database, Visual Studio 2010 synchronizes the local database with production database, So the production database will get the latest changes.

you can create a DTS job to copy database and its contents on timely basis.
http://msdn.microsoft.com/en-us/library/cc917688.aspx

Related

Duplicate localDB under SQL Servers on my laptop

I've been running into an issue recently when I attempt any tutorials that involve using a SQL database, entity framework, dapper, etc.
When it comes time to publish a database, or utilize an ORM, I'm given duplicate options for the same localdb under SQL Servers. Furthermore, then I attempt to publish, the database doesn't show up under the localdb that I've chosen.
I'm wondering how I go about removing the other SQL Servers and just having the one available.
If you look at the image below, the Browse option gives me two of the same LocalDbs. Plus I also get a 3rd one under \ProjectModels. I'm wondering what's causing this and how it can be fixed since no matter which one I choose, the sql database I attempt to publish doesn't show up within any of them.
My advice is not to use this method to publish the database. (right click to delete)
Please refer to this official documentation.
File-based databases like SQLite or SQL Server Express are designed to store their data in easily transferable files that can be served with your application/site.
"Copy to Output Directory" Property of the database file to "Copy if newer". Just point the address to it.
If you are using a server-based database like SQL Server, MySQL, etc., you need to make sure that the target machine/environment has the same database server installed, and you need to write a deployment script to append the pre-populated data files to the server. This might be troublesome for you.
You can also refer to these links. 1,2,3

DB Designer in Visual Studio 2010

I need to create an entirely new Sql Server 2008 database and want to use a Database Project in Visual Studio 2010 (Ultimate). I've created the project and added a table under the dbo schema.
The table .sql is shown only as plain text, though with colors. It has no designer, no Add Column, and no autocomplete. Existing column's properties are grayed out.
Usually, I use DB Project for nothing more than storing .sql files for source control purposes, but I'm assuming it can help me with designing the DB. Currently, it offers no such help and I think it's because I'm doing something wrong. Perhaps I need to deploy the DB to server first, or something of the such. I've looked for a Getting Started guide, but all guides I found start from importing an existing database.
Please help my understand what a DB Project can do for me and how.
Thanks,
Asaf
The whole idea of the VSTS DB is to get you set on the right path, ie. store database object definitions as .sql files, not as some fancy diagram. Any modification you do to the objects you do it by modifying the SQL definition. This way you get to do any modification to the objects, as permitted by the DDL syntax, as opposed to whatever the visual-designer-du-jour thinks you can and can't do. Not to mention the plethora of SQL code generation bugs associated with all designers out there.
The closes to a visual view is the Schema View, which shows tables, columns, indexes etc in a tree view and you can see the properties from there.
By focusing the development process and the Visual Studio project on the .sql source files, teams can cooperate on the database design using tried and tested source control methods (check-out/check-in, lock file, conflict detection and merge integration, branching etc).
the deliverable of a VSTS DB project is a the .dbschema file, which can be deployed on any server via the vsdbcmd tool. This is an intelligent deployment that does a a schema synchronization (merge of new object, modifies existing ones) and can detect and prevent data loss during deployment. By contrast, the 'classical' way of doing it (from VS Server eExplorer, or from SSMS) the deliverable was the MDF file itself, the database. This poses huge problems at deployment. The deployment of v1 is really smooth (just copy the MDF, done), but as soon as you want to release v1.1 you're stuck: you have a new MDF, but the production is running on its own MDF and does not want to replace it with yours, since it means data loss. Now you turn around and wish you have some sort of database schema version deployment story, and this is what VSTS DB does for you from day 0.
You might be better off downloading the SQL Server Management Studio for SQL Server 2008 Express - http://www.microsoft.com/downloads/details.aspx?FamilyId=C243A5AE-4BD1-4E3D-94B8-5A0F62BF7796
Using this tool you can create your database using the visual tools provided by that software. You can run your .sql script to build up the database and then visually adjust columns settings, table relationships, etc.
Once you have your database designed open up Visual Studio and open a connection to this database using the Server Explorer.
Visual Studio is ok for simple tweaks and changes to an existing database structure but for anything serious like making the database from scratch I would recommend using the Management Studio. It's free and built for that exact purpose :)

How do you transfer the data from a CRM 4.0 database into another CRM 4.0 database?

Our client wants to transfer all the data in the Production CRM 4.0 database and use it in UAT. What is the best way to transfer the data over.
I would copy the database. Then use the Deployment Manager tool to import the organization (this will update the MSCRM_CONFIG database, etc.). You may have to be careful with things like running workflows - as last I recall these might save stuff like Organization Name in their serialized state.
Open SQL Server Management Studio
Right click on your source database and select Tasks/Back Up
Update the Destination to your desired location and click OK
Right click on the empty target database and choose Tasks/Restore Database
Select from Device and choose the file name for your backup, then click OK
Another option is to use scribe. this is considered the industry standard tool for moving CRM data around.
http://www.scribesoft.com/

Can I integrate SourceSafe / VSS with SQL Server 2005?

How are the SQL objects managed with VSS?
Can I integrate SourceSafe / VSS with SQL Server 2005?
I want versioning in my SQL schemas.
Aside from VSS being a total train wreck, if you want to store your schema in source control, one possible approach is to store the object creation script for each database object as a separate file in source control. That way, you can update individual parts of the schema easily. So you have a script for each table, trigger, index, procedure etc. As part of your build process these can be pulled together into a single script for ease of application to the target database.
Utilize tools like SQL Compare from Red Gate which will allow you to create schema, object per file, and then maintain the state of the files with VSS. I am currently trying to push that in my organization.
We've just added Visual Source Safe support to SQL Source Control, providing source control integrated into SQL Server Management Studio. This is in early access at the moment so please sign up for the early access build and tell us what you think.
More detail is available here:
http://www.red-gate.com/MessageBoard/viewtopic.php?t=12265

mysqldump equivalent for SQL Server

Is there an equivalent schema & data export/dumping tool for SQL Server as there is for MySQL with mysqldump. Trying to relocate a legacy ASP site and I am way out of happy place with working on a windows server.
Note: The DTS export utility own seems to export data, without table defs.
Using the Enterprise Manager and exporting the db gets closer with exporting the schema & data... but still misses stored procedures.
Basically looking for a one does it all solution that grabs everything I need at once.
To do this really easily with SQL Server 2008 Management Studio:
1.) Right click on the database (not the table) and select Tasks -> Generate Scripts
2.) Click Next on the first page
3.) If you want to copy the whole database, just click next. If you want to copy specific tables, click on "Select Specific Database Objects", select the tables you want, and then click next.
4.) "Save to File" should be selected. IMPORTANT: Click the Advanced button next to "Save to File", find "Types of data to script", and change "Schema only" to "Schema and data" (if you want to create the table) or "Data only" (if you're copying data to an existing table).
5.) Click through the rest and you're done! It will save as a .sql file.
The easiest way is the sql server database publishing wizard.
Open source
Free
Does exactly what you want
Developed by microsoft
It does not have all the features of mysqldump but it is close enough.
http://www.codeplex.com/sqlhost/wiki/view.aspx?title=database%20publishing%20wizard
The easiest way to move a Database would be to use SQL Server Management Studio to Export the database to another server, or if that doesn't work, make a backup like other's had suggested and restore it elsewhere.
If you are looking for a way to dump the table structure to SQL as well as create insert scripts for the data a good free option would be to use amScript and amInsert from http://www.asql.biz/en/Download2005.aspx.
If you want a good pay version I would check out Red-Gate SQL Compare and Red-Gate SQL-Data Compare. These tools are probably overkill though and probably a bit pricey if you don't intend to use them a lot. I would think it would mostly be relegated to DBAs. You can look at the Red-Gate tools at http://www.red-gate.com/.
Not finding the right tool, I decided to create my own: a sqlserverdump command line utility. Check it out on github.
Even easier is to use the SMO API. It lets you do exactly like mysqldump, and even better. Here is a code example:
http://samyem.blogspot.com/2010/01/automate-sql-dumps-for-sqlserver.html
easiest would be a backup and restore or detach and attach
or script out all the tables and BCP out the data then BCP in the data on the new server
or use DTS/SSIS to do this
SQL Enterprise manager or SQL Server Management studio have wizard based approaches, and the latter will generate the scripts so you can see how its done.
You could also use the BACKUP and RESTORE commands. More detail here: http://msdn.microsoft.com/en-us/library/ms189826.aspx
If you can get DTS or Integration Services to connect to both servers, you can use the wizards to 'copy objects' from one server to another. 'Copy Database' requires that the two servers can authenticate with each other, which typically means being on the same domain and that the service runs under a domain logon.
Otherwise, you can generate a script for the schema, and you can use an Integration Services/DTS package to export data to a file, then import it on the other.
We now generally use SQL Compare and SQL Data Compare. Red Gate's SQL Packager might also be an option.
Well, Mysqldump is a series of SQL statements. You can do this with DTS, but why not just create a backup and restore it on your new machine?
If you want to do it via SQL:
http://msdn.microsoft.com/en-us/library/aa225964(SQL.80).aspx
Or just right click the DB and hit Tasks -> Backup (http://msdn.microsoft.com/en-us/library/ms187510.aspx)
Two things a backup/restore won't do:
Get off of a Microsoft server, which
was part of the original question
Help quickly find a structural difference
between two DBs that are supposed to
have the same structure when one of
them is running slowly. Unix diff,
or sdiff, ignoring white space but
need a way to make input files.
If you need equivalent SQL statements like CREATE TABLE... & INSERT INTO..., then I recommend you try HeidiSQL. It's a fantastic and free utility that can access Microsoft SQL Server, MySQL and PostgreSQL. It enables you to browse and edit data, create and edit tables, views, procedures, triggers and scheduled events. Also, you can export structure and data to SQL file.
http://www.heidisql.com
Go to Tools / Export database as SQL and select the schema.
Check the box to create the tables and “Insert” data. That’s it.
I prefer HeidiSQL to "Microsoft SQL Server Management Studio" or phpMyAdmin... etc.

Resources