Microsoft SQL Server 2005 backup the database in *.sql - 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.

Related

How to create schema only from SQL Server 2012 backup file

I have a backup file which is over 150gb. I am trying to create a new database from a backup. But it failed with the following exception:
Create Database or Alter database failed because the resulting cumalative database size
would exceed your licensed limit of 10240 MB per database
As there is restriction of max 10 GB for SQL Server Express, is there any way to create only the schema (stored procedures, functions etc) from the backup file, and not the data.
No, there is no "schema-only" restore. But you have several other options:
Download and install an instance of Evaluation Edition, restore your database there, and extract the things you need. Then uninstall the instance of Evaluation Edition.
Buy Developer Edition and use that, permanently, instead of Express. You should be able to find this at your favorite software retailer. Amazon has it for $43.99; Microsoft has it too but they charge full price ($59.95).
Try out some of these 3rd party tools that let you "attach" a backup as if it were a database (Red Gate has one; Quest may also have similar functionality with LiteSpeed). With Express Edition though it may be subject to the same size limitation, I haven't tried, and I'm not sure if the trial editions of those products have any limitations either.
To create a database with an empty schema I can suggest two options:
1) In SSMS, right click on the database in the Object Explorer, go to Tasks/Generate Scripts and run through the wizard to generate a sql script to create a new database with the same schema.
2) Use Red Gate SQL Compare, put the database in the left hand side data source, and select 'Create Database' on the right hand side of the comparison. Run through the comparison and Deployment Wizard to create a new database based on the schema of the source database.
I know this is an older question, but it popped as the first result in a search for me. SQL Server Developer edition is now free through Visual Studio Dev Essentials. You can download 2017 Developer here for all your large database needs!

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 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.

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.

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.