> ## Documentation Index
> Fetch the complete documentation index at: https://docs.morph-data.io/llms.txt
> Use this file to discover all available pages before exploring further.

# SQLServer

## SQLServer に接続する

コネクターについての概要は以下のリンクを参照してください。

* [DB/SaaSと接続する](https://docs.morph-data.io/docs/ja/develop/guides/integration.md)

> [tab] ダッシュボード
>
> **1. Connectorを作成する**
>
> トップページの「Connectors」タブを選択し「Create」ボタンを押下します。
>
> ![Connection](https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-connection.png?fit=max\&auto=format\&n=evgQaQjX53Vch8Y5\&q=85\&s=19c6e7afc70da565c2a8ab4f75469549)
>
> **2. SQLServerの認証情報を入力する**
>
> 表示されたフォームに接続するデータベースの認証情報を入力します。
> リモートサーバー経由で接続する場合は「SSH」の欄にチェックを入れ任意の項目も入力します。
>
> 入力後Createボタンを押下すると作成が完了します。パラメータに不備がある場合はエラーが表示されるため接続内容が正しいか再度ご確認ください。
>
> ![SQLServer Connection Create](https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-mssql-create.png?fit=max\&auto=format\&n=evgQaQjX53Vch8Y5\&q=85\&s=be60f30dbb79da9a92a2f678e9563e98)
>
> | 項目名 | 説明 | 必須 | 例 |
> | - | - | - | - |
> | `Host` | データベースのホスト名を入力します。 | ✅ | `example.com` |
> | `Port` | データベースの接続ポートを入力します。 | ✅ | `1433` |
> | `Database` | データベース名を入力します。 | ✅ | `my_database` |
> | `Username` | データベースのユーザー名を入力します。 | ✅ | `db_user` |
> | `Password` | データベースのユーザーのパスワードを入力します。 | ✅ | `mypassword` |
> | `SSH` | SSH接続を使用する場合に指定します。 | | |
> | `SSH Host` | リモートサーバーのホスト名を入力します。 | | `ssh.example.com` |
> | `SSH Port` | リモートサーバーの接続ポートを入力します。 | | `22` |
> | `SSH Username` | リモートサーバーのユーザー名を入力します。 | | `ssh_user` |
> | `SSH Password` | リモートサーバーのユーザーのパスワードを入力します。 | | `sshpassword` |
> | `SSH Private Key` | リモートサーバーの秘密鍵を入力します。 | | `-----BEGIN RSA PRIVATE KEY-----` |
> | `SSH Passphrase` | リモートサーバーの秘密鍵のパスフレーズを入力します。 | | `my_passphrase` |
>
> > [!WARNING]
> >
> > 接続先のデータベースの IP ホワイトリストに必ず`54.150.149.0/32`からのアクセスを許可するように設定してください。
>
> **3. 作成したデータベースに接続する**
>
> 作成が成功すると一覧表示からコネクター名を取得することができます。アイコン横の文字列が`connection_name`です。これを使用することでコード上でデータベースに接続可能です。
>
> ![SQLServer Connection Create Complete](https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-mssql-complete.png?fit=max\&auto=format\&n=evgQaQjX53Vch8Y5\&q=85\&s=720834f4f89ceef607f74bde1a97bc6c)
>
> SQLまたはPythonのコードに作成したコネクター名を入力しデータを取得します。
>
> ```sql SQL theme={"dark"}
> {{
>     config(
>         connection="connection_name"
>     )
> }}
>
> select * from table_name
> ```
>
> ```python Python theme={"dark"}
> import morph
> from morph import MorphGlobalContext
> from morph_lib.database import execute_sql
>
>
> @morph.func
> def main(context: MorphGlobalContext):
>     df = execute_sql(
>         sql="SELECT * from table_name"
>         connection="connection_name"
>     )
>     return df
> ```

> [tab] ローカル
>
> **1. morph initコマンドを実行する**
>
> `morph init`コマンドを実行してDBの接続情報を`~/.morph/connections.yml`に保存します。
>
> ```bash Shell theme={"dark"}
> morph init
> ```
>
> > [!WARNING]
> >
> > morphパッケージのインストールを行なっていない場合は事前に下記のコマンドでインストールしてからお進みください。
> >
> > ```bash shell theme={"dark"}
> > pip install morph-data
> > ```
>
> 対話式のインターフェースでデータベースの種類一覧が表示されるのでSQLServerを選択します。
>
> ```bash Shell theme={"dark"}
> Select your database type:
> 1. PostgreSQL
> 2. MySQL
> 3. Redshift
> 4. SQLServer
> 5. Snowflake (User/Password)
> 6. BigQuery (Service Account)
> Enter the number corresponding to your database type: 4
>
> SQLServer selected.
> ```
>
> 続いてデータベースの認証情報を入力します。`slug`はコネクター名として扱うもので、この名前をSQLもしくはPythonで指定します。
>
> 以下の入力例は実際に接続できる値に置き換えてください。
>
> ```bash shell theme={"dark"}
> Create a slug for your connection: sqlserver-connection
> Enter your SQLServer host (default: localhost): localhost
> Enter your SQLServer username: your_user_name
> Enter your SQLServer password: your_password
> Enter your SQLServer port (default: 1433): 1433
> Enter your SQLServer database name: your_database_name
> Enter your SQLServer schema name (default: public): public
> Select if you are using SSH tunneling (y/n): n
> ```
>
> 認証情報の保存が完了すると以下のメッセージが表示されます。
>
> ```bash shell theme={"dark"}
> Successfully initialized! 🎉
> You can edit your connection details in `path_to_connections.yml`
> ```
>
> `connections.yml`は以下のように保存されています。他のコネクターを作成した場合は`connections`以下に追加されます。
>
> ```bash shell theme={"dark"}
> cat ~/.morph/connections.yml
> ```
>
> ```yaml theme={"dark"}
> connections:
>   sqlserver-connection: # コネクター名
>     dbname: your_database_name
>     host: localhost
>     password: your_password
>     port: 1433
>     schema_: public
>     ssh_host: null
>     ssh_password: null
>     ssh_port: null
>     ssh_private_key: null
>     ssh_user: null
>     type: mssql # 固定
>     user: your_user_name
> ```
>
> **2. コード上でコネクターを使いデータを取得する**
>
> SQLまたはPythonのコードに作成したコネクター名を入力しデータを取得します。
>
> ```sql SQL theme={"dark"}
> {{
>     config(
>         connection="connection_name"
>     )
> }}
>
> select * from table_name
> ```
>
> ```python Python theme={"dark"}
> import morph
> from morph import MorphGlobalContext
> from morph_lib.database import execute_sql
>
>
> @morph.func
> def main(context: MorphGlobalContext):
>     df = execute_sql(
>         sql="SELECT * from table_name"
>         connection="connection_name"
>     )
>     return df
> ```
>
> > [!WARNING]
> >
> > 実際にコネクターを指定してファイルを実行した場合の優先順位は以下の通りです。
> > また`morph deploy`を行なった環境ではダッシュボードで作成したコネクターのみが使用可能なため注意してください。
> >
> > 1. ローカルマシンの`~/.morph/connections.yml`に記載されたコネクター
> > 2. クラウドで登録されたコネクター


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.