Create SQL Server Database and Database Tables From a File

When working with a database, it’s is always handy to develop a series of scripts to backup, restore, create, move databases around. As of late, I’ve been working with SQL Server a lot so I’ve come to appreciate the sqlcmd utility.

You can run the sqlcmd utility from the command line and run a input sql script and capture the output in a file. The input sql script file can run any sql statement, such as creating a new databases, views, tables, or procedures. You can also restore and backup any given database or whatever else you require.

To run the sqlcmd utility and have it read a file, createTables.sql, you can run something like the following from your command line, of course if you have SQL Server Management Studio and sqlcmd installed.

sqlcmd -S localhost -d dbName -U user -P password -i createTables.sql -o output.out

Leave a Reply