Can I create two SQL DB's from batch file?

By ajspruit911 ·
With very little knowledge of .bat files and even less of SQL scripting i might be asking the imposible here but fundimentily i am trying to create databases from a .bat file.

Big picture:

I need to create two databases with the same name one prefixed M and the other prefixed R (i.e. MYDBM and MYDBR). I want to be able to pass the database name to create, as a parameter to the bat file.

I also need to be able to specify what machine it needs to be on from within the bat file or parameter.

Im really not sure if this can be done or not and if so where to start, if anyone with more SQL/Scripting knowledge could help or point me in the right direction it would be much appricaited.

thanks for reading

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Not from a batch file, you could from a VBS file however

by Slayer_ In reply to Can I create two SQL DB's ...

Using ADO objects in VBS you could create a database.

I am almost certain you cannot do this from a batch file.

Collapse -


by Tony Hopkinson In reply to Can I create two SQL DB's ...

It's a bit messy though.
First thing to do, is to generate a sql script to create the db.
If you create a db 'manually' , then use sql manager's scripting tools to generate the script. Give it a good test, logins roles users permissions etc

With sql server you get a command line tool called osql, you pass in server, username etc, and the name of the script.

Then put the osql call in the bat/cmd file and you are off.

Proably the easiest way to get the db name in there is once you passed it in to the script, copy the template you created earlier then do a search and replace on the copy script you created earlier.

That's the one on your osql command line.

PS redirect output from osql to a file for an install log. That way you can see how much of an arse you've made of it, after a simple little amendment.

Lots to to look up and play with, have fun.


Related Discussions

Related Forums