write ahead log

ロールフォワード用

bashとbcpを使ってSQLServerのデータをCSVへ出力した時のメモ

WSL2のUbuntuへbcpをインストールして行った。

bcpのインストールはこちらから。

Sql Server Command-Line Tools on Linux をインストールする - SQL Server | Microsoft Learn

スクリプトはこんな感じで雑に描いた。

#!/bin/bash

# bcpコマンドのインストールが必要です
# https://learn.microsoft.com/ja-jp/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver16&tabs=ubuntu-install#ubuntu
rm -rf export_csv
mkdir export_csv

# エクスポートするテーブル
TABLES=("sample_table_1" "sample_table_2" "sample_table_3")

for TABLE in "${TABLES[@]}"; do
    bcp "SELECT * FROM sample_schema.${TABLE}" queryout "./export_csv/${TABLE}.csv" -c -t, -S localhost -d sample_db-U sa -u -P saNoPassword
done

一応出力できたけど、bcpがちゃんとエスケープ処理までやってCSVを出してはくれないので困る。

とりあえずやる分には便利。