{"id":18284507,"url":"https://github.com/pplu/perl-mssql-server","last_synced_at":"2026-04-30T10:09:49.748Z","repository":{"id":66753291,"uuid":"105210970","full_name":"pplu/perl-mssql-server","owner":"pplu","description":"Notes on how to configure your system to connect to a SQL server with Perl","archived":false,"fork":false,"pushed_at":"2020-12-28T20:28:53.000Z","size":5,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T05:46:45.139Z","etag":null,"topics":["article","dbi","microsoft-odbc-driver","mssql","odbc","perl","sql-server"],"latest_commit_sha":null,"homepage":null,"language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pplu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-29T00:12:49.000Z","updated_at":"2023-07-26T10:02:21.000Z","dependencies_parsed_at":"2023-02-24T16:15:20.242Z","dependency_job_id":null,"html_url":"https://github.com/pplu/perl-mssql-server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pplu/perl-mssql-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pplu%2Fperl-mssql-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pplu%2Fperl-mssql-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pplu%2Fperl-mssql-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pplu%2Fperl-mssql-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pplu","download_url":"https://codeload.github.com/pplu/perl-mssql-server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pplu%2Fperl-mssql-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32460975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T22:27:22.272Z","status":"online","status_checked_at":"2026-04-30T02:00:05.929Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["article","dbi","microsoft-odbc-driver","mssql","odbc","perl","sql-server"],"created_at":"2024-11-05T13:13:46.393Z","updated_at":"2026-04-30T10:09:49.733Z","avatar_url":"https://github.com/pplu.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Connecting to SQL Server with Perl from Linux\n\nFinding information on how to use SQL Server with Perl is scarce (compared to the plethora of information available for MySQL). Information gets \neven scarcer when you want to connect to SQL Server with Perl from Linux.\n\nMost (if not all) of the documentation regarding this ordeal revolves around using FreeTDS: an open source implementation of [FreeTDS](https://www.freetds.org/), which\nis an Open Source implementation of the protocol used by SQL Server. For some while now, Microsoft has been shipping an ODBC driver that works under Linux, and I \nwanted to try that out, as it also leads to [using Azure SQL Datawarehouse](https://github.com/pplu/azure-sqlserver-sqldatawarehouse-perl).\n\nSo, we'll be connecting from a Linux host via ODBC with the native Microsoft ODBC driver. We can locally \"simulate\" a MSSQL host by running SQL Server inside a Linux\nDocker container (thanks MS!)\n\nI've run this test successfully on Debian 10 (buster), and prior to this on Debian 9 (stretch) and Debian 8 (jessie) with minor adjustments (repo URLs)\n\n# Preparing the environment:\n\nIt looks like the DBD::ODBC has problems if you have libiodbc2 installed. If you don't want to uninstall libodbc2, take a look at \n[this stack overflow question](https://stackoverflow.com/questions/11354288/undefined-symbol-sqlallochandle-using-perl-on-ubuntu)\nfor how to avoid the problem without removing libodbc2\n\n```\nsudo apt-get remove --purge libiodbc2\n```\n\nWe'll use Perls' carton bundler to install the latest versions of some dependencies (DBI, DBD::ODBC) in a local directory\n```\nsudo apt-get install -y build-essential carton\n```\n\nWe'll need the UNIX ODBC library, and its' dev package (to compile the DBD::ODBC module)\n```\nsudo apt-get install -y unixodbc unixodbc-dev\n```\n\nNow we'll need to install the Microsoft ODBC driver. [Luckily there are Debian packages](https://docs.microsoft.com/es-es/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server)\n\n```\nsudo su -\ncurl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -\ncurl https://packages.microsoft.com/config/debian/10/prod.list \u003e /etc/apt/sources.list.d/mssql-release.list\napt-get update\nACCEPT_EULA=Y apt-get install msodbcsql17\nexit\n```\n\nNow we'll download the example script from this repo (`apt-get install git` if you don't have git installed)\n```\ngit clone https://github.com/pplu/perl-mssql-server.git\ncd perl-mssql-server\n```\nNow install the local dependencies with carton (they are in the cpanfile of the repository)\n```\nDBD_ODBC_UNICODE=1 carton install\n```\nnote that you can just `carton install` if you don't need the unicode support.\n\n# Connecting to SQL Server with Perl\n\nStart SQL Server. We'll start it on port 1401 (this is where the [connect](connect.pl) script will connect to)\n```\ndocker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Password1' -e 'MSSQL_PID=Developer' -p 1401:1433 --name sqlcontainer1 -d microsoft/mssql-server-linux\n```\nRun the connect script. It will create a database, a table, put data in it and SELECT it via DBI\n```\ncarton exec connect.pl\n```\nYou're done! Happy Hacking\n\n# Do it again, please\n\nThe second time you run connect.pl, it will error out, saying that TestDB already exists. To reset the environment, just:\n```\ndocker stop sqlcontainer1\ndocker rm sqlcontainer1\n```\nAnd `docker run` the SQL Server container again\n\n# Additional notes\n\n## Named DSN\n\nIn the example, the DSN for ODBC is inlined in the connect call to DBI. You can connect via a named DSN also.\n\n```\nmy $dbh = DBI-\u003econnect(\"dbi:ODBC:testdsn\", $user, $password, { RaiseError =\u003e 1 });\n```\n\nWith the `odbcinst -q -s` command you can see what DSNs are configured in your system. In the example we're using `testdsn`\n\nIn /etc/odbc.ini you should have:\n\n```\n[ODBC Data Sources]\ndata_source_name = testdsn\n\n[testdsn]\nDriver = /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.6.so.1.1 \nDESCRIPTION = Microsoft ODBC Driver 13 for SQL Server\nSERVER=localhost,1401\n```\n\n## What is the RaiseError in the connect call?\n\nThis enables DBI to throw exceptions when there are failures. Traditional DBI (and lots of code samples on the Internet)\nmanually throw exceptions based on return values from DBI like this:\n\n```\nmy $dsn = DBI-\u003econnect('...', $user, $password) or die \"\";\n```\nThat is quite old-school. DBI lets you say that it will throw the exceptions for you, so your code is cleaner, and you\ndon't have to worry about plaguing all your DBI calls with `or die \"\"`.\n\n# Additional links that helped me get this running:\n\nhttps://stackoverflow.com/questions/4905624/how-do-i-connect-with-perl-to-sql-server\n\nhttps://metacpan.org/pod/DBD::ODBC\n\nhttps://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker\n\nhttps://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-docker\n\nhttps://www.connectionstrings.com/sql-server/\n\n# Author, Copyright and License\n\nThis article was authored by Jose Luis Martinez Torres\n\nThis article is (c) 2017 CAPSiDE, Licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)\n\nThe canonical, up-to-date source is [GitHub](https://github.com/pplu/perl-mssql-server.git). Feel free to\ncontribute back\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpplu%2Fperl-mssql-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpplu%2Fperl-mssql-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpplu%2Fperl-mssql-server/lists"}