SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 Create Tables & Database
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sunny_10
Yak Posting Veteran

65 Posts

Posted - 04/11/2013 :  06:21:05  Show Profile  Reply with Quote
Hi

I want to Create Database & Tables using Stored Procedure . It should also check if Database already exists or not if not then it should create . Secondly i want to pass Database Name & MDF file as parameters.

Thanks

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 04/11/2013 :  07:39:15  Show Profile  Reply with Quote
--Script for create Database by passing Database name and .mdf file path name
DECLARE @DBName VARCHAR(30) = 'test12', @MDFFileName VARCHAR(100)
DECLARE @sql VARCHAR(MAX)=''
SET @sql =
'if exists (select name from sys.databases where name=''' + @DBName +''') 
	drop database ' + @DBName +';	
create database ' +@DBName +' 
 on primary (filename= '+ @MDFFileName + ' );'
--PRINT (@SQL);
EXEC (@sql);


To create tables, will you want to pass table names and columns?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 04/12/2013 :  05:04:23  Show Profile  Reply with Quote
quote:
Originally posted by bandi

--Script for create Database by passing Database name and .mdf file path name
DECLARE @DBName VARCHAR(30) = 'test12', @MDFFileName VARCHAR(100)
DECLARE @sql VARCHAR(MAX)=''
SET @sql =
'if exists (select name from sys.databases where name=''' + @DBName +''') 
	drop database ' + @DBName +';	
create database ' +@DBName +' 
 on primary (filename= '''+ @MDFFileName + ''' );'
--PRINT (@SQL);
EXEC (@sql);


To create tables, will you want to pass table names and columns?



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000