Skip to content

Commit

Permalink
Provided the full script for copy between subscriptions
Browse files Browse the repository at this point in the history
Customers open a lot of support requests for copy between subscriptions. The finding the SID part might not be something they are familiar with. I have at least handled 10 customers with the same ask and I have always provided this script. It is better to have it in the doc itself to avoid support requests.
  • Loading branch information
ronazi authored Aug 28, 2020
1 parent 8a7b82d commit 23257b6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions articles/azure-sql/database/database-copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,46 @@ CREATE DATABASE Database2 AS COPY OF server1.Database1;

You can use the steps in the [Copy a SQL Database to a different server](#copy-to-a-different-server) section to copy your database to a server in a different subscription using T-SQL. Make sure you use a login that has the same name and password as the database owner of the source database. Additionally, the login must be a member of the `dbmanager` role or a server administrator, on both source and target servers.

```sql
Step# 1
Create login and user in the master database of the source server.

CREATE LOGIN loginname WITH PASSWORD = 'xxxxxxxxx'
GO
CREATE USER [loginname] FOR LOGIN [loginname] WITH DEFAULT_SCHEMA=[dbo]
GO

Step# 2
Create the user in the source database and grant dbowner permission to the database.

CREATE USER [loginname] FOR LOGIN [loginname] WITH DEFAULT_SCHEMA=[dbo]
GO
exec sp_addrolemember 'db_owner','loginname'
GO

Step# 3
Capture the SID of the user “loginname” from master database

SELECT [sid] FROM sysusers WHERE [name] = 'loginname'

Step# 4
Connect to Destination server.
Create login and user in the master database, same as of the source server.

CREATE LOGIN loginname WITH PASSWORD = 'xxxxxxxxx', SID = [SID of loginname login on source server]
GO
CREATE USER [loginname] FOR LOGIN [loginname] WITH DEFAULT_SCHEMA=[dbo]
GO
exec sp_addrolemember 'dbmanager','loginname'
GO

Step# 5
Execute the copy of database script from the destination server using the credentials created

CREATE DATABASE new_database_name
AS COPY OF source_server_name.source_database_name
```

> [!NOTE]
> The [Azure portal](https://portal.azure.com), PowerShell, and the Azure CLI do not support database copy to a different subscription.
Expand Down

0 comments on commit 23257b6

Please sign in to comment.