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

More than 5 years have passed since last update.

SQLserverでDBを異なるスキーマ名に変更する手順

Posted at

復元したDBを異なるスキーマ名のDBとする

通常、復元したDBを異なるDB名にした場合、スキーマ名は元々のスキーマ名を保持しているので、
全TBLのスキーマ名の変更が必要になってくる。
1件、1件やるのは効率が悪いので、SELECT文にて、一括変更用のSQLを作成する。

変更方法

  1. 下記SQLを実行する。
     ※afterschemaは、変更後のスキーマ名を設定
      beforeschemaは変更前のスキーマ名を設定
sql
SELECT
    'alter schema afterschema transfer beforeschema.' + RTRIM(A.name) + ';' AS TableName
FROM
    sys.objects AS A
    JOIN
        sys.sysindexes AS B
    ON  A.object_id = B.id
    AND B.indid < 2
WHERE
    A.type = 'U'
ORDER BY
    A.name

実行結果
image.png
2. 実行結果に表示されたSQL文を実行する。
 2.1. 実行結果を全選択
 2.2. 「alter schema afterschema transfer beforeschema.dtproperties;」は実行するSQLから除く事。
  →スキーマ名を変えてはいけないテーブルです(> <)
 2.3. 2.2.で除いたSQL以外を実行する。
image.png

※DBを復元した状態だと、ユーザーID等が紐づいてない可能性有。
 上記手順を行う前に、ユーザーの紐づき直しを行わないといけない可能性有。
  参考 DBを復元した場合のユーザー紐づけ方法

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