0

I am using the below command in my batch file and running it via task scheduler.

osql -S myserver\sqlexpress -E  -i "c:\test.sql" -o "c:\output.txt"

I get the error stating that:

Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections.

Could someone advise what exactly should be the myserver and sqlexpress in the above example?

I am referring the link for the above script.
How to automatically execute SQL queries with a batch file
Source: Mitel MiContact Center Knowledge Base

0

1 Answer 1

2

The -S portion of the osql command tells osql where to find the SQL instance that you want to interact with. It typically consists of two parts -- the server name and the instance name.

You'll need to replace "myserver" with the name of the server that your SQL installation is running on. And, if your SQL installation is using a named instance, you'll need to replace "sqlexpress" with the name of that instance. If, on the other hand, you installed SQL as a default instance, you can leave out the part after the backslash.

Examples

I've got SQL running on a machine named DougSQL01. As a default instance. So my osql command would look like this:

osql -S DougSQL01 -E -i "c:\test.sql" -o "c:\output.txt"

I've got another instance of SQL running on another server, DougSQL02. This one is a named instance, with an instance name of "Testing". In this case, my osql command would look like this:

osql -S DougSQL02\Testing -E -i "c:\test.sql" -o "c:\output.txt"

There are some other subtleties about how to specify a server and an instance, like specifying a port instead of an instance name, using an IP address instead of a hostname, specifying "mssqlserver" for a default instance, and the ability to leave out the name of the instance if a named instance is running on port 1433, but none of them are likely to apply in your case.

6
  • Thanks Doug for the clarification. I was able to run it but the output file which i get has data all over the place and is not in columns. How can I get the output data in columns? Could you please advise?
    – rk02
    Commented Feb 4, 2021 at 19:37
  • The osql tool is deprecated. If you're at the point of setting up new automation, you should use sqlcmd instead. See learn.microsoft.com/en-us/sql/tools/… and learn.microsoft.com/en-us/sql/tools/…. If you must use osql, I recommend that you ask a new question for the output formatting.
    – Doug Deden
    Commented Feb 4, 2021 at 20:13
  • Doug that link was helpful. However, the second server that I am trying is a azure database server and the same script is failing
    – rk02
    Commented Feb 5, 2021 at 16:02
  • For those following along, the followup question is here: dba.stackexchange.com/q/284729/170408
    – Doug Deden
    Commented Feb 5, 2021 at 16:05
  • Regarding Azure: the osql tool, while deprecated, is still supported for Azure instances. We'll need a little bit more to go on than "the same script is still failing". If your original question has been answered, and the Azure issue is a new problem, you should open a new question.
    – Doug Deden
    Commented Feb 5, 2021 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.