Printing SQL Server Database Schema

I’ve been working with SQL Server 2000 for about 2 months now, and it completely frustrates me that I cannot print table schema from Microsoft’s Enterprise Manager. I’m one of those people who like to have printouts of the table’s I’m working with as it makes it easier for me to generate my SQL statements.

Doing some searching on Google, I found a couple of tools that will print schema, but they wanted at least $59US! So I decided to write my own script and offer it up for free.

The script uses ASP classic, so it will need to run on an IIS server. You will need to modify the script and specify data source/ip address of your SQL Server, choose an Initial Catalog/Database”, and supply a *username and password. When you first run the script in your web browser, you will see a navigation bar along the left side of the page that lists the tables for your selected database. When you click on a table name, the schema for that table will appear. The action of clicking on a table name invokes an xmlhttprequest object to display the table schema. This means that the table list is not being regenerated with each request. It also means that you can click on multiple tables to stack them up! To remove a table from the display, simple click on the table name and it is removed from the DOM.

The Hide Table List link that appears above the list of tables is simply there to hide the table navigation bar when you want to print your table schema. Once you hide the list, you must refresh the browser to bring it back.

So, here is the file. Help yourself and I look forward to any feedback!

Share and Enjoy:
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • RSS
  • Technorati
  • LinkedIn

