Question

I'm selecting 1 field from 1 table and storing it into a temp table.

Sometimes that table ends up with 0 rows.

I want to add that field onto another table that has 20+ fields

Regular union won't work for me because of the field # mismatch. Outer wont work for me because there is nothing to compare. NVL doesn't work on the first temp table.

Anyone know how to do it?

UPDATED:

I failed to mention.... When the table that retrieves 1 field finds a match in other cases, this code that I'm using now works....

SELECT DISTINCT reqhead_rec.resp_name<br>
FROM reqhead_rec, biglist<br>
WHERE reqhead_rec.req_no = biglist.req_no
 AND reqhead_rec.frm = biglist.req_frm<br>
INTO TEMP grabname with no log;

SELECT biglist.*, grabname.resp_name<br>
FROM biglist, grabname<br>
ORDER BY prnt_item, account_amt<br>
INTO TEMP xxx with no log;
Was it helpful?

Solution

What field would it match with? BTW, here's how to line them up:

SELECT NULL, NULL, NULL, NULL, MySingleField, NULL, NULL, NULL... FROM #temp
UNION ALL
SELECT Col1, Col2, Col3, Col4, Col5, Col6,... FROM OtherTable

UPDATE:

OK, after reading your update... I don't think you want a UNION at all, but rather, and incredibly simple SUBSELECT

SELECT
    *,
    (SELECT TOP 1 Name FROM Blah WHERE Blah.SomeID = MyTable.SomeID) AS ExtraCol
FROM
    MyTable

OTHER TIPS

It sounds like you do want a join, not a union.

You don't need to compare anything to do a join. You end up with a cross product if you specify no join condition:

SELECT t20.*, t1.*
FROM table_with_20_columns AS t20
  LEFT OUTER JOIN temp_table_with_1_column AS t1 ON (1=1);

When there are zero rows in the temp table, it'll be reported as NULL in the result of the above query.

However, if there are multiple rows in the temp table, you'll get the cross product with the first table. I can't tell from your question what you want.

edit: The join condition expressed in the ON or USING clause should be optional according to the SQL standard, but at least as I test it in MySQL 5.0, it's a syntax error to omit that clause. But you can use ON (1=1).

edit: Answering your question in the comment:

SELECT COALESCE(reqhead_rec.resp_name, dflt.resp_name) AS resp_name
FROM (SELECT 'default name' AS resp_name) dflt
  LEFT OUTER JOIN reqhead_rec ON (1=1)
WHERE reqhead_rec.req_no = biglist.req_no AND reqhead_rec.frm = biglist.req_frm 
INTO TEMP grabname WITH NO LOG;

Actually, you may be able to skip the temp table altogether. Just LEFT JOIN your main table to reahead_rec. Put those conditions into the ON clause of the join, not in the WHERE clause. Then use COALESCE() in the select-list of that query to give a default name when one is not found in the other table.

SELECT b.*, COALESCE(r.resp_name, 'default name') AS resp_name
FROM biglist AS b
  LEFT OUTER JOIN reqhead_rec AS r
    ON (b.req_no = r.req_no AND r.frm = b.req_frm)
INTO TEMP xxx WITH NO LOG;

Try selecting nvl(NULL,NULL) for missing values

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top