26
28

More than 5 years have passed since last update.

コマンドプロンプトでSQL Serverを使う

Posted at

SQLServerを少しだけ触る機会があったため、基本なコマンドをまとめます。
sqlcmdユーティリティが必要になります。
<>内はご自身の環境に合わせて置き換えてください。

SQLServer認証でログイン

sqlcmd -S <host> -U <user> -P <password>

接続できると1>と表示される。

ポートを指定したい場合は、host,port とする。

ex
sqlcmd -S hostname,1433 -U user -P password

データベースの指定も含むと以下のようになる。

sqlcmd -S <host> -U <user> -P <password> -d <database>

データベース一覧を表示する

select name from sys.databases;
go

命令文のあとにgoが必要になる。
goがないと実行されない。

テーブルの一覧を表示する

select name from sysobjects where xtype = 'U';
go

パスワードの変更

sqlcmd -S <host> -U <user> -P <current-password> -Z <new-password>

データベースの作成

create database <database-name>;
go

データベースの削除

drop database <database-name>;
go

データベースの指定

use <database-name>;
go
26
28
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
26
28