I have 2 tables "Order and "OrderDetails" with the similar structure below.Order.id is foreign key for OrderDetails
The same structure exists in 3 different regions Databases.
Now
we have a requirement to merge 3 regions tables to one . ie (from 2*3=6 tables to 2 tables)
Order.id can be duplicated in all regions
eg:
region1DB.Order.id{1,2,3,4,5} , region2DB.order.id{1,2,3,4,5}
Table structure
I need a final output of 2 tables with data from 3 databases, with all the relationships.
eg: if first region table has 10 orderid's , while merging the data from second region order.id will be starting/updated with 11,12,13(wherein the actual number was 1,2,3,4 in second region) and in Orderdeatils it should be reflected as well.
Could you help me with a query for this merge? or any other latest technique available like sql merge? If yes please give me a sample.
I am new to sql and I couldn't see a perfect example merging relationship tables.
Here is my query to join the first table
select tab1.ID, tab1.OrderName
from [DB1].[dbo].[Order] tab1
union all
select tab2.id + (select max(id) from [DB1].[dbo].[Order]) ,tab2.OrderName
from [DB2].[dbo].[Order] tab2
-> query for merging second table(OrderDetails)
declare @t varchar(8000)
set @t=(select max(id) from [DB1].[dbo].[Order])
from [DB1].[dbo].[OrderDetails] tab1
select tab1.OrderID, tab1.productCode from [DB1].[dbo].[OrderDetails] tab1
union all
select
case when
tab2.OrderID> (@t)
then tab2.OrderID + (@t)
when
tab2.OrderID<=(@t)
then
tab2.OrderID
END
,tab2.productCode
from [DB2].[dbo].[OrderDetails] tab2
order by OrderID
Thanks
SNA