none
Command Line connect to SQL Server Database

    Question

  • I am trying to connect to the SQL Server database from the command line or from a cmd script on Windows - Is there a way to do this?

    I am new to SQL Server and I am trying to create a script that can check to see if the database is up and then send me a message if it is down.
    Thursday, November 10, 2005 11:09 PM

Answers

  • osql.exe is command line client which allows you to establish a connection to SQL Server.

    Please try
       osql -E
    for trusted connection, or 
       osql -U<login id> -P<password> -S<instance name>

    you can skip -S parameter if you have installed a default instance.

    In addition, use
       osql -U<login id> -P<password> -S<instance name> -i<tsql script file name>
    to run your scripts. 

    HTH.

    MCW

    Tuesday, November 15, 2005 6:48 AM

All replies

  • Yes, you can connect to the SQL Server from the command line by means of the BCP utility for example.

    Here is a list of the various CMD utilities for SQL 2000. Haven't tried them for SQL 2005 but i suppose they either work or there is something simmilar around.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/coprompt/cp_intro_3nsj.asp

    As to inform you on the status of the SQL Server, you could create a Job in the SQL Server Agent to check for a simple query such as "SELECT @@SERVERNAME" and send an e-mail when it fails.
    You can also set the SQL Server Agent service to auto-restart and create another Job to start every time the SQL Server Agent starts and tell the job to send an e-mail, so you can check for problems with the SQL Server Agent.

    Hope this helps.
    Good luck,
    Alan.
    Friday, November 11, 2005 12:37 AM
  • osql.exe is command line client which allows you to establish a connection to SQL Server.

    Please try
       osql -E
    for trusted connection, or 
       osql -U<login id> -P<password> -S<instance name>

    you can skip -S parameter if you have installed a default instance.

    In addition, use
       osql -U<login id> -P<password> -S<instance name> -i<tsql script file name>
    to run your scripts. 

    HTH.

    MCW

    Tuesday, November 15, 2005 6:48 AM
  • Thanks for pointing me to the CMD utilities.
    • Proposed as answer by Misza Stonzelka Tuesday, September 10, 2013 11:13 AM
    Thursday, November 17, 2005 4:48 PM
  • Thanks this was very helpfull I was able to login from the cmd prompt.
    Thursday, November 17, 2005 4:50 PM
  • Hi AllanK. Unfortunately MS decided to drop or rename some of these products so this list is only partial helpfull for 2008 users.

    diff:

    isql is called osql these days

     

    regards

    Wednesday, April 27, 2011 7:21 AM
  • sqlsplus - great add to SQLCMD


    You can also use free command line tool "SQLS*Plus" which is like an Oracle SQL*Plus for SQL Server. 

    sqlsplus works with SQL Server 2008, 2005 and 2000 and very flexible with variables (&, &&), data formatting (set lines size, pagesize, etc), etc - much better alternative to sqlcmd or isql

    I found sqlsplus at http://www.sqlsplus.com
    Wednesday, October 30, 2013 10:23 PM