Executing sql statements from the command line (SQL Server 2008 R2 Express)


One of the things I’ve always liked about MySql is its easy to use command line interface. Microsoft Enterprise Manager was always pretty good, and of course SQL Management Studio, so I rarly have to interface with MSSQL using the command line, but when I did, I always used osql. As of SQL 2008 this was replaced by sqlcmd which is very similar to the mysql command line implementation. Basic usage is;

sqlcmd -S server_name\instance_name -U sa -P your_password

You can then type directly into the console, executing a “GO” after each statement to run. I recently used this to enable remote connections on a new install of sql express, like so;

1> exec sys.sp_configure N'remote access', N'1'
2> GO
Configuration option 'remote access' changed from 0 to 1. Run the RECONFIGURE statement to install.
1> RECONFIGURE WITH OVERRIDE
2> GO
1> exit
  1. No comments yet.
(will not be published)