For example I have two tables (temp tables). One of them contains only IDs, and the other contains real information. They have exactly the same row number.
#Table1
ID
14
15
16
#Table2
CarModel Year
Ford 1996
Ferrari 2005
Toyota 2010
How can I join this two table in one (without any condition, just the result table has 3 columns, no matter in what order)? I need it for inserting the result for real table
Cars
ID CarModel Year
And this table doesn't use identity. There is an own mechanism of getting IDs for inserting. I'm not able to change it.
So as a result I need a table like one of the following (ordering is NOT important):
#Table3
ID CarModel Year
14 Ford 1996
15 Ferrari 2005
16 Toyota 2010
or
#Table3
ID CarModel Year
16 Ford 1996
14 Toyota 2010
15 Ferrari 2005
P.S. I understand how to do this with CTE and row_number(), but I think there is more easier way to do this task. And may be this code will be used in MS SQL Server 2000, so I will be glad to look at other variants of the solutions.
row_number()
would be the way to accomplish this. However, I would have to question WHY you would want a join on these two tables if they dont actually contain related data. – TraditionROW_NUMBER
seems the best way to me. For SQL Server 2000 you would need to simulate it with anidentity
column or triangular join or use cursors. – Detrude