Sunday, February 12, 2012

Command Line connect to SQL Server Database

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.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.|||

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

|||Thanks for pointing me to the CMD utilities.|||Thanks this was very helpfull I was able to login from the cmd prompt.

No comments:

Post a Comment