Bcp and linked servers to Azure

Bcp and linked servers to Azure

Bcp and linked servers to Azure

SQLShack

SQL Server training Español

Bcp and linked servers to Azure

July 13, 2015 by Daniel Calbimonte

Introduction

Sometimes, we need to use the command line to copy tables using the command line. In this new chapter, we will use the BCP command to copy tables from a local machine to the SQL Server in Azure. The BCP is a very fast bulk copy tools used for this purpose. We will also learn how to create a Linked Server in our local machine to Azure. The Linked Servers are very powerful features of SQL Server to connect SQL Server to different Data Sources like Excel, MS Access, Oracle, MySQL, and several databases. In this example, we will connect to SQL Azure.

Requirements

An Azure subscription. A VM Machine in Azure with SQL Server already installed (check my article about Azure VMs). The AdventureWorks database installed in the source and destination columns (you can create your own tables if you prefer). A local machine with Windows installed. A SQL Server Management Studio (SSMS) connected to Azure (check our article related here).

Getting started

In order to start, in the local machine open the command prompt.
Figure 1. The cmd In the command prompt, run the following bcp command: bcp adventureworks2014.humanresources.department out c:\s
cripts\department.dat -S localhost -T -n –q The command copies the data of the table HumanResources.Department of the Adventureworks2014 database to the file department.dat. The –S parameter is the Server name, the –T means to use a Trusted connection (Windows authentication). The –n parameter is used to perform the bulk copy operation using the native datatypes. This option is very important to increase the copy with a good performance. The –q parameter means to Set Quoter identifiers which means to use a qualified name (database name, owner and table name). If Everything is fine, the command will display a message similar to this one:
Figure 2. Copying rows to the dat file Now we need an empty table in the Azure machine. In the Azure machine, go to the SSMS, AdventureWorks database, select the HumanResources.Department table and select the option Script Table as ➜ Create To ➜ New Query Window:
Figure 3. Generating T-SQL code to create a new table This option will generate the T-SQL code of the table. Now modify the code like this (or copy this code to create a new table): 1234567891011121314 CREATE TABLE [HumanResources].[Department2]( [DepartmentID] [smallint] IDENTITY(1,1) NOT NULL, [Name] [dbo].[Name] NOT NULL, [GroupName] [dbo].[Name] NOT NULL, [ModifiedDate] [datetime] NOT NULL CONSTRAINT [DF_Department_ModifiedDate2] DEFAULT (getdate()), CONSTRAINT [PK_Department_DepartmentID2] PRIMARY KEY CLUSTERED ( [DepartmentID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] GO The T-SQL code used creates an empty table named department2 which will be used to upload data from the local machine. Now run the T-SQL code of the step 8 to create the table.
Figure 4. The table created Finally, run the following command to import data in Azure: bcp adventureworks2014.humanresources.department2 in c:\ cripts\department.dat -n -U daniel -S tcp:olapsqldan.cloudapp.net -P Myp@wd1 We use the in to import data and we specify the .dat file created in the step 2. The –n is parameter was explanined in step 3. The –U parameter specifies the SQL Azure User Name. The –P parameter is used to set the SQL Azure password. Finally, the –S specifies the Azure Server Name which can be displayed in the Azure Portal. The message displayed by the command will be similar to this one: Figure 5. Copying rows to Azure If everything is OK, you will be able to see data in your table in your Azure machine.
Figure 6. The data copied to Azure. As you can see, copying data from your local machine to Azure is a straightforward process. Now let’s just to another topic. The Linked Servers.

Linked Server to Azure

Another method to connect and copy data from your local machine to Azure is using Linked Servers. The Linked Servers in SQL Server let you connect your SQL Database with other Data Bases like Access, Oracle, MySQL, etc. In this new example, we are going to connect and create a Linked Server to a SQL Azure machine. In order to start, connect and open the SSMS.
Figure 7. The SSMS Go to Server Objects ➜ Linked Servers and right click on it and select the New Linked Server option.
Figure 8. The linked Server In the general page, in the Linked server text box, write the DNS name of the SQL Azure. In the Server type, select SQL Server.
Figure 9. The SQL Azure created as a Linked Server. Now we need to specify the Security credentials. Go to the Security page and in the Local Login, select a SQL Server local user. In Remote user specify the user name used to login to SQL Azure. Finally, in Remote Password specify the password of the remote user. Figure 10. Linked Server Security Settings. If everything is fine, you will be able to see the Databases and tables that belong to the SQL Azure in the Catalogs folder.
Figure 11. The Linked Server just created. Alternately, you can use the T-SQL commands to create the Linked Server: 123 EXEC master.dbo.sp_addlinkedserver @server = N'OLAPSQLDAN.CLOUDAPP.NET', @srvproduct=N'SQL Server' The system Procedure addlinkedserver is used to add the server. The Server name is the name of the Azure SQL Server. The product is SQL Server. The next part is the security. 1234 EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'OLAPSQLDAN.CLOUDAPP.NET',@useself=N'False',@locallogin=N'PALADIN\Administrator',@rmtuser=N'daniel',@rmtpassword='########' The system procedure sp_addlinkedsrvlogin is used to add logins to connect to Azure. The @rmtsrvname contains the name of the remote Server. In this case, the Azure machine. The @useself parameter is to use the current account. The @locallogin parameter specifies the local login and @rmtuser and @rmtpasswords are used to specify the Azure user name and azure password respectably. In order to test the Linked Server, you can query the Linked Server using the OPENQUERY command: 123 SELECT * FROM OPENQUERY([OLAPSQLDAN.CLOUDAPP.NET], 'SELECT * FROM Adventureworks2014.[HumanResources].[Department]') You will be able to see the data in your local machine from your SQL Azure table. Figure 12. The Linked Server query results You can simple use the full name instead of using the OPENQUERY: 1234 SELECT * FROM [OLAPSQLDAN.CLOUDAPP.NET].Adventureworks2014.[HumanResources].[Department] Now, you can insert, delete data using the full name.

Conclusion

In this chapter, we learned two things: First, we learned how to copy data using the command line and specifically the BCP command from the local SQL Server to Azure. Secondly, we learned how to connect our local database to Azure using the Linked Servers. 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 create, configure and drop a SQL Server linked server using Transact-SQL How to create and configure a linked server in SQL Server Management Studio An introduction to the bcp Utility (bulk copy program) in SQL Server How to query Excel data using SQL Server linked servers How to create a linked server to an Azure SQL database 3,037 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!