How to copy an Azure SQL database using the Azure Portal Cloud Shell and T SQL

How to copy an Azure SQL database using the Azure Portal Cloud Shell and T SQL

How to copy an Azure SQL database using the Azure Portal Cloud Shell and T-SQL

SQLShack

SQL Server training Español

How to copy an Azure SQL database using the Azure Portal Cloud Shell and T-SQL

June 23, 2017 by Daniel Calbimonte This article will provide an overview covering programmatically moving databases on the Azure Portal while avoiding common problems with users and logins.

Introduction

Sometimes we need to create copies of a database when we migrate a database from our testing environment to Production or maybe when we want to create a new database based on a template database. Or sometimes to test the database and make some experiments without affecting the production database or to have a replica to reduce the overhead or for security reasons. In Azure, it is possible to create Azure SQL database copies between Azure SQL Servers. If you have an Azure AD authentication, it can be a straight forward process. However, if you use a SQL Authentication, you may require additional steps. In this article, we will show some tips to migrate Azure SQL logins and users between different Azure SQL Servers using the portal, Cloud Shell and T-SQL. In this article, we will show how to do the following tasks using the Azure Portal: Copy a database using the Azure Portal How to migrate logins from one Azure SQL Server to another (common problems) using the Azure Portal Show how to avoid login migration problems using the Azure Portal How to copy an Azure SQL database using the Cloud Shell using the Azure Portal How to copy an Azure SQL database using T-SQL using the Azure Portal

Requirements

A subscription to Azure Portal An Azure SQL Source Database An Azure SQL target database A local SQL Server Management Studio (SSMS)

Get started

How to copy a database using the Azure Portal

We will first copy a database with a login from a source Azure SQL Server to another. In this example, we have a source database named sqlshackdb stored in the server sqlshackserver: Figure 1. The source database You can search the SQL Servers in Azure by clicking on the > icon and selecting SQL servers in the Azure Portal: Figure 2. The target Server Our Azure SQL Server name is sqlshackdestination. This will be the destination Server where we will create a copy of the source database: Figure 3. Target SQL Server We will create a login to verify common problems when we copy an Azure SQL Database to another Azure SQL Server using the Azure Portal. To do this, we will enable the firewall rule to connect with a local SSMS: Figure 4. Enabling a firewall rule in Azure to allow the local computer to connect to Azure SQL Source server In SSMS, connect to a Database Engine: Figure 5. Connection to Azure SQL Specify your credentials to connect to the Azure SQL Source database: Figure 6. Connecting to Azure SQL Source database The following code will create an Azure SQL Login named “psmith” and a database user associated to that login: 12345678 CREATE LOGIN psmithWITH PASSWORD = 'ThisIsmySupperstrongpwd!#' GO CREATE USER psmithFOR LOGIN psmithWITH DEFAULT_SCHEMA = dboGO In the Azure Portal, select the database and press the copy icon: Figure 7. The copy option Specify the Target server. The name of the database copy. You can also choose the price tier for the new database and press OK: Figure 8. Copy options If everything is OK, a copy of the database should be created: Figure 9. The database created

How to migrate logins from one Azure SQL Server to another common problems using Azure Portal

To connect to the target server using SSMS, you will need to enable the firewall: Figure 10. Adding Firewall rules in the Target Server to open with SSMS If we check the logins in the source and destination, we will notice that only the database owner was migrated, but not the login “jsmith”: Figure 11. The logins are not copied when we copy a database This is because the logins are stored in the master database. That is why, if you need to migrate the logins, you may need to migrate them separately. The following query can generate scripts to create the logins manually in Source Server and in the master database: 123 SELECT 'create login '+name +' with password =''Mypws1234!!''' from sys.sql_loginswhere type='s' Basically, the T-SQL query will generate CREATE LOGIN sentences with the same password for each user. The query shows all the logins of type SQL Server (type=’s’). You will need to copy the results of the query and execute the sentences in the target server to generate the logins. To list the SQL Server logins, you can run the following query in the master database: 1 select * from sys.sql_logins Note that the option MUST_CHANGE used to change the password is not supported in Azure SQL. To avoid this problem, you can create contained database users. In the next section, we will show how to do it.

How to create contained users and avoid login migration problems

