macOS High Sierra で sqlcmd ユーティリティをセットアップする方法

#はじめに
SQL Server 2017 からマルチプラットフォーム対応になっており、macOS で利用する機会も増えていることだと思います。GUI のツールである [SQL Operations Studio](https://docs.microsoft.com/ja-jp/sql/sql-operations-studio/what-is) も提供されているのですが、コマンドラインでの利用を好まれるケースも多いと思います。
この記事では、sqlcmd ユーティリティのセットアップ方法を備忘録がてら残しておきます。

#事前準備
macOS 環境で広く利用されている[Homebrew](https://brew.sh/index_ja.html)を利用できるように設定しておきます。

#インストール
Homebrew が入ってれば Terminal でコマンド3行打つだけです。

```bash
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install --no-sandbox msodbcsql mssql-tools
```
インストール後に、__sqlcmd__ って打って動作確認しましょう。

##実行例
```bash

Mihos-MacBook:Data miyamam$ brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
amber                     blast                     field3d                   jdupes                    libtomcrypt               nyx                       sickle                    zig
auditbeat                 bwa                       glances                   kallisto                  mariadb-connector-odbc    opencascade               skafos
augustus                  container-diff            go-statik                 kumo                      mmseqs2                   orocos-kdl                tmux-xpanes
bedops                    darksky-weather           hlint                     libccd                    monero                    plank                     vis
bioawk                    elektra                   hmmer                     libjwt                    mpir                      seqtk                     visp
==> Updated Formulae
azure-cli ✔                  consul                       gist                         khard                        mpv                          pure-ftpd                    statik
gdbm ✔                       coreutils                    git                          kibana                       mr                           pushpin                      streamlink
python3 ✔                    cp2k                         git-annex                    kitchen-sync                 mruby                        pwntools                     stress-ng
sqlite ✔                     cppcheck                     git-cinnabar                 kite                         mu                           py3cairo                     subversion
abcm2ps                      cppcms                       git-cola                     knot                         mutt                         pybind11                     subversion@1.8
abcmidi                      cracklib                     git-ftp                      knot-resolver                mycli                        pyenv                        suite-sparse
abyss                        creduce                      git-integration              kobalt                       mypy                         pygobject                    sundials
acmetool                     cromwell                     git-remote-hg                kompose                      mysql                        pygobject3                   superlu
acpica                       crosstool-ng                 git-secret                   konoha                       mysql++                      pypy                         svgcleaner
agda                         crowdin                      git-town                     kontena                      mysql-connector-c++          pypy3                        svtplay-dl
algernon                     cryfs                        gitbucket                    kotlin                       mysql-sandbox                pyqt                         swagger-codegen
amazon-ecs-cli               cryptopp                     gitlab-runner                kpcli                        mysql@5.5                    python                       swi-prolog
angular-cli                  crystal-icr                  gjs                          kubeless                     mysql@5.6                    pytouhou                     swiftformat
ansible                      crystal-lang                 glib                         kubernetes-cli               mytop                        qca                          swiftlint
ansible-lint                 csvkit                       gmsh                         kubernetes-helm              nagios-plugins               qd                           sword
antigen                      csvtomd                      gmt                          lame                         nano                         qpdf                         sync_gateway
apache-arrow                 ctop                         gmt@4                        lammps                       nco                          qpid-proton                  syncthing
apache-opennlp               curl                         gnome-builder                languagetool                 ncurses                      qrupdate                     sysbench
apache-spark                 czmq                         gnome-doc-utils              lapack                       neko                         qscintilla2                  sysdig
apibuilder-cli               datetime-fortran             gnome-recipes                lbdb                         nesc                         qt                           taisei
apktool                      datomic                      gnu-smalltalk                lcm                          net-snmp                     qt@5.7                       taktuk
apm-server                   davmail                      gnumeric                     ldc                          netcdf                       quex                         talloc
app-engine-go-64             dbhash                       gnupg@2.0                    ldns                         newsboat                     quicktype                    tcl-tk
app-engine-java              dcm2niix                     gnutls                       leaps                        nghttp2                      r                            tclap
arangodb                     dcos-cli                     go                           ledger                       ngrep                        rabbitmq                     telegraf
argon2                       ddgr                         gobuster                     lensfun                      nifi                         radare2                      teleport
argyll-cms                   debianutils                  godep                        leptonica                    nikto                        rancher-cli                  termius
armadillo                    dehydrated                   goenv                        libcddb                      nnn                          ranger                       terraform
armor                        dep                          goffice                      libcdio                      node                         ratfor                       terraform_landscape
arpack                       dependency-check             gollum                       libcds                       node-build                   rbenv-aliases                terragrunt
artifactory                  devd                         gom                          libconfig                    node@6                       rbenv-binstubs               texmath
asciinema                    dhall-json                   goofys                       libdvdcss                    node@8                       rbenv-bundle-exec            thefuck
asdf                         diamond                      googler                      libdvdnav                    noti                         rbenv-bundler                thrift
astyle                       diff-so-fancy                gopass                       libdvdread                   notmuch                      rbenv-bundler-ruby-version   thrift@0.9
ats2-postiats                diffoscope                   gpg-agent                    libebur128                   nrpe                         rbenv-chefdk                 tig
augeas                       digdag                       gradle                       libgetdata                   nspr                         rbenv-communal-gems          tile38
autogen                      ditaa                        gradle-completion            libgig                       nss                          rbenv-ctags                  tin
aws-elasticbeanstalk         django-completion            graphicsmagick               libgit2-glib                 numpy                        rbenv-default-gems           tintin
aws-sdk-cpp                  dlib                         grc                          libgosu                      nuxeo                        rbenv-gemset                 tippecanoe
awscli                       dmd                          grib-api                     libhttpseverywhere           nvc                          rbenv-use                    tnef
babl                         dnscrypt-proxy               gromacs                      libjson-rpc-cpp              octave                       rbenv-vars                   todoman
backupninja                  docfx                        gron                         liblunar                     offlineimap                  rbenv-whatis                 tokei
bam                          docker                       groonga                      libmagic                     ola                          re2                          tomcat
bandcamp-dl                  docker-completion            grpc                         libmaxminddb                 oniguruma                    recutils                     tomcat-native
baresip                      docker-gen                   gsoap                        libmonome                    onioncat                     redis                        tor
bartycrouch                  docker-machine-nfs           gst-plugins-bad              libmpc                       ooniprobe                    redland                      tracebox
basex                        doxygen                      gst-plugins-base             libmspub                     open-babel                   restic                       traefik
bash-preexec                 doxymacs                     gst-python                   libpagemaker                 open-mpi                     rex                          trafficserver
bash-snippets                dpkg                         gtk+                         libphonenumber               openblas                     rgbds                        transmission
bdw-gc                       dscanner                     guile                        libpqxx                      opencoarrays                 riemann                      travis
bear                         dssim                        guile@2.0                    libpst                       opencolorio                  rocksdb                      ttf2eot
beast                        dub                          gutenberg                    libqalculate                 opencv                       rom-tools                    ttfautohint
bench                        duck                         gwt                          libraw                       opencv@2                     root                         ttyd
bento4                       dungeon                      gwyddion                     libre                        openrtsp                     roswell                      twoping
bettercap                    duplicity                    gxml                         librealsense                 openshift-cli                rpm                          twtxt
betty                        e2fsprogs                    gzip                         librsvg                      openssl@1.1                  rsync                        txr
binaryen                     ecl                          hadolint                     libsass                      openttd                      rtags                        u-boot-tools
bind                         ejabberd                     haproxy                      libsigsegv                   orc-tools                    rtv                          unbound
binutils                     elasticsearch                hashpump                     libsoup                      oscats                       ruby                         unixodbc
binwalk                      elixir                       haste-client                 libstfl                      osm2pgrouting                ruby-build                   urh
biogeme                      elvish                       hdf5                         libtasn1                     osm2pgsql                    ruby@1.9                     urweb
bit                          emscripten                   hdf5@1.8                     libtensorflow                osquery                      ruby@2.0                     v8
bitcoin                      encfs                        heartbeat                    libtiff                      overmind                     ruby@2.1                     vala
bitrise                      enchant                      henplus                      libtorrent-rasterbar         owfs                         ruby@2.2                     vapoursynth
blackbox                     entr                         heroku                       libu2f-server                packetbeat                   ruby@2.3                     vault
blastem                      envconsul                    hexgui                       libuv                        packmol                      rust                         vcdimager
boost-mpi                    ephemeralpg                  hg-fast-export               libvirt                      paket                        rustup-init                  vdirsyncer
boost-python                 eralchemy                    hg-flow                      libvoikko                    pandoc                       s-search                     veclibfort
boost-python@1.59            etcd                         hh                           libvpx                       pandoc-citeproc              s6                           verilator
boost@1.55                   etsh                         highlight                    libxc                        pandoc-crossref              sagittarius-scheme           vim
boost@1.57                   exim                         hledger                      libzdb                       par2                         saldl                        vim@7.4
boost@1.59                   exomizer                     homebank                     link-grammar                 parallel                     samtools                     vips
boost@1.60                   exploitdb                    htop                         linkerd                      pari                         sassc                        voltdb
bork                         faas-cli                     htslib                       liquigraph                   pass                         sbcl                         vte
botan                        fabio                        http-server                  little-cms                   passpie                      sbt                          vtk
bowtie2                      fades                        httpd                        llnode                       pdal                         scalapack                    vultr
braid                        fail2ban                     httpie                       llvm                         pdf2htmlex                   sccache                      w-calc
bsponmpi                     fb-client                    hugo                         llvm@3.9                     pdns                         scipy                        w3m
btfs                         fd                           hwloc                        llvm@4                       pdnsrec                      scummvm                      watson
bubbros                      fdclone                      hydra                        logstash                     pegtl                        scummvm-tools                weboob
buku                         fdroidserver                 hyperscan                    logtalk                      percona-server               selecta                      websocketd
bzt                          feh                          hypre                        lwtools                      percona-toolkit              serialosc                    weechat
cake                         fftw                         ib                           lynis                        percona-xtrabackup           serveit                      wget
calabash                     fibjs                        ibex                         lysp                         pex                          sfcgal                       whatmp3
cargo-completion             filebeat                     ice                          lz4                          pg_top                       shadowsocks-libev            whohas
cask                         firebase-cli                 idnits                       macosvpn                     pgloader                     shairport-sync               whois
cayley                       flatcc                       idris                        macvim                       pgplot                       shfmt                        wiggle
ccache                       flow                         igv                          magic-wormhole               pgpool-ii                    shmcat                       wine
ccm                          fluid-synth                  imagemagick                  mame                         pgrouting                    shocco                       wireshark
ceres-solver                 fn                           imagemagick@6                mapnik                       pianod                       shunit2                      wpscan
certbot                      fobis                        imagesnap                    mariadb                      picard-tools                 shyaml                       writerperfect
cfengine                     folly                        immortal                     mariadb-connector-c          pick                         silk                         wrk
cfr-decompiler               fonttools                    innotop                      mat                          picocom                      simgrid                      wtf
cgal                         format-udf                   inspircd                     mdp                          pigz                         sip                          wwwoffle
cgrep                        fox                          io                           mercurial                    pilosa                       sjk                          wxpython
chakra                       freeciv                      ipython                      meson                        pipenv                       smlnj                        x3270
chamber                      freeradius-server            ipython@5                    mesos                        pius                         snakemake                    xapian
charm-tools                  freeswitch                   iron-functions               metricbeat                   planck                       snapcraft                    xcenv
check_postgres               freetds                      irssi                        micro                        plantuml                     snappystream                 xml-tooling-c
checkstyle                   freetype                     iso-codes                    micropython                  platformio                   snapraid                     xmrig
chisel                       frugal                       jags                         mikutter                     plplot                       sngrep                       xonsh
chromaprint                  fwup                         jenkins                      miller                       pmd                          snort                        xtensor
chromedriver                 gammu                        jenkins-job-builder          mingw-w64                    pngquant                     socat                        xvid
chronograf                   gauge                        jenkins-lts                  minio-mc                     poco                         solr                         xxhash
chuck                        gawk                         jetty                        minizinc                     pod2man                      sonarqube                    yaf
cimg                         gcc                          jetty-runner                 mitmproxy                    ponyc                        sops                         yara
citus                        gcc@4.9                      jfrog-cli-go                 mksh                         ponysay                      sourcery                     yaze-ag
clamav                       gcc@5                        jhiccup                      mkvtoolnix                   postgis                      spades                       yeti
clipper                      gcc@6                        jhipster                     mlt                          postgres-xc                  spdlog                       ykpers
clojure                      gdal                         jing-trang                   moco                         postgresql                   speech-tools                 you-get
cloog                        gdcm                         joe                          modules                      postgresql@9.4               sphinx                       youtube-dl
closure-compiler             gearman                      joplin                       molecule                     postgresql@9.5               sphinx-doc                   z3
cmake                        geeqie                       json-fortran                 monetdb                      postgresql@9.6               spigot                       zabbix
cmark                        gegl                         jsvc                         mongo-c-driver               postgrest                    spin                         zero-install
cmark-gfm                    geoip                        juju                         mongodb                      povray                       sql-translator               zile
cockroach                    geoserver                    juju-wait                    monitoring-plugins           pre-commit                   sqldiff                      zimg
cocoapods                    get-flash-videos             jump                         moreutils                    presto                       sqlite-analyzer              znc
coffeescript                 get_iplayer                  just                         mp3gain                      prometheus                   sqlmap                       zookeeper
collectd                     gexiv2                       kafka                        mpd                          proof-general                ssh-audit                    zplug
collector-sidecar            ghi                          karn                         mpfi                         protobuf                     sslh                         zpython
compcert                     gifsicle                     kedge                        mpfr                         protobuf@2.5                 sslscan                      zsh
conan                        gifski                       keepassc                     mpich                        pspg                         sslsplit                     zsh-lovers
conjure-up                   gimme                        khal                         mplayershell                 psqlodbc                     sslyze
==> Renamed Formulae
camlistore -> perkeep
==> Deleted Formulae
antlr@3                           docker@1.71                       gst-plugins-base@0.10             kubernetes-cli@1.3                moodbar                           selenium-server-standalone@2.45
apache-spark@1.5                  eigen@3.2                         gst-plugins-good@0.10             laszip@2                          mpfr@2                            srtp@1.6
apache-spark@1.6                  gcc@4.6                           gst-plugins-ugly@0.10             ledger@2.6                        mvptree                           stklos
autoconf@2.64                     gcc@4.7                           gstreamer@0.10                    libmpc@0.8                        open-mpi@1.6                      swig@2
automake@1.12                     gcc@4.8                           influxdb@0.8                      libpng@1.2                        otto                              talk-filters
azure-cli@1                       glfw@2                            isl@0.11                          libpqxx@3                         pcap_dnsproxy                     unison@2.40
bazel@0.2                         gmp@4                             isl@0.12                          libxml2@2.7                       percona-server@5.5                zeromq@3.2
clang-format@3.8                  go@1.5                            isl@0.14                          litmus                            perl@5.14                         zeromq@4.0
cloog@0.15                        grails@2.5                        jetty@8                           logstash@2.4                      pond                              zeromq@4.1
cloudbees-sdk                     gsl@1                             jpeg@6                            logstash@5.6                      ppl@0.11
docker@1.11                       gst-plugins-bad@0.10              juju@1.25                         mg3a                              redis@2.6

==> Tapping microsoft/mssql-release
Cloning into '/usr/local/Homebrew/Library/Taps/microsoft/homebrew-mssql-release'...
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 8 (delta 3), reused 6 (delta 2), pack-reused 0
Unpacking objects: 100% (8/8), done.
Tapped 4 formulae (35 files, 34.5KB)
Mihos-MacBook:Data miyamam$ brew update
Already up-to-date.
Mihos-MacBook:Data miyamam$ brew install --no-sandbox msodbcsql mssql-tools
==> Installing msodbcsql from microsoft/mssql-release
==> Installing dependencies for microsoft/mssql-release/msodbcsql: libtool, unixodbc
==> Installing microsoft/mssql-release/msodbcsql dependency: libtool
==> Downloading https://homebrew.bintray.com/bottles/libtool-2.4.6_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libtool-2.4.6_1.high_sierra.bottle.tar.gz
==> Caveats
In order to prevent conflicts with Apple's own libtool we have prepended a "g"
so, you have instead: glibtool and glibtoolize.
==> Summary
🍺  /usr/local/Cellar/libtool/2.4.6_1: 71 files, 3.7MB
==> Installing microsoft/mssql-release/msodbcsql dependency: unixodbc
==> Downloading https://homebrew.bintray.com/bottles/unixodbc-2.3.5_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring unixodbc-2.3.5_1.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/unixodbc/2.3.5_1: 43 files, 1.8MB
==> Installing microsoft/mssql-release/msodbcsql
==> Downloading http://download.microsoft.com/download/4/9/5/495639C0-79E4-45A7-B65A-B264071C3D9A/msodbcsql-13.1.9.2.tar.gz
######################################################################## 100.0%
The license terms for this product can be downloaded from
https://aka.ms/odbc131eula and found in
/usr/local/share/doc/msodbcsql/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
YES
==> odbcinst -u -d -n "ODBC Driver 13 for SQL Server"
==> odbcinst -i -d -f ./odbcinst.ini
Warning: Calling <<-EOS.undent is deprecated!
Use <<~EOS instead.
/usr/local/Homebrew/Library/Taps/microsoft/homebrew-mssql-release/Formula/msodbcsql.rb:16:in `caveats'
Please report this to the microsoft/mssql-release tap!

Warning: Calling <<-EOS.undent is deprecated!
Use <<~EOS instead.
/usr/local/Homebrew/Library/Taps/microsoft/homebrew-mssql-release/Formula/msodbcsql.rb:16:in `caveats'
Please report this to the microsoft/mssql-release tap!

==> Caveats
If you installed this formula with the registration option (default), you'll
need to manually remove [ODBC Driver 13 for SQL Server] section from
odbcinst.ini after the formula is uninstalled. This can be done by executing
the following command:
    odbcinst -u -d -n "ODBC Driver 13 for SQL Server"
==> Summary
🍺  /usr/local/Cellar/msodbcsql/13.1.9.2: 9 files, 2.6MB, built in 14 seconds
==> Installing mssql-tools from microsoft/mssql-release
==> Downloading https://go.microsoft.com/fwlink/?linkid=848963
==> Downloading from http://download.microsoft.com/download/F/D/1/FD16AA69-F27D-440E-A15A-6C521A1972E6/mssql-tools-14.0.6.0.tar.gz
######################################################################## 100.0%
The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746949 and found in
/usr/local/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
YES
🍺  /usr/local/Cellar/mssql-tools/14.0.6.0: 11 files, 1.1MB, built in 55 seconds
M
```
#参考リンク
- [sqlcmd Utility](https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility)
- [Microsoft/homebrew-mssql-release](https://github.com/Microsoft/homebrew-mssql-release)
- [SQL Server Command Line Tools for macOS released](https://blogs.technet.microsoft.com/dataplatforminsider/2017/05/16/sql-server-command-line-tools-for-macos-released/)