Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.
I had the need to connect from the command line recently, and decided to make a quick post on using SQLCMD, as I had an issue.
SQLCMD is a command line utility that comes with SQL Server. I know many people don’t use command lines, but they are handy at times. I recently opened a command prompt.
I then typed SQLCMD. After a delay, I got this:
The issue here is that I don’t have a default instance on this machine. All of mine are named. I need to provide a –S parameter, with a server name (and possibly instance name).
I do that and I’m connected. By default, SQLCMD (and osql) try to use Windows Auth. The 1> indicates that the utility is ready for T-SQL queries. You need to know your language here as there’s no help.
I can enter code, and check my user name. I do this, and get a 2>. The end of a batch is indicated with “GO” and this will execute the batch. You can see how this works below:
I can use this to make my code easier to read. I can format code as I would in an editor, though be aware you can’t go back and edit previous lines.
If I enter go, I’ll get this:
Not so easy to read. I have to scroll up to even figure out what the display is:
As you can see, using SELECT *, or retrieving too many columns make results hard to read. You would to wise to pick only those columns you need to return.
To leave SQLCMD, you can type exit, which will return you to the command prompt.
This is a short look at SQLCMD. The older, osql, utility functions the same way, and both are good, lightweight ways to connect to your SQL Server instance.