Saturday, April 25, 2015

Running SQL Server script from the command-line

Have you ever needed to run a Transact-SQL script of 1 or 2GB into your SQL Server? Well that happened to me yesterday.

Normally I work with machines with enough resources to open such files, so I'm able to create the databases on SQL Server using the Management Studio. But since last week I've been working on a MacMini with just 4GB of memory, what to do when you need to open a 2GB SQL file into Management Studio? Well go do something else, because it's going to take some time.

Today I found a very elegant way to do it, using the utility SQLCMD.

Using this tool, available in your path if you have SQL Server installed, all you need to do is:
  • Open a command prompt window.
  • In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql
  • Press ENTER.
That's all folks, have fun! P.S: SQL Server 2014 Rocks!