I have two temp tables (#TempA, #TempB). each temp table has few records with the formorder 1, 2 and 3. Each Order has multiple records/rows.
For Example:
TempA
===========================
C1 C2 C3 FormOrder
===========================
abc xyz lmn 1
------------------------
anc ppl nmp 2
----------------------
acc bbl mnp 3
-----------------------
Similarly, TempB has few records in the same format.
while combining these two Temp tables with using union all, I am getting the data. but the Data is not coming in order from the #TempA table.
I have tried adding 'Order by' at the end of the select query, but I am getting results in different way.
Without using Order By:
select * from #TempA
Union All
select * from #TempB
Results:
===========================
C1 C2 C3 FormOrder
===========================
abc xyz lmn 1
-----------------------
acc bbl mnp 3
----------------------
anc ppl nmp 2
----------------------
xyz ccc nnn 1
------------------------
xyn klm uul 2
------------------------
cpp klm rnp 3
------------------------
Here the order from the #TempA is missing. So when I tried with Order By
select * from #TempA
Union All
select * from #TempB
Order by FormOrder
Results:
==========================
C1 C2 C3 FormOrder
============================
abc xyz lmn 1
---------------------------
xyz ccc nnn 1
----------------------------
anc ppl nmp 2
----------------------------
xyn klm uul 2
--------------------------
acc bbl mnp 3
-----------------------------
cpp klm rnp 3
-------------------------
I am getting the above Order, but I am expecting the Order as 1,2,3 and them from TempA 1,2, 3 from TempB.
============================
C1 C2 C3 FormOrder
============================
abc xyz lmn 1
--------------------------
anc ppl nmp 2
-------------------------
acc bbl mnp 3
-----------------------
xyz ccc nnn 1
-------------------------
xyn klm uul 2
---------------------------
cpp klm rnp 3
------------------------
How can I achieve this? Any help much appreciated.