I've finally did it with the help of powershell script (and DBATools module) and the use of SQLCMD scripts.
I tried to do the database merge at first, but it didn't work on the schema I was working on. The trick was the each database must be unique for itself, so I have to transfer each and every table/view/procedure/function to a new schema and all of the object reference to relate to this new schema.
There were no room for table merging nor data merge. each and every object must stay unique with its own data to transfer it to one Azure SQL Database.
So I had to set up some rules.
One schema. the database schema shouldn't be changed. All the databases are with the same schema so I can use the same DDL script to create each and every object in the new database.
I used SQLCMD and used a parameter( $(NewSchema)
) in the script that I can create a new schema for each set of object and can control every object to relate to others in the same schema.
For the data transfer I wanted to use INSERT INTO [LinkedServer]...[Tablename]
but, it didn't work too well because of Identity column in the tables.
I could not manipulate it using "Set Identity On" because you have to be on the same session on the server in order to do this. Couldn't do it using Linked Server.
Another issue I faced was a bunch of constraints on the database and foreign keys.
So I had to solve this issue in a different manner.
PowerShell module DBATools come to the rescue.
Running on every table in the database exporting out only the data as a script with the help of Export-DbaScript, storing it into a variable, replacing the schema name and running it on the remote server.
The script already created with SET IDENTITY_INSERT On
for each table.
For the constraints, I used an SQLCMD script again to ALTER TABLE NOCHECK CONSTRAINT
all constraints for the current schema and then used it to turn it back on (after all the data copied).
I rapped everything in a PowerShell script that gets parameters for all of the connections and databases and just do the job.
I'm pretty sure there are better ways of achieving this goal, but this one worked for me.
I'll love to here from you,
what do you think of the solution I came out with?
I'll be happy to share scripts, if anyone asks me too.
Nathan