<p>As far as I understand, if I want to get my database under source control then I need to be checking in change scripts for each change and then running them from some revision to get the correct DB.</p>

<p>I am trying to make a batch file that will be checked in too, that allows other developers on the team to re-build the DB locally without too much trouble. I believe sqlcmd is the way to achieve this. I have it setup to enumerate all files in the dir of .sql files and run sqlcmd for each.</p>

<p>My question is who has done this before and do you have an advice on the best way to achieve this? Is the way I intend to do this the best way or is there a better way?</p>

<p>Hope that's not too vague.</p>

<p>Thanks in advance,</p>

<p>Martin.</p>

One thing to be aware of is that you need to create objects in the order of dependency. So you cannot simply iterate files.
We ended up having a batch file that would list all the objects in order of dependency
Actually we had 2 batch files one called say createDBObject.bat:
```
```
:: Parameters Required:
:: %1 UserID
:: %2 Password
:: %3 Server
:: %4 Database
:: %5 file with scripted object
::
echo. >> CreateDBObjectsLog.txt
echo  %5 >> CreateDBObjectsLog.txt
osql -U%1 -P%2 -S%3 -i%5 -d%4 -n >> CreateDBObjectsLog.txt
echo * %5
```
```
and then the other with the list of all the db objects:
```
```
:: Parameters Required:
:: %1 UserID
:: %2 Password
:: %3 Server
:: %4 Database
::
echo object  in %4 database on %3 server
echo Please Wait ...

if exist CreateDBObjectsLog.txt del CreateDBObjectsLog.txt

call createDBObject.bat %1, %2, %3, %4, ScriptedTable1
call createDBObject.bat %1, %2, %3, %4, ScriptedTable2
...
call createDBObject.bat %1, %2, %3, %4, ScriptedTableN

call createDBObject.bat %1, %2, %3, %4, ScriptedView1

call createDBObject.bat %1, %2, %3, %4, ScriptedSP1

etc
```
```
Now we use
SQL Compare Pro
that automates all those tasks
You can also check related question:
Is there a “poor man’s” alternative to RedGate for scripting out entire database schema?