Sunday, September 23, 2007

SQLCMD -- Part I (Basics, Connectivity)

“SQLCMD” is a command line tool which was shipped by Microsoft along with SQL Server 2005. Previously SQL Server was having ISQL and OSQL as its command line utility. SQLCMD is replacing both of them (i.e., ISQL is not there in SQL Server 2005 RTM version. OSQL would also be eventually removed!!).

When “SQLCMD” is run from the MS-DOS command prompt, it uses the OLE DB provider to execute the given queries.

SQLCMD has batch and interactive modes. 'Batch mode' can be used mainly for scripting and automation tasks, while 'Interactive mode' is for firing ad-hoc querys.

i) To list all the parameters supported by SQLCMD utility, run the following in command prompt

ii) To connect to SQL Server using SQL Authentication
SQLCMD -U username -P yourpassword -S Servername

Once we press the enter key. If the UID/PWD is valid it would prompt you to enter the t-sql query to execute.

If at all the UID/PWD combination is wrong it would throw an error similar to the one shown below:

Msg 18456, Level 14, State 1 .......
Login Failed for user 'sa'

iii) To connect to SQL Server using Windows authentication
SQLCMD -S servername

By default, SQLCMD uses the trusted connection only. So no need of specifying -E parameter which is meant for this purpose.

iv) To connect to a named instance using the portnumber and the hostname

In the command prompt type Hostname and press enter. It would display the hostname of your PC.

SQLCMD -U sa -P hotmail -S tcp:VADIVEL,1433

Note: I have just mentioned 1433 here for example. Actually its not needed as its the default port for SQL Server.

v) How to know what is the port number configured in my box?

SQL Server's works by default with Port 1433. If it has been configured earlier to work on a different port then follow the below steps to find it out!

1) Open up 'SQL Server Configuration Manager'

2) Then double click on 'Protocols for MSSQLServer'

No comments: