Create db from db project - sql

Does anyone know how to create an entire database from a visual studio database project? Rather then running logs of smaller scripts individually.
Thanks

In VS express you do the following- It should be similar in the other versions but I don't have them in front of me.
Open Database Explorer (Ctrl-Alt-S)
Right Click "Data Connections" > "Add Connection"
Set 'Data Source' To Microsoft SQL Server Database File
Enter a name for your new Database
Choose your authentication method
It should confirm the creation of the database from there...
In Database Explorer Expand your Database and right click tables > Add new table and so on
It looks like I completely misunderstood the question. If you are trying to generate a script from an existing project for export the Database publishing wizard may do what you need.
http://www.microsoft.com/downloads/details.aspx?FamilyID=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

Are you using vs2008 team edition? If so use the db edition and you can use schema compare to create tables, along with stored procedures, views, etc.
http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&displaylang=en
The other way I have done is to publish the database, which will create a large sql file for schema and/or data, and then you can execute that in SQL Server Management Suite.

Related

Creating a local database from server database in visual studio

I have a rather large database I am working with and I am about ready to break something. To prevent this affecting live data, how would I use the live database to setup a local database? Not sure if this is even possible but I do know you can setup a local db.
You can create a SQL Server Data Tools database project type, then right click the project file and do an "Import..." to import the database to your local machine. Then you can deploy the local DB and it will be available in the SQL Server Object Explorer locally. This way you don't have to install SQL server on your machine - everything's in Visual Studio. Hopefully you are developing with a small set of data locally.
Answer
Use Visual Studio's Data Comparison tool to synchronize data to your target database from your source after you've created the database (schema only, no data) in your local database server.
Steps
From the Visual Studio's SQL Server Object Explorer:
A. Create the local database
Add two SQL Server Objects: One that connects to your production server and one that connects to a local (development/testing) server. If you need help setting up a local server then take a look at SQL Server LocalDB
Add a New database in your local server to receive the data (don't over think this step).
B. Migrate the Schema
Right-click the source (production) database and click Schema Compare...
From the SQlSchemaCompare tab that opens, use the Select Target dropdown to select your local database as the target.
From the SQlSchemaCompare tab, click Compare.
Uncheck everything in the comparison results except for the Tables, Views, and Procedures (unless you know what you're doing) then click Update.
C. Migrate the Data
Right-click the source (production) database and click Data Comparison...
Follow through the prompts to select the Tables to migrate then click Finish.
From the SQlDataCompare tab that opens, review the comparison results (it should make sense to you) then click Update Target
That's it! Either your local database is ready with data, or you confused your target/source and wiped out all of your data in production. Either way, you're done for the day.

Scripting Database schema

I have a database in my sql management studio how can script the database schema to file so that I can put it in another device?
thanks
Open SQL Management -> Script Database As -> .... I think its the closest to what you mean.
Of course there are 3rd party tools for these kind of works. See RedGate.
Shutdown SQL Server, copy the MDF and LDF Files to a new server and attach them on that instance by right clicking on Databases and choosing Attach. Specify the location of the files.
Alternatively, you could also backup the database from the source and restore it in the destination.
The above two methods copy all data too. If you want only the schema, Right click on the database and choose Generate SQL Scripts and follow the instructions.
SQL Management Studio 2008 can make query to unload Schema and data from database, but older versions if Management studio can't do it. You can use another soft to do it like an EMS SQL Management studio

How to create database project from existing SQL Server database?

By using VS TFS Database Edition 2008, how to create dbproject from an existing SQL SERVER database?
This as updated in VS 2010. There is now the option to "Import database objects and settings" from the project context menu.
In "Visual Studio" go to the "SQL Server Object Explorer" locate to database, right-click on the database you want and select "Create New Project ..."
There no direct, built-in support to do that, unfortunately.
You can get close by doing this:
in SQL Server Management Studio, go to the Object Explorer, right-click on the database you want, and pick Tasks > Generate Scripts
select all the database objects you want to script out
at the end, choose to generate the scripts into separate files for each db object
What you end up with are a number of .sql scripts in a folder of your choice, which basically represent your database.
Next:
create a Database Project in Visual Studio 2008
once you've done that, add the existing scripts to that project
It's a bit more involved that it should be - but that's the only way that I know of to achieve this right now. Don't know if VS2010 will support this in an easier way....

How to get script of SQL Server data? [duplicate]

This question already has answers here:
What is the best way to auto-generate INSERT statements for a SQL Server table?
(24 answers)
Closed 6 years ago.
I'm looking for a way to do something analogous to the MySQL dump from SQL Server. I need to be able to pick the tables and export the schema and the data (or I can export the schema via SQL Server Management Studio and export the data separately somehow).
I need this data to be able to turn around and go back into SQL Server so it needs to maintain GUIDs/uniqueidentifiers and other column types.
Does anyone know of a good tool for this?
From the SQL Server Management Studio you can right click on your database and select:
Tasks -> Generate Scripts
Then simply proceed through the wizard. Make sure to set 'Script Data' to TRUE when prompted to choose the script options.
SQL Server 2008 R2
Further reading:
Robert Burke: SQL Server 2005 - Scripting your Database
SQL Server Management Studio
This is your best tool for performing this task. You can generate a script that will build whichever tables you wish from a database as well as insert the data in those tables (as far as I know you have to export all of the data in the selected tables however).
To do this follow these steps:
Right-click on your database and select Tasks > Generate Scripts
In the Generate and Publish Scripts wizard, select the "Select specific database objects" option
Expand the "Tables" tree and select all of the tables you wish to export the scheme and data for, then click Next
In the next screen choose how you wish to save the script (the Output Type must remain set as "Save scripts to a specific location"), then click the Advanced button in the top right corner
In the newly opened window, under the General section is a setting called "Types of data to script", set this to "Scheme and data" and click OK
Click Next, review the export summary and click Next again. This will generate the script to your selected destination.
To restore your database, simply create a new database and change the first line of your generated script to USE [Your.New.Database.Name], then execute. Your new database will now have all of the tables and data you selected from the original database.
I had a hell of a time finding this option in SQL Management Studio 2012, but I finally found it. The option is hiding in the Advanced button in the screen below.
I always assumed this contained just assumed advanced options for File generation, since that's what it's next to, but it turns out someone at MS is just really bad at UI design in this case. HTH somebody who comes to this thread like I did.
If you want to script all table rows then
Go with Generate Scripts as described by Daniel Vassallo. You can’t go wrong here
Else
Use third party tools such as ApexSQL Script or SSMS Toolpack for more advanced scripting that includes some preprocessing, selective scripting and more.
Check out SSMS Tool Pack. It works in Management Studio 2005 and 2008. There is an option to generate insert statements which I've found helpful moving small amounts of data from one system to another.
With this option you will have to script out the DDL separately.
SqlPubWiz.exe (for me, it's in C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2>)
Run it with no arguments for a wizard. Give it arguments to run on commandline.
SqlPubWiz.exe script -C "<ConnectionString>" <OutputFile>
BCP can dump your data to a file and in SQL Server Management Studio, right click on the table, and select "script table as" then "create to", then "file..." and it will produce a complete table script.
BCP info
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=319
http://msdn.microsoft.com/en-us/library/aa174646%28SQL.80%29.aspx
I know this has been answered already, but I am here to offer a word of warning.
We recently received a database from a client that has a cyclical foreign key reference. The SQL Server script generator refuses to generate the data for databases with cyclical references.

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

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.