1

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.

Either

2
  • 2
    If you are using a GUI, it is most definitely not programming.
    – leppie
    Feb 1, 2011 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 Feb 2, 2011 at 1:17

1 Answer 1

3

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

4
  • 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, 2011 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. Feb 1, 2011 at 7:55
  • so, there is not direct option without doing it manually in the sql script? Anyway, Thanks a lot.
    – Rana
    Feb 1, 2011 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. Feb 2, 2011 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .