Author |
Topic |
prodsac
Starting Member
8 Posts |
Posted - 2011-04-12 : 17:38:43
|
Hi,I need to make a probably simple thing.I need to Create 100 database from a Master one.Is there a way to automate it?I will:detach DBUSE [master]GOEXEC master.dbo.sp_detach_db @dbname = N'MASTERDB',@keepfulltextindexfile = N'true'GOcopy the DB files with powershell (copy-items 01..100)Attach the databases and change the dbownersUSE [master]CREATE DATABASE [NEWDB01] ON( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB01.mdf' ),( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB01_log.ldf' )FOR ATTACHGOIF EXISTS ( SELECT nameFROM master.sys.databases sdWHERE name = N'NEWDB01'AND SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() )EXEC [NEWDB01].dbo.sp_changedbowner @loginame=N'DOMAIN\USER01',@map=falseGOUSE [master]CREATE DATABASE [NEWDB02] ON( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB02.mdf' ),( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB02_log.ldf' )FOR ATTACHGOIF EXISTS ( SELECT nameFROM master.sys.databases sdWHERE name = N'NEWDB02'AND SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() )EXEC [NEWDB01].dbo.sp_changedbowner @loginame=N'DOMAIN\USER02',@map=falseGOand so on..Is there a way to push variables? Or make it simpler?thanks |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-04-12 : 17:58:13
|
Better to make a backup of your database and just restore it, rather than detach, copy and attach. |
 |
|
prodsac
Starting Member
8 Posts |
Posted - 2011-04-12 : 21:43:40
|
thanks, but is there a way I could change the following statementUSE [master]CREATE DATABASE [NEWDB01] ON( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB01.mdf' ),( FILENAME = N'C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\NEWDB01_log.ldf' )FOR ATTACHGOIF EXISTS ( SELECT nameFROM master.sys.databases sdWHERE name = N'NEWDB01'AND SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() )EXEC [NEWDB01].dbo.sp_changedbowner @loginame=N'DOMAIN\USER01',@map=falseGOto made it do a loop on the "NEWDB01", to make him do NEWDB01, NEWDB02, NEWDB03... NEWDB100?Thanks |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-04-13 : 11:19:20
|
I would say1. as robvolk said make one backup2. then create a script that loops however many times you want to3. create db and restore from source4. If you want this to run weekly put it in sql jobyou better have some good amount of space in C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATAIf you don't have the passion to help people, you have no passion |
 |
|
prodsac
Starting Member
8 Posts |
Posted - 2011-04-13 : 11:40:26
|
thanks.Can you tell me why it is better to backup and copy than detach and copy?Thanks |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-04-13 : 11:43:56
|
it is not about better. masterdb might be used by other processes. you detach it and ....If you don't have the passion to help people, you have no passion |
 |
|
prodsac
Starting Member
8 Posts |
Posted - 2011-04-13 : 11:57:46
|
Ok. so something like that?BACKUP DATABASE { MASTERDB | @MASTERDB_var } TO "C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\MASTERDB.bak" ;DROP DATABASE NEWDB01CREATE DATABASE NEWDB01RESTORE DATABASE { NEWDB01 | @NEWDB01_var }FROM "C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\MASTERDB.bak";DROP DATABASE NEWDB02CREATE DATABASE NEWDB02RESTORE DATABASE { NEWDB02 | @NEWDB02_var }FROM "C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\MASTERDB.bak"; |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-04-13 : 12:14:23
|
yes something like that. you probably want to make it dynamic which might involve dynamic query. what if tomorrow you want to provision 200 more databases are you going to come in there and add one more ?If you don't have the passion to help people, you have no passion |
 |
|
prodsac
Starting Member
8 Posts |
Posted - 2011-04-13 : 12:26:59
|
Yes.That's what I was asking before, is there a way to loop in it? Probably something like that?-------------CREATE TABLE DBNAMELIST (DBNAME VARCHAR(20))INSERT INTO DBNAMELIST SELECT "NEWDB1"INSERT INTO DBNAMELIST SELECT "NEWDB2"INSERT INTO DBNAMELIST SELECT "NEWDB3"DROP DATABASE NEWDB1DROP DATABASE NEWDB2DROP DATABASE NEWDB3EXEC CREATE_NEWDBCREATE PROCEDURE CREATE_NEWDB ASDECLARE @QUERY VARCHAR(2000)DECLARE @DBNAME VARCHAR(25)--XP_SENDMAILDECLARE MAK_CURSOR CURSOR FORSELECT DBNAME FROM MAK.DBO.DBNAMELISTOPEN MAK_CURSORFETCH NEXT FROM MAK_CURSOR INTO @DBNAMEWHILE (@@FETCH_STATUS = 0)BEGINSET @QUERY = RESTORE DATABASE { "+@DBNAME" | "+@DBNAME_var" }FROM "C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\"+ @DBNAME+".bak"PRINT @QUERYEXEC (@QUERY)FETCH NEXT FROM MAK_CURSOR INTO @DBNAMEENDCLOSE MAK_CURSORDEALLOCATE MAK_CURSOR |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-04-13 : 12:42:22
|
exactly! plug it into a sql job and you should be good to go. test it out good though maybe debug it step by stepyou might want to add IF NOT EXISTS before you add a db name in the dbconfig table list. otherwise you will gave dups and ....If you don't have the passion to help people, you have no passion |
 |
|
prodsac
Starting Member
8 Posts |
Posted - 2011-04-13 : 12:47:47
|
thanks, it' s the first time I automate anything in SQL lol |
 |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-04-13 : 13:09:19
|
why are you using C:\Program Files\Microsoft SQL Server\INSTANCE\MSSQL10_50.INSTANCE\MSSQL\DATA\ rather nauseating. do you have storage elsewhere. because if your server goes caputs so do all these dbs. do you have good backup? either way i would suggest putting it somewhere else.is this real world or just a homework? dev or prod?also you will need to add script for backup in there.If you don't have the passion to help people, you have no passion |
 |
|
|