Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 Create Tables & Database

Author  Topic 

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2013-04-11 : 06:21:05
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-11 : 07:39:15
[code]--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);
[/code]

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-12 : 05:04:23
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
   

- Advertisement -