I am using SQL Server 2008. I have 2 table variables like
FirstName ========== Little John Baby LastName ========== Timmy Doe Jessica
And I want the result table be:
First Last ===================== Little Timmy John Doe Baby Jessica
Note that there is no PK can join the 2 tables. I am trying to use a cursor but not sure how to start.
---- Updated -----
I know it's a rare case, but I am writing a script to clean up legacy data. The only way we know "Little" goes with "Timmy" is that they are both the first record of the table. Would it help if we had PK for the tables but there is no relation?
ID FirstName ========== 1 Little 2 John 3 Baby ---------- ID LastName ========== 4 Timmy 5 Doe 6 Jessica ----------
I am not familiar with TSQL so I thought I can loop through the 2 tables like looping through Arrays in memory.
preguntado el 22 de mayo de 12 a las 18:05
You could try something like this, to match up based on row numbers:
SELECT FirstName AS First, LastName AS Last FROM ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS RowNum, FirstName FROM FirstName ) t1 INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS RowNum, LastName FROM LastName ) t2 ON t1.RowNum = t2.RowNum
But don't take this as a signal that you don't need keys.
You can't reliably join these two table variables without identities. Assuming they are being populated in an ordinal manner in the first place, each table could be created with identities as follows:
DECLARE @first TABLE(ID INT IDENTITY(1,1), NameFirst VARCHAR(30)); DECLARE @last TABLE(ID INT IDENTITY(1,1), NameLast VARCHAR(30)); -- Note that we don't need to list column names here INSERT INTO @first VALUES('Little'); INSERT INTO @first VALUES('John'); INSERT INTO @first VALUES('Baby'); INSERT INTO @last VALUES('Timmy'); INSERT INTO @last VALUES('Doe'); INSERT INTO @last VALUES('Jessica'); SELECT n1.NameFirst , n2.NameLast FROM @first n1 INNER JOIN @last n2 ON n1.ID=n2.ID;
NameFirst NameLast ------------------------------ ------------------------------ Little Timmy John Doe Baby Jessica
There is no such thing as a "first record". Reading order is totally undefined. This holds true in practice as well! You sí see random row order in practice.
Usted necesitas to define a sorting order. After doing that you can query like this:
SELECT FirstName AS First, LastName AS Last FROM ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS r, FirstName FROM FirstName ) t1 INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY ID) AS r, LastName FROM LastName ) t2 ON t1.r = t2.r
Notice the placeholder "
SORT ORDER HERE". Need to fill that in. Example: "ID" or "FirstName".
Edit: Having seen your edit I added sort order on ID. This will work now.
Here is a hack, that might work in your case:
select t1.id, t1.name, t2.name from (select name, row_number() over (partition by null order by (select NULL)) as id from t1 ) t1 join (select name, row_number() over (partition by null order by (select NULL)) as id from t2 ) t2 on t1.id = t2.id
In my experience, the row_number() with the subquery for the order by means that no sort is actually done, but the rows are brought in sequentially. This is more likely to work if you are using a single threaded instance.
And, I don't guarantee that it will work! Definitely check the results! If this doesn't work, you might be able to get the same effect by using temporary tables.
Is this data coming from outside the database world? If you have to perform this process more than once, then you should handle the problem either externally or on input into the database.
Building on solution @JohnDewey presented, you can sort the records into the table variables. In practice, it will create a relation between the values based on the order of the records:
DECLARE @first TABLE(sequence INT IDENTITY(1,1), FirstName VARCHAR(30)); DECLARE @last TABLE(sequence INT IDENTITY(1,1), LastName VARCHAR(30)); INSERT INTO @first(FirstName) SELECT FirstName FROM TableFisrt ORDER BY id; -- sequence FirstName -- ======== ========= -- 1 Little -- 2 John -- 3 Baby INSERT INTO @last(LastName) SELECT FirstName FROM TableLast ORDER BY id; -- sequence LastName -- ======== ========= -- 1 Timmy -- 2 Doe -- 3 Jessica SELECT frs.FirstName, lst.LastName FROM @first frs INNER JOIN @last lst ON frs.sequence = lst.sequence; -- sequence FirstName -- ======== ========= -- Little Timmy -- John Doe -- Baby Jessica