How To Connect to SQL Server From Unix





One project I’ve had to work on recently requires me to pull data from Microsoft SQL Server from a Linux machine. It took me a lot of time to search the internet to find the proper software to install, configure, and get everything running. To save time for everyone else who may need to accomplish this task in the future, I list out the steps below:

1. Download the latest stable distribution of FreeTDS from FreeTDS.org.

2. Unpack the distribution file.

3. Follow the directions in the INSTALL file to install.

4. You are done!

To connect to the SQL Server database, use the following syntax:

/usr/local/bin/bsqldb -U [user] -P [pwd] -S [host] -D [database] -i [input file] -o [output file]

[user]: username on SQL Server.
[pwd]: password on SQL Server.
[host]: Host name of SQL Server. Remember to include port information if any.
[database]: SQL Server database you wish to connect to.
[input file]: The input file containing your SQL/T-SQL statements.
[output file]: The output file containing the result set from your input.

One thing you need to pay attention to is that you’ll need to manually specify the column delimiters in your input file. Otherwise, you’ll have a difficult time parsing your result file. For example, if you want to use ‘|’ as your delimiter, you will type in:

select column1, ‘|’ column2 from table_name;

A second thing to watch out for is that if you are running bsqldb from within a script, you’ll need to specify the full path. In the syntax example above, I listed out the full path of bsqldb.

I installed bsqldb on a Linux machine. At the same time, I believe this package should work on all Unix-type machines.

Tags: Tags: , , ,