Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL SQL-Server script
Dear Sir,
After creating script from database in MS SQL server 2008,if we want to run all the database in other PC,then we can crate Database using SQL script in SQL server.
In reverse order to create database from given script.
How to create the SQL Database from SQL script in MS SQL server 2008.
 
Please give steps or links.
Posted 9-Aug-12 22:20pm
Edited 9-Aug-12 22:26pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

CREATE DATABASE [test]
 CONTAINMENT = NONE
 ON  PRIMARY
( NAME = N'test', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\test.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON
( NAME = N'test_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\test_log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
 

or simply just
 
CREATE DATABASE [test]
  Permalink  
v2
Comments
hiteshprajpati at 10-Aug-12 3:43am
   
Thank you sir for your reply,
Using CREATE Database [test]
Database is created but the inside Tables are not there, is there any other procedure for creating table inside that Database.
Santhosh Kumar J at 10-Aug-12 3:44am
   
You want the script for all tables in a database right?
hiteshprajpati at 10-Aug-12 7:01am
   
Hello Sir,
I have script now from Script I need all the database with tables.
In MS SQL server 2008.
Thanks.
hiteshprajpati at 10-Aug-12 7:02am
   
Now I have tried SQL create command to create Database from given sql script.
I have tried as follows:
-------------------------------------------------
Create DATABASE [datauwatchit]
ON PRIMARY
( NAME = N'datauwatchit', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbuwatchit.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 
LOG ON
( NAME = N'datauwatchit', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbuwatchit_log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
-----------------------------------------------
Error is as follows:
Msg 1828, Level 16, State 5, Line 1
The logical file name "datauwatchit" is already in use. Choose a different name.
==========================================================
So I have tried Alter command to solve above errror
------------------------------------------------
ALTER DATABASE [$(datauwatchit)]
ADD FILE
(
NAME = [$(dbuwatchit)],
FILENAME = '$(C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\datauwatchit.mdf)$(datauwatchit).mdf',
SIZE = 10 MB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 5 MB
)
TO FILEGROUP [PRIMARY];
------------------
Error is as follows:
Msg 911, Level 16, State 1, Line 1
Database '$(datauwatchit)' does not exist. Make sure that the name is entered correctly.
=====================
 
Please reply where is problem and how to solve in code.
Santhosh Kumar J at 10-Aug-12 7:03am
   
How did you get ??Like solution 3?
Santhosh Kumar J at 10-Aug-12 7:05am
   
You have to change the mdf and ldf filename.. Add \dbuwatchit1.mdf or \dbuwatchitnew.mdf like this
hiteshprajpati at 10-Aug-12 7:39am
   
Using command "CREATE DATABASE [Test]" database is created but there is no Tables in this database.
I have .sql script,from which I need Database with all the tables.
Is there any other way to do so?
Santhosh Kumar J at 10-Aug-12 7:40am
   
Solution 3 is the other way. it will generate script for database creation as well as all tables creation togther.. mark it as solved, if you are able to generate script via solution 3
hiteshprajpati at 11-Aug-12 0:34am
   
Thank you sir for your help, finally with your help and kind support I solved my problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well basically if you have scripted the whole database on one server, you can take that script and run it on another server for example in master database.
 
The full script should contain the database creation (CREATE DATABASE...) and after that all necessary object creations into that database.
 
To run the script, you can use for example SSMS (Sql Server Management Studio)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you want to generate script for all tables from a database, then you have to do as below.
 
1. Go to Object explorer. Select the database which you want to generate script. Right click it.
 
You can see Tasks. click on tasks.. There you can see Generate scripts below restore. click that. it will take you to wizard. In the wizard you can select the tables which you want to generate. By default it will generate script for all tables and that database. Select a path and click on next until finish enables. Then click on finish. Go and check the script in the path you mentioned in the wizard. It will have create script for database and as well for all tables.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100