0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL Server 2017 Expressの操作

Last updated at Posted at 2021-07-31

概要

SQL Server 2017 Expresの基本的な操作について記載しています。

環境

パラメータ
OSのホスト名 SQL
インスタンス名 SQLEXPRESS
認証方法 Windows認証
データベース名 test

操作

SQL Server 2017 構成マネージャの設定変更

SQL Server 2017 Expressはデフォルト値として、TCP/IPの通信が
無効となりますので、有効に変更します。

1.PowerShellを管理者で起動する。

2.以下コマンドでTCP/IPを有効とする。
※ManagedComputer[@Name='SQL']の箇所はOSのホスト名を入力する。
※ServerInstance[@Name='SQLEXPRESS']の箇所はインスタンス名を入力する。


$wmi = New-Object 'Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer'
$tcp = $wmi.GetSmoObject("ManagedComputer[@Name='SQL']/ServerInstance[@Name='SQLEXPRESS']/ServerProtocol[@Name='Tcp']")
$tcp.IsEnabled = $true
$tcp.Alter()

3.SQL Serverのサービスを再起動する。


Restart-Service -Name 'MSSQL$SQLEXPRESS'

4.SQL Serverに接続する。

「sqlcmd」コマンドを使用してSQL Serverに接続する時に
SQLEXPESSのインスタンス名を明示的に指定する必要があります。


sqlcmd -S localhost\SQLEXPRESS


5.データベースを作成する。


create database test;

GO

6.データベースのバックアップをする。

※SQL Server Management Studioでバックアップスクリプトを作成したときのコマンドとなります。


BACKUP DATABASE [test] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test.bak' WITH NOFORMAT, NOINIT,  NAME = N'test-完全 データベース バックアップ', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO


7.データベースのリストアをする。

※SQL Server Management Studioでリストアスクリプトを作成したときのコマンドとなります。


USE [master]
RESTORE DATABASE [test] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5

GO


0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?