Created
November 4, 2021 14:46
-
-
Save AndreSteenveld/4406afcdc5bca008a42d31fde387d454 to your computer and use it in GitHub Desktop.
Using bcp to dump table to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
set -e | |
# | |
# Invoke this script; table-to-tsv <database> <schema> <table> -U sa -P 'P455word!!!' -S localhost | |
# | |
# The output will be written to stdout, redirecting to where you need your tsv to go. Make sure `bcp` | |
# is in your path, in the docker images it can be found in /opt/mssql-tools/bin | |
# | |
declare database="$1" | |
declare schema="$2" | |
declare table="$3" | |
declare header_query=" | |
select column_name | |
from information_schema.columns | |
where '${database}' = table_catalog | |
and '${schema}' = table_schema | |
and '${table}' = table_name | |
order by ordinal_position | |
" | |
# | |
# A few notes here; `bcp` just writes to stdout and nothing else if no file is provided, it will also | |
# output messages every time with warnings and a summary of what has been done. Which pollutes the | |
# the output if you want to work with it as a stream. Good for us everything on *nix is a a file and | |
# that bcp is a little naive in this regard, using `cat` in a subshell as a file will allow us to | |
# to stdout without adding the extra stuff to it. | |
# | |
bcp "$header_query" queryout >(cat) -c -t'' -r'\t' -d "$database" "${@:4}" > /dev/null; | |
bcp "$schema.$table" out >(cat) -c -t'\t' -r'\n' -d "$database" "${@:4}" >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment