1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bcpコマンド入門

1
Last updated at Posted at 2026-02-20

はじめに

SQL Server環境で大量データの移行を行うことになりました。
そこで登場するのが bcp(Bulk Copy Program) です。

本記事では、bcpコマンドの基本を整理します。

bcpコマンドとは

bcpコマンドは、SQL Serverに付属するコマンドラインツールで、テーブルとファイル間で大量データを高速にコピーするためのユーティリティです。

用途

  • テーブル → ファイル(エクスポート)
  • ファイル → テーブル(インポート)
  • 大量データの高速ロード
  • バッチ連携・データ移行

特徴

  • 非常に高速(ログ最小化可能)
  • コマンドラインで自動化しやすい
  • CSVファイルに対応
  • ネットワーク経由のコピーも可能

基本構文

この記事では以下の観点で使用するパラメータに絞って解説します。

  • 動作モード
  • 出力のデータ形式
  • 区切り文字
  • 接続
  • 実行制御
bcp {table_name | query} {in | out | queryout} data_file
    [-n]
    [-c]
    [-w]
    [-t field_term]
    [-r row_term]
    [-S server_name]
    [-U login_id]
    [-P password]
    [-T]
    [-d database_name]
    [-b batch_size]
    [-F first_row]
    [-L last_row]
    [-m max_errors]
    [-e err_file]

動作モード

{table_name | query} {in | out | queryout} data_file
  • ファイルの内容をテーブルに登録
    table_name in data_file

「user.csv」の内容を「m_user」に登録
bcp m_user in user.csv -c -S localhost -T -d sample_db

  • テーブルの内容をファイルに出力
    table_name out data_file

「m_user」の内容を「user.csv」に出力
bcp m_user out user.csv -c -S localhost -T -d sample_db

  • クエリの内容をファイルに出力
    query queryout data_file

「SELECT * FROM m_user WHERE IsActive = 1」の結果を「active_users.csv」に出力
bcp "SELECT * FROM m_user WHERE IsActive = 1" queryout active_users.csv -c -S localhost -T -d sample_db

出力のデータ形式

[-n]
[-c]
[-w]

バイナリ形式で出力
bcp m_user out user.dat -n -S localhost -T -d sample_db

ANSI形式(Shift_JIS)で出力
bcp m_user out user.csv -c -S localhost -T -d sample_db

Unicode形式(UTF-16)で出力
bcp m_user out user.csv -w -S localhost -T -d sample_db

区切り文字

[-t field_term]
[-r row_term]
  • 列区切り文字
    -t field_term

カンマ区切りで出力
bcp m_user out user.csv -c -t "," -S localhost -T -d sample_db

  • 行終端
    -r row_term

行末をLfで改行
bcp m_user out user.csv -c -r "\n" -S localhost -T -d sample_db

Windows版bcpでは、テキストモード出力時に \n 指定でも \r\n に正規化される場合があります。
\n が必要な場合は、出力後にPowerShell等で変換するのが最も確実です。

DB接続

[-S server_name]
[-U login_id]
[-P password]
[-T]
[-d database_name]
  • サーバーを指定
    -S server_name

localhostを指定
bcp m_user out user.csv -c -S localhost -T -d sample_db

  • SQL認証
    -U login_id
    -P password

sa(パスワード:P@ssw0rd)で認証
bcp m_user out user.csv -c -S localhost -U sa -P P@ssw0rd -d sample_db

  • Windows認証
    -T

Windows認証
bcp m_user out user.csv -c -S localhost -T -d sample_db

  • データベースを指定

sample_dbを指定
bcp m_user out user.csv -c -S localhost -T -d sample_db

実行制御

[-b batch_size]
[-F first_row]
[-L last_row]
[-m max_errors]
[-e err_file]
  • コミット単位
    -b batch_size

10000件ごとにコミット
bcp m_user in user.csv -c -b 10000 -S localhost -T -d sample_db

  • 分割ロード
    -F first_row
    -L last_row

20行目から1000行目までをインポート
bcp m_user in user.csv -c -F 20 -L 1000 -S localhost -T -d sample_db

  • 指定回数エラーで停止
    -m max_errors

10回エラー発生で停止
bcp m_user in user.csv -c -m 10 -S localhost -T -d sample_db

  • エラーログを出力
    -e err_file

エラーログ「error.log」を出力
bcp m_user in user.csv -c -e error.log -S localhost -T -d sample_db

応用構文

日本語CSVエクスポート

  • -w ⇒ Unicodeで文字化け防止
  • -t "," ⇒ CSV形式

bcp m_user out user.csv -w -t "," -r "\n" -S localhost -T -d sample_db

大量データ高速インポート

  • -b 10000 ⇒ バッチサイズ指定
  • -e error.log ⇒ エラー行退避

bcp m_user in users.csv -n -b 10000 -e error.log -S localhost -T -d sample_db

おわりに

bcpコマンドは、SQL Server環境における大量データ処理の基礎ツールです。
ポイントは次の通りです。

  • 高速な大量データ入出力が可能
  • コマンドラインで自動化しやすい
  • 本番ではエラー出力(-e)は必須

bcpコマンドを使ったデータ移行の手順はこちら↓

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?