44 Responses to “Printing SQL Server Database Schema”

  1. Webdaddy said on February 25th, 2005 at 11:01 am

    Now if I could only find a way to use a schema.ini file to create tables in SQL Server 2000…

  2. Craig Rosenblum said on February 25th, 2005 at 12:35 pm

    You need to learn how to do ERDs, short for entity relationship diagrams, which has all info you want.

    Primary Key’s, Foreign Key Relationships.

    Using MS Visio or a number of other tools, to reverse engineer your database structure into a very printable diagram. Depending on how big a printout you want. I usually want a huge colored one, right above my desk..

    There may be freeware or shareware ones out there…But I tend to use Visio the most myself. Because it will draw out the table structures itself. Just make sure to create the relationships that you want to show up in your diagram in the tables, and then it will display that as well..

    Good luck!

  3. moko said on February 25th, 2005 at 2:36 pm

    Good work! One minor fix: line 180 of index.asp should be:
    sOut += ';

    Also, I’m unable to get more than 1 page to print from Firefox 1.0.1. And IE 6.0 messes up the formatting of “Table Schema for…” headings.

  4. moko said on February 25th, 2005 at 2:47 pm

    My suggested code fix above was broken during the parse. Line 180 contains an opening unordered list tag, but it should be a closing unordered list tag.

  5. Matt W said on February 25th, 2005 at 3:00 pm

    I had always just copied all the rows in Design view and pasted into Excel. Resize a couple of columns and print away.

  6. Jim Rutherford said on February 25th, 2005 at 3:32 pm

    Thanks for pointing out the markup error! Note to self – always validate before publishing!

    As for the printing – I’ll look into it!

  7. waihing said on February 26th, 2005 at 5:46 am

    You can actually view the database schema with the data type and length from SQL Server diagrams… :

    1. Create new diagrams fr SQL Server Manager
    2. Add the relevant tables
    3. Right click on the table diagram view, under “Table View -> Modify Custom …” menu, just add the custom columns (default value, length, precision, scale etc) you want

  8. jim lofton said on March 2nd, 2005 at 5:17 pm

    Maybe this does not do exactly what you want… but it’s what I’ve used…

    In Enterprise Manager:

    1) Right click on the database you want to work with

    2) Select All Tasks -> Generate SQL Script…

    3) Click on the Show All button

    4) Select the options you want

    5) Click preview and copy to the clipboard or go through the save process

    You have the schema formatted as an SQL script ready to use or print

  9. Jim Rutherford said on March 11th, 2005 at 1:41 pm

    The printing problem has been fixed. Looks like IE and Firefox have problems printing absolutely positioned elements.

  10. Steve Turner said on June 3rd, 2005 at 11:13 am

    Just what I was looking for. Simple, readable, free. In response to some of the comments up above, diagrams/ERDs serve a different purpose, and expanding, copying and pasting 30 or more tables becomes tiresome. Thanks for putting this script together and making it available.

  11. TixXio said on November 28th, 2005 at 12:37 pm

    Link for source is broken

  12. sathya kothandram said on December 16th, 2005 at 7:10 am

    I could use something like this. The link to the file, however, is broken. Could this be fixed and the file made available?

  13. Tim Meade said on December 16th, 2005 at 8:56 am

    Link is stil broken! This sounds perfect for what I was looking for. Can someone please email me when fixed?

  14. Jim said on December 16th, 2005 at 4:31 pm

    Link is fixed - enjoy!

  15. Willio said on February 14th, 2006 at 7:36 am

    I don’t want to see my tables. I made a diagram with the manager of SQL Server 2000. I would like to plot that on a huge paper, because there are about 15 tabels with different relations.

    Willio

  16. FeedTheNetwork said on February 22nd, 2006 at 10:33 am

    After reading the comments, I wasn’t sure that your code would be worth my time.

    However, I implemented it anyhow, and I must say that I do find it useful.

    Thanks!

  17. Mr. Malaysian said on March 17th, 2006 at 11:47 am

    I’m just happy that you took the time to write the asp. Served my purposes perfectly. Thank you very much.

  18. Nick said on April 6th, 2006 at 12:00 pm

    This was great. Thank you.

  19. jsperkins said on May 10th, 2006 at 1:10 pm

    This was (is!) a huge time saver for me. Very, very convenient when I’m working remotely and need to refresh my memory of some table’s schema.

    Thanks so much.

  20. renfred said on June 11th, 2006 at 8:28 pm

    perfect site good information, very nice news and etc… tnx

  21. Joginder Nahil said on June 14th, 2006 at 2:28 pm

    I am the owner of the product, SQL2000Print, thats is mentioned in the opening paragraph ( …but they wanted $59). I would like to say that the product is worth $59. It prints almost ALL the objects (Tables, Views, Stored Procedures, Logins … + all their permissions and dependencies etc. ) in the Database

  22. Newbian said on June 15th, 2006 at 8:39 am

    Absolutely simple to implement (even for a novice) and the script returns much better formatting than the “generate script” option in EM. Served my purpose very well. Thanks for your contribution.

  23. werds said on June 23rd, 2006 at 5:55 am

    Useful script and good example of XMLHttp !!

  24. Sino said on June 27th, 2006 at 8:04 am

    Excellent piece of code! Thanks.

  25. Colfusion Developer said on July 22nd, 2006 at 8:34 am

    I spent the last 1/2 hour trying to find a suitable solution, since I am using a shared hosting account that doesn’t give me access to Database Diagrams. Your solution was exactly what I was looking for and took me only a minute to set up. I’m using SQL 2005. Thanks for taking the time!

  26. Jason said on July 27th, 2006 at 8:01 am

    The code is almost as useful as the output! Nice to see XMLHttp in action.

    Thanks for sharing, it was exactly what I was looking for.

  27. Ba said on August 7th, 2006 at 10:50 am

    Please please tell me how I would print a table using sql .. ie the contents of a table. Thanks

  28. Jon said on August 14th, 2006 at 1:52 pm

    All I seem to get is Page cannot be displayed? Any ideas?

  29. Charles A. Adewoye said on August 18th, 2006 at 4:54 am

    Good job!, very helpfull!!.
    Cheers

  30. Dave Carnes said on August 18th, 2006 at 7:36 am

    Very Very Nice.. Thanks.

  31. Texrat said on October 5th, 2006 at 9:25 am

    VERY helpful– many thanks!

    Now if MS would just build this simple need directly into their apps…

  32. Charles Harford said on November 30th, 2006 at 12:58 am

    Fantastic! Nice and simple to use and does exactly what it says on the tin.
    Many thanks.
    To make it work for me I had to comment out line 4:

    // sConn += “Network Library=DBMSSOCN;”;

  33. Jim Morrison said on December 7th, 2006 at 11:10 am

    Simply perfect!…Got it up and running in seconds. Thank you very much.

  34. Tom Kessler said on December 16th, 2006 at 7:49 pm

    Very new to web based applications and SQL 2005 and facing a major deadline. Boy did this help!!! Thanks for posting this.

  35. Heather Walker said on January 11th, 2007 at 2:12 pm

    this is awesome!!! Quick and dirty - love it! Thanks

  36. Mark Chadwick said on March 2nd, 2007 at 9:15 am

    Thanks so much Jim! Simple, clean, exactly what I needed!

  37. Rog said on June 1st, 2007 at 2:46 pm

    Very nice job. Does exactly what it says and is easy to implement.

    Keep up the good work.

  38. Sean Chang said on July 20th, 2007 at 11:43 am

    Good job for the poor M$SQL.

  39. Elizeu said on July 31st, 2007 at 11:26 pm
  40. Sara said on September 5th, 2007 at 4:54 am

    Thanks for this, I am a sql server newbie and this took seconds to set up.

  41. I NEED HELP said on January 18th, 2008 at 9:58 am

    How do you run this ?

  42. Jaime said on February 1st, 2008 at 9:33 am

    @I NEED HELP

    This is a web page written in asp/javascript. In order to run this, you need to setup your OS with IIS (to enable your computer to act as a web server.. Typically Windows 2000, Windows XP Pro, Vista Business or Ultimate) or have a windows server running IIS. It needs to be renamed from index.asp.txt to index.asp.

    It takes a few seconds to implement this and if you arent a web person, google setting up a personal web server or ask someone that has access to a Windows web server.

    Jim,tThanks for a great and simple app, does the job perfectly!

  43. Maxx said on April 25th, 2008 at 2:31 am

    Thanks… just saved 59$

  44. ATUL TIWARI said on May 10th, 2008 at 12:12 am

    hi how am i know about the schema of table or database and could i know the procedure of making DFD through SQL.