In a contained database, the database users can login directly without a SQL login. You can have a database user with a password. If you create contained users, when you copy a database from a source to a destination, you do not need extra effort to copy the logins separately. The following example will show how to create a database user without a traditional login in the master database: 1 CREATE USER containeduser WITH PASSWORD = 'ThisIsmySupperstrongpwd!#'; As you can see, the login is not required with this option. The database user includes a password. You can optionally add it to a database role if necessary. The disadvantage with a contained user is that if you cannot assign If you create a copy of the database as we did in figure 7 and 8, you will have a new database, but now you will have a user named “containeduser” that can login directly: Figure 12. The user, containeduser, is copied when you copy a database because it is inside it To login with a database contained user, go to SSMS and specify the Azure SQL Server name and login credentials: Figure 13. Credentials to login to Azure You will need to specify the database. Press the Options >> button and in the Connect to database combo box, specify the database name: Figure 14. With a contained user, you need to specify the database

How to copy an Azure SQL database using the Cloud Shell in Azure Portal

If you want to automate administrative tasks, the Cloud Shell will help you a lot. Now, you can run bash to automate your tasks and this year you will also be able to run PowerShell using the console. With bash, you can automate tasks using the command line. As we explained in a previous chapter, you do not need to install the Azure Client in the local machine to run the Cloud Shell. Azure now allows you to run the Shell using the Azure Portal. To test this feature, press the >_ icon: Figure 15. The Cloud Shell You will connect to the Cloud Shell: Figure 16. Connecting to the terminal In the Cloud Shell, we will create a copy of the Azure SQL database using the following commands: az sql db copy –dest-name azdbcopy -n sqlshackdb -g mynewgp -s sqlshackserver –dest-resource-group group1 –dest-server sqlshackdestination az sql db copy is the command to copy an Azure SQL database. Azdbcopy is the name of the copy in the destination database. Sqlshackdb is the source database to be copied Mynewgp is the name of the source resource group where the source Azure sql database was created. Sqlshackserver is the name of the Azure SQL Server Source. Group1 is the name of the target resource group. Finally, sqlshackdestination is the name of the target Azure SQL Server where we will create the database copy. If everything is OK, a destination database named “sqlshackdestination” will be displayed: Figure 17. The database copied

How to copy an Azure SQL database using T-SQL

You can also create a database copy using T-SQL. The following example shows how to create the copy in SSMS. You can run this sentence in the master database: 1 CREATE DATABASE tsqldatabasecopy AS COPY OF sqlshackserver.sqlshackdb Where tsqldatabasecopy is the destination database and sqlshackserver.sqlshackdb is the name of the Azure SQL Server and the Azure SQL Database name separated by a “.”. You can verify that the new database was created successfully in SSMS or the Azure Portal: Figure 18. The new database created using T-SQL

Conclusion

In this article, we learned how to create a copy of an Azure SQL database using the Azure Portal, the Cloud Shell and T-SQL. We also learned how to create contained users to avoid the problems to migrate logins from one Azure SQL Server to another. We also create a query to generate code to create Azure SQL logins to manually create logins in the destination Server. The Azure Portal is a powerful and effective medium to manage your SQL Azure databases. I hope this article helps you understand how to utilize it more effectively, for common tasks like moving databases. Other articles in this series: How to migrate MySQL tables to Microsoft Azure SQL database How to create an Azure SQL Database using the Cloud Shell Working with Azure Active Directory and Azure SQL Database How to automate Azure Active Directory (AAD) tasks using the Cloud Shell Author Recent Posts Daniel CalbimonteDaniel Calbimonte is a Microsoft Most Valuable Professional, Microsoft Certified Trainer and Microsoft Certified IT Professional for SQL Server. He is an accomplished SSIS author, teacher at IT Academies and has over 13 years of experience working with different databases.

He has worked for the government, oil companies, web sites, magazines and universities around the world. Daniel also regularly speaks at SQL Servers conferences and blogs. He writes SQL Server training materials for certification exams.

He also helps with translating SQLShack articles to Spanish

View all posts by Daniel Calbimonte Latest posts by Daniel Calbimonte (see all) SQL Partition overview - September 26, 2022 ODBC Drivers in SSIS - September 23, 2022 Getting started with Azure SQL Managed Instance - September 14, 2022

Related posts

How to migrate MySQL tables to Microsoft Azure SQL database How to create an Azure SQL Database using the Cloud Shell SQL Server 2014 contained databases How to migrate users to a partially contained database in SQL Server Getting started with Azure CLI 2.0 13,149 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ►Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ▼Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ►Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ►Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!