views:

1229

answers:

11

I'm in a situation where I would to generate a script for a database that I could run on another server and get a database identical to the original one, but without any of the data. In essence, I want to end up with a big create script that captures the database schema.

I am working in an environment that has SQL Server 2000 installed, and I am unable to install the 2005 client tools (in the event that they would help). I can't afford RedGate, but I really would like to have a database with identical schema on another server.

Any suggestions? Any simple .exe (no installation required) tools, tips, or T-SQL tricks would be much appreciated.

Update: The database I'm working with has 200+ tables and several foreign-key relationships and constraints, so manually scripting each table and pasting together the script is not a viable option. I'm looking for something better than this manual solution

Additional Update Unless I'm completely missing something, this is not a viable solution using the SQL 2000 tools. When I select the option to generate a create script on a database. I end up with a script that contains a CREATE DATABASE command, and creates none of the objects - the tables, the constraints, etc. SQL 2005's Management studio may handle the objects as well, but the database is in an environment where there is no way for me to connect an installation of Management Studio to it.

+8  A: 

Run Sql Server Management Studio, right click on the database and select Script Database as > Create to > file

That's for 2005. 2000 has a similar command. Just right-click on the database and you should see it in the menu.

EDIT: In 2005, you can select "Database" in the object explorer pane and select several databases in the details pane. Then, right-click on your selection and "Script Database as > Create to > file". This will cause it to put them all into one script and it will include all tables, keys, stored procedures, and constraints.

BoltBait
This answer is close, but wrong. I finally found the command.I needed to find the answer for the SQL 2000 tools. It is located with Enterprise Manager, by right clicking the database->All Tasks->Generate Scripts. Oddly, None of the create script options in query analyzer worked.
Michael Lang
A: 

The others are correct, but in order to create a full database from scratch, you need to create a 'device' in SQL before you run the create tables, and procedures scripts...

Use ADODB, since just about every (if not every) Windows box has it installed to execute the script.

Hell, you could even write a vbScript that executes to build your entire database. Any domain tables you have, you need to remember to turn on the identity insert before you add the items to the DB.

I'm open to source code sharing if you need it. I had to write the very same thing a couple years ago, and ended up with 3 scripts, one that created the device, then the tables, then the procedures/triggers and domain data. I wrote the code to parse the default script and allow the user to name his own database, and logins, etc.. You may not have to go that far.

Hope this helps at all.

LarryF
Not quite sure what you mean by 'create a device', database maybe?. Provided the SQL account has sufficient rights absolutely everything can be scripted.
Kev
+1  A: 

Michael Lang, when you right-click on the database and choose to create a script, there are several options boxes that you will need to click in order for everything to be generated. 2005 is much easier in this respect, but 2000 can do this, you just need to select the proper options.

BoltBait
+1  A: 

If you're looking for a command line version, Sql Server Hosting Toolkit has worked for me in the past.

Richard Nienaber
+3  A: 

So, I assuming that you cannot install SQL Server Management Studio. Not even the free version. If this is a onetime thing, I would install the red gate tools. The trial version is fully functional for 14 days. Do your work, then uninstall. You might also check out http://www.xsqlsoftware.com/, they have similar functions as Red Gate. If your database is small, then they have a free option for you. I also like http://www.apexsql.com/. Use the trial of one of these and then try again to convince your boss to buy one.

Bobby Ortiz
Thanks for the suggestion! Another hurdle I'd not mentioned is that the immediate problem I'm trying to resolve includes client hosted databases and an environment lock-down where I have to jump through serious hoops to install software. Very helpful and relevant suggestions!
Michael Lang
+2  A: 

I have used this program from CodeProject successfully many times before. Not only does it script out the schema, it can (optionally) script out all the INSERT statements you will need for recreating the database.

I've used it against SQL Server 2000 in the past. Obviously if you have a million rows in a table it might not be a good idea to script out the contents but it's a really neat tool actually to make a series of SQL scripts to replicate a database.

Schnapple
+1  A: 

Microsoft Database Publishing Wizard (in Visual Studio 2008).

leppie
+4  A: 

In addition to the above answers, I'd like to suggest that (for future projects, at least) you don't have you master database design in the database itself.

One way to achieve this is to either simply maintain your database tables, procedures etc as 'CREATE' scripts from day one, and devise a master script that will pull all of the individual scripts together for deployment to a database of your choosing.

A more sophisticated solution is to use something like Visual Studio Database Edition (Probably too pricey, if your comments are anything to go by) which allows you to treat each database object as a node in a project, whilst still allowing the whole thing to be deployed with a few clicks.

The explanation of both of these options is over-simplified, as there are a lot of other issues - versioning, data migration etc - but the main principle is the same.

Once you've extracted your script - using one of the other answers - you may want to consider using this script as the basis for your 'master'.

Just keep the 'design' out of the database - that's purely for 'implementations'.

Try to think of the process as similar to developing code - the source and the binaries are kept separate, with the latter being generated from the former.

belugabob
Excellent suggestions. I'd actually used that strategy once before on a project - checking in create scripts into VSS (had wished we were using something were you could check in versioned sets, but got around that a bit with labels) I wish the developers I'd inherited this from had done this.
Michael Lang
+1  A: 

As you mentioned in SQL Server 2000 the command to use is:

While in Enterprise Manager select the database you want to script objects from and then Right Click and select All Tasks -> Generate SQL Scripts...

While in the Options Pane it is handy to select option Create one file per object it that way you can store each object in source control separately.

So then whenever you do some updates on a tableA you can check it out in source control to let others know that you work on it and the after you finish you can script that single object and save check it in.

To script a single object you can you the same option All Tasks -> Generate SQL Scripts... and then select just that one object.

The only problem with that approach is when you want to restore the complete database you need to take care of dependent objects in the sense that the top level object must be restored before the ones dependent on them.

Ironically when you script the whole database to one file the objects are not ordered in terms of dependency but rather based on the creation date. That leads to errors when you try to use it to restore the complete DB.

We ended up creating batch file that would create each object separately calling "osql "

And that all worked pretty well at that time

Now we use SQLCompare Pro and it safes us from or that hassle, but if you do not make release frequently you can live without it.

kristof
Bummer - I thought I vaguely remembered an issue with dependencies. I'm guessing sorting those out is going to be not much fun as I've got 200+ tables. Thanks for the helpful reply
Michael Lang
A: 

If it is one-off operation and you do not fancy ordering the object scripts yourself just download a free trial version of RedGate SQL Compare.

It is 14 days full working version so it will do the entire job for you – it is completely legitimate solution. And you will have all the scripts for the future use with your manual scripting. Anyway I am pretty sure that if you find out how handy the tool is you will buy it in some later stage and that is what they probably hope for by offering fully working trial. It somehow worked in that way in my case.

Just be aware that once the trial expires it affects all the tools so make sure to make the most of it.

kristof
+2  A: 

You can do it on command line using SCPTXFR tool which is included in SQL Server 2000. Removing "CREATE DATABASE" at the beginning of a script is a separate and a much easier problem.

ssg