views:

378

answers:

2

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
FROM reqhead_rec, biglist
WHERE reqhead_rec.req_no = biglist.req_no AND reqhead_rec.frm = biglist.req_frm
INTO TEMP grabname with no log;

SELECT biglist.*, grabname.resp_name
FROM biglist, grabname
ORDER BY prnt_item, account_amt
INTO TEMP xxx with no log;

+3  A: 

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;
Bill Karwin
You need an "ON" clause other wise you'll get an error... you can join "ON 1 = 1".
Timothy Khouri
I guess what i really should be asking is how can i fill a field with a default value if no results come from the query?original query..SELECT reqhead_rec.resp_nameFROM reqhead_recWHERE reqhead_rec.req_no = biglist.req_noAND reqhead_rec.frm = biglist.req_frmINTO TEMP grabname WITH NO LOG;
CheeseConQueso
Im getting a syntax error at the open quote for default name... im not using mysql and sometimes the syntax of subroutines fail in the version im using... im going to keep trying this though, thanks.... i edited the original question with the sql im using now
CheeseConQueso
im getting syntax errors with the coalesce subroutine... im using informix believe it or not... but thanks a lot for your help... much appreciated
CheeseConQueso
Wow, that's pretty weak. Coalesce is a standard SQL function. It could be Informix has a function to do a similar thing, like IFNULL() or NVL() or something.
Bill Karwin
nvl exists, but did not do the trick for some unknown reason.. thats why i posted this question
CheeseConQueso
+2  A: 

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
Timothy Khouri
i need to add the field to the tablethe first table has 73 fields, its needs #74, which is someones name from another table
CheeseConQueso
thanks.. the subselect worked!!! "2 row(s) retrieved." never looked so good...
CheeseConQueso