I want to retrieve a script for a database that will include operations of creating database, creating tables, and insert all data. How can I achieve this using management studio 2008 please.

I have tried the generate script option , but that seems to create only table structure, doesn't create database or doesn't insert data either.


migrated from stackoverflow.com Feb 2 '11 at 19:10

This question came from our site for professional and enthusiast programmers.

  • 2
    If you are using a GUI, it is most definitely not programming. – leppie Feb 1 '11 at 7:38
  • @leppie: absolutely true, but most programmers have to get their hands dirty with databases. :) I'd put this in the same bracket as asking how to get rid of warnings in visual studio – JumpingJezza Feb 2 '11 at 1:17

There are to separate steps:

  1. Use the Script Database as / CREATE To / ... operation to generate script for the database.

  2. Use the Tasks / Generate Scripts... operation to generate script for the objects inside the database.
    Make sure to set the Script Data option to True to generate INSERT statements along with tables structure.

    enter image description here

  • hi thanks, I got how to extract data to script. Now, can you please tell me, how can I set option so that the script generates the db if not exist? Currently it has query with "USE DBName" , but it won't work if the database doesn't exist on the server. – Rana Feb 1 '11 at 7:49
  • @Rana Use IF DB_ID('database_name') IS NULL to determine if the database is missing and use the script generated with Script Database as / CREATE To / ... to create with. – Marek Grzenkowicz Feb 1 '11 at 7:55
  • so, there is not direct option without doing it manually in the sql script? Anyway, Thanks a lot. – Rana Feb 1 '11 at 8:14
  • 1
    Took me a while to find the start point for this. For anyone else: Select your database in the object explorer. - Right Click and select - Tasks - Generate Scripts to bring up the wizard as described above. – JumpingJezza Feb 2 '11 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.