Free TDS: Execute Query & Send Result

Tabular Data Stream (TDS) is an application layer protocol, used to transfer data between a database server and a client. Initially designed and developed by Sybase Inc. for their Sybase SQL Server relational database engine in 1984, and later by Microsoft in Microsoft SQL Server. For more info see wikipedia at URL

FreeTDS is the open source implementation of the TDS protocol described briefly in the previous paragraph.
Often you must provide a query result in CSV or other text format for integration with other systems or simply to import the data to an Excel spreadsheet for pivot tables.

Obviously you can do this using the tools offered by SQL Server, but must have administrative rights.
In this short article I would like to illustrate how to run a query on SQL Server and send the results via email. I assume that on your Unix / Linux Box is up and running FreeTDS software, freely downloadable from the site

Thanks to the simplicity of the Unix / Linux, you can compose complex commands from the composition of simpler commands. The command shown below, run the query contained within the sql file and then send an email with attached the query result.

Tsql command (which is part of FreeTDS) sends the query to the SQL server specified by option-H, the result of the query is stored on the file. The output file is compressed by the command gz and finally sent as an attachment by email using the mail command. The command shown above could be useful in case you need to send data (text format) from your database application weekly or daily. Here is a simple shell script that could be put into crontab.

Hoping to be helpful to someone.

Antonio Musarra

I began my journey into the world of computing from an Olivetti M24 PC ( bought by my father for his work. Day after day, quickly taking control until … Now doing business consulting for projects in the enterprise application development using web-oriented technologies such as J2EE, Web Services, ESB, TIBCO, PHP.

You may also like...