Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

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

1 Answer 1

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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