Querying a database for values in each row
In database terms, you want to LEFT JOIN all the rows in your Excel
range (table on the left) to those in the your external database
(table on the right) or a row of nulls if there is no match. The join
would look something like this:
SELECT T1.Col1, T1.Col2, T1.Col3, T2.LookupCol As NewCol
FROM MyExcelData T1 LEFT JOIN
MyExternalData T2 ON T1.Col1=T2.Col1
AND T1.Col2=T2.Col2
AND T1.Col3=T2.Col3
I've never known a RDBMS allow you to JOIN tables from different data
sources on the fly i.e. without previously linking servers (SQL
Server) or using linked tables (MS Access). You need both tables in
the same 'place'.
Also, in database terms, you are talking about appending a new column
to an existing table, which is not something normally done on the fly
with a query!
So it makes me wonder: is this a one off or something you will be
doing regularly in an Excel application?
If it's a one off, you are probably best off importing or linking the
Excel data within the RDBMS, create a new table locally using a LEFT
JOIN, then updating the original Excel source. If it was a regular
thing, it would be difficult to do all this in code from Excel if you
wanted to repeat regularly.
Post back with some more details e.g. the external database (MS
Access, SQL Server, another Excel workbook etc), what your data looks
like (Excel and external), how often you plan to do this, whether to
run from code, etc.
--
"Stephen Goldfinger" wrote in message ...
Howdy.
I have a worksheet where each row has several columns with
values. I want to take these column values and look up a
value from an external database. I also don't want to
create a query for each row (since there are many
thousands of rows). I know how to get a query to return a
value for a single row, but is there a way to get a query
to return a value for each row and place the result in a
column at the end of the row whose values are being
matched.
Thanks in advance for your help.
Stephen
|