3

I need Ur help regarding Join query between two different Azure SQL database with same table name and same structure. Actually I have two different database with same table name and whole fields with different data.

I would like to Transfer the data from One database to another on Azure Sql or Join the table (both the table have the same name and structure). For this I am using the "Elastic Database Query"

https://ppolyzos.com/2016/07/30/cross-database-queries-in-azure-sql-databases/

I am facing the error message while creating External Table “CREATE EXTERNAL TABLE [dbo].[Users]”

Error Message :- "There is already an object named 'Users' in the database."

Sample Database 1st

[Test].[dbo].[Users]

CREATE TABLE [Users] (
  [UserGuid] uniqueidentifier NOT NULL
, [FirstName] nvarchar(90)  NOT NULL
, [LastName] nvarchar(90)  NOT NULL
, [UserID] nvarchar(50)  NOT NULL
, [PasswordSalt] nvarchar(90)  NOT NULL
, [Password] nvarchar(90)  NOT NULL
, [PasswordChanged] datetime NULL
, [UserName] nvarchar(50)  NOT NULL
);

GO


Database 2nd [TestAnother].[dbo].[Users]

CREATE TABLE [Users] (
  [UserGuid] uniqueidentifier NOT NULL
, [FirstName] nvarchar(90)  NOT NULL
, [LastName] nvarchar(90)  NOT NULL
, [UserID] nvarchar(50)  NOT NULL
, [PasswordSalt] nvarchar(90)  NOT NULL
, [Password] nvarchar(90)  NOT NULL
, [PasswordChanged] datetime NULL
, [UserName] nvarchar(50)  NOT NULL
);
GO

I have written the Elastic Database Query as below :

Use TestAnother
Go

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ABCmn!A4F43';

CREATE DATABASE SCOPED CREDENTIAL MyDbCredential 
WITH IDENTITY = 'TestID',
SECRET = 'ABC9mn!A4F43';

CREATE EXTERNAL DATA SOURCE Connect2Test
WITH (
TYPE=RDBMS,
LOCATION='XXXX.database.windows.net',
DATABASE_NAME='Test',
CREDENTIAL= MyDbCredential,);

CREATE EXTERNAL TABLE [dbo].[Users] (
  [UserGuid] uniqueidentifier NOT NULL
, [FirstName] nvarchar(90)  NOT NULL
, [LastName] nvarchar(90)  NOT NULL
, [UserID] nvarchar(50)  NOT NULL
, [PasswordSalt] nvarchar(90)  NOT NULL
, [Password] nvarchar(90)  NOT NULL
, [PasswordChanged] datetime NULL
, [UserName] nvarchar(50)  NOT NULL 
)
WITH ( 
        DATA_SOURCE = Connect2Test);
GO

If I remove or rename the Users table on "TestAnother" database then its work fine. But I wanna use the same Table name and structure.

How can I migrate the data from one table from One database to another table on other database and both the database table name and structure are same on Azure Sql environment.

6
  • do you just want to migrate the data? Commented Oct 15, 2018 at 14:58
  • Both Database sitting in the same server? Commented Oct 15, 2018 at 15:14
  • HI Both are on the same Server. I want I have a multiple database (from different customers) on the same server and I wanna club the unique data from all and generate export on our main database finally. Commented Oct 15, 2018 at 15:17
  • You could possibly do this with an Elastic Database Job. Create a Target Group with each of the databases with the same dbo.Users table and execute the query against them and stored the output in another table. If each of the databases has exactly the same structure but has data for different customers then I'd also consider a Sharded approach if you can add an extra key to each table with the customerid. Commented Oct 15, 2018 at 15:44
  • @skt, could you please elaborate: is this a migration task i.e. that will be executed a few times only; or is this meant to be part of some OLTP-type queries? Commented Oct 15, 2018 at 18:42

2 Answers 2

2

You need to create the external table with different name, and define its schema and real name in the create, bellow

CREATE EXTERNAL TABLE [dbo].[ExternalUsers] (
  [UserGuid] uniqueidentifier NOT NULL
, [FirstName] nvarchar(90)  NOT NULL
, [LastName] nvarchar(90)  NOT NULL
, [UserID] nvarchar(50)  NOT NULL
, [PasswordSalt] nvarchar(90)  NOT NULL
, [Password] nvarchar(90)  NOT NULL
, [PasswordChanged] datetime NULL
, [UserName] nvarchar(50)  NOT NULL 
)
WITH ( 
        DATA_SOURCE = Connect2Test,
SCHEMA_NAME = 'dbo',
 OBJECT_NAME = 'Users');
Sign up to request clarification or add additional context in comments.

Comments

0

You want to migrate the data from one table from One database to another table on other database and both the database table name and structure are same on Azure Sql environment, just table-to-table, am I right? Since your two databases are in the same SQL server, you do this operation with SSMS(SQL Server Management Studio).

Login your Azure sql database with SSMS, choose the “SQL Server Authentication”:

https://i.sstatic.net/G6h2Q.png

Select your databse and create a new query, I have tried to migrate the data from table MyDatabase2.dbo.Users to the table MyDatabase.dbo.Users successfully by using these code:

INSERT INTO MyDatabase.dbo.Users (
  FirstName,
  LastName,
  UserID,
  PasswordSalt,
  Password,
  PasswordChanged,
  UserName) 
SELECT 
  FirstName,
  LastName,
  UserID,
  PasswordSalt,
  Password,
  PasswordChanged,
  UserName
  FROM MyDatabase2.dbo.Users

After the data is transfered, you need to decide if need to drop or delete the table in anther database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.