Scripting Database schema - sql

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

Related

Microsoft SQL Server 2005 backup the database in *.sql

I work in a small company which is running M/S SQL Server 2005
Now our head office is asking me to give the whole database backup with the table schema in a single file of *.sql
please help me to backup my database in a *.sql including the table schema.
Thanks in advance.
Use SQL Server Management Studio
right click on your database and choose Generate scripts... and hit Next
choose Script entire database and all database objects and hit Next
choose Save to file and enter a path and a file name for your future sql script file. On the same screen choose Advanced and change the Types of data to script property value from Schema only to Schema and data. Hit OK. Hit Next.
and hit Next again.
You can download, install, and use SQL Server Management Studio that comes free with Microsoft® SQL Server® 2012 Express for that
I would go out and download Microsoft SQL Server Management Studio Express.
http://www.microsoft.com/en-us/download/details.aspx?id=8961
It is free. You will be able to connect to the database, drill down into Databases, right click and under Tasks, pick Backup Database. Make sure you pick full...CHoose Disk as the place you want to write it to and Execute...Look thru your options as well...
Hope this helps!
I’d go with the method peterm suggested but note that this also has flaws. Problem is that SSMS doesn’t order the scripts in correct execution order.
For example, it might happen that DDL for stored procedure P is before DDL for table T that is used in P.
All you need to do is to review your script and make sure there are no such cases. If there are you can try fixing this yourself or using some third party tool to generate script that is ordered correctly.

Download an entire SQL Azure database as single file

Is there a facility in Azure to get a copy of the database? Or rather, detach the mdf and get it as file? On occasion I create a database in the cloud, it's up for a while, and then I want to take it down and archive it. My current rutine copies the database using SQL Azure Migration Wizard to a local Express instance, which I then detach and put in a safe place.
EDIT
Interestingly my method of choice throws an exception this time around. So it's far from ideal.
There is another way to do it:
"C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe" /Action:Export /SourceServerName:"tcp:xxxxx.database.windows.net,1433" /SourceDatabaseName:DbName /SourceUser:user /SourcePassword:password /TargetFile:C:\backups\backup.bacpac
From SQL Server Management Studio 2014, connect to the destination database server, right click its Databases node, and click Import Data-tier Application. In the import wizard, select the option to import from Windows Azure.
Using Sql Managment Studio (I use 2012):
Create a new local database.
Right-click in the database -> Tasks -> Import Data
Then you need to select the azure database as the data source and you new local database as data destination.
At this point, you can create a sql server backup or generate a sql script file to get a local copy at that moment.
I created the Enzo Backup for SQL Azure utility for that very reason. You can create a full backup and get your hands on a file that you can restore later to either another SQL Azure database, or a SQL Server database.
Note that SQL Azure will offer a form of backup, cloud-only, in the future. That's another good option. Finally Red-Gate has a product to copy a SQL Azure database to a local SQL Server database, but I am not sure that it gives you a "backup file" per say.
There is a RedGate tool that will backup your database to a local server http://www.red-gate.com/products/dba/sql-azure-backup/
I have found this useful before I do any database upgrades, in case bad stuff happens.
Since I've asked this question, the Azure management console added an option to export the entire database to blob storage. You can keep your backups there, but if you prefer a hardcopy there are many blob explorer tools such as this one.
A good free option I've been using for past few years is Sql Backup and Ftp. It's 100% free if you backup to your local laptop.
External storage requires paid license though.
From the website:
No more multi-step SSMS configuration, just a single form to automate backups: select databases, backup (full, diff, tran log), encrypt, compress, send to a folder, FTP or cloud service:
schedule backups, receive confirmation emails and restore when needed.

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.

Create db from db project

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.

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.