SQL SERVER – sqlcmd vs osql – Basic Comparison

Today we will go over very simple but to the point comparison of two SQL Server utilities or SQL Server tools.

This comes often to users which one to use sqlcmd or osql, when in need of running SQL Server queries from command prompt. Answer to this is very simple use “sqlcmd”.

sqlcmd has all the feature which osql has to offer, additionally sqlcmd has many added feature than osql. isql was introduced in earlier versions of SQL Server. osql was introduced in SQL Server 2000 version. sqlcmd is newly added in SQL Server 2005 and offers additionally functionality which SQL Server 2005 offers.


osql SQL SERVER   sqlcmd vs osql   Basic Comparison


sqlcmd SQL SERVER   sqlcmd vs osql   Basic Comparison

It is worth noting that when osql command is run on system where SQL Server 2005 is installed it gives following message in the beginning, which itself suggests to use sqlcmd.

Note: osql does not support all features of SQL Server 2005.
Use sqlcmd instead. See SQL Server Books Online for details.

If there are scenario where the script is using very basic functions of SQL Server and either sqlcmd and osql can execute them what user should do? In this case, I would suggest that use sqlcmd because sqlcmd is feature rich and why not use something which is latest.

Let me give one example, in one of the recent script which I came across required to run OS commands along with SQL commands. If osql was only used then whole script has to divide in two parts. osql would run SQL commands and XP_CMDSHELL would run OS commands. However, using sqlcmd both can be achieved easily.

If you are using sqlcmd and you need to run system command “dir” just write “!! dir” and it will run system command right away.

sqlcmd1 SQL SERVER   sqlcmd vs osql   Basic Comparison

There are many more advantage of using sqlcmd which we will see in follow up posts.

Reference : Pinal Dave (http://blog.SQLAuthority.com)