I'd like a query that at its simplest will join 2 tables together with no other express relationship other than each row in what I will call the "pool" table will match to precisely 1 row in my other table. I don't care which row, I just want every row in my primary table to get a single row from the "pool" table and know that each row from the pool will only be used once.
I was thinking something like ROW_NUMBER() OVER() could be used to match on an arbitrary row number which would be fine but I think requires at least 2 inner rowset providers; I thought there would be something simpler.
To put the problem a bit more succinctly, you have a table of ids and you want to join it to a table of records to assign the ids. You can only use each id once. What query structure or join can you use to return a rowset of all of the records each assigned to an ID. It does not matter what record gets what ID, only that each ID can only be used once.
Background
For those of you interested in the background, what I have is a logical entity which is made up of a header row in a header table which is generic to lots of objects which gives us its ID and then a record in an entity specific table. I'm using a query like the following to pregenerate a bunch of IDs in the header table:
declare @idsTable TABLE(ID INT);
INSERT INTO Header (HeaderType)
OUTPUT INSERTED.id INTO @idsTable
SELECT 4 as HeaderType
FROM Company c WHERE c.CompanyType = 12;
At this point I have a bunch of header rows and the ids (IDENTITY) that were assigned to the rows. Now, I want to use a similar INSERT into the object specific table but I need to match an ID from the @idsTable to only 1 row in my select query (and hence my insert query). Something like:
INSERT INTO Specific (HeaderiD, Value1, ...)
SELECT *
FROM @idsTable
JOIN RecordsToWrite r2r ON ???