How to Query a SQL Server Database from Linux.



Applies to: SQL Server 2012 Standard Edition SP1 CU2 on Windows 7 SP1, Red Hat 5.4 Enterprise, Microsoft SQL Server ODBC Driver 1.0 for Linux.



This article shows how to query a database attached to a SQL Server 2012 instance from a linux server.

First you need to download and install the SQL Server ODBC Driver 1.0 for Linux from here. However, before installing the driver, make sure the following RPM packages have been installed: glibc-2.5, libgcc-4.1.2, libstdc++-4.1.2, e2fsprogs-libs-1.39, krb5-libs-1.6.1, and openssl-0.9.8e. Then follow the instructions provided on the "Install Instructions" section of the download link.

Once the driver has been installed, you need to configure the SQL Server instance for mixed mode authentication, create a SQL login on the SQL Server instance, provide the login permissions on the database you would like to query from Linux, and then you are ready to query the instance as shown in the image below:









As you can see on the image, we specify the IP address of the SQL Server instance using -S. We specify the database, in this case the Pubs database, using -d. The credentials that will be used for the connection to the database are specified with -U (for the SQL login you previously created) and -P (password associated with the SQL login). Finally we use the -Q to query the database, and the image above shows a select statement against a table named dbo.discounts.

Something that I have to mention is that not only sqlcmd is available when Microsoft SQL Server ODBC Driver 1.0 for Linux is installed, but also the bcp utility is installed with the driver.

I would like to express my appreciation to Paul Melendez at my workplace for joining me on this SQL Server adventure, and perform the installation of the drive on the Linux server.