Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Distributed Query Between SQL and DB2

Author  Topic 

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 06:21:32
Hi all,

Usually I write distributed queries using linked servers between SQL and DB2 like this.
Select *
FROM OPENQUERY (DB2_Linked server,
' Select * From MVXADTASWC.CCURRA
')

This is ok when I just need to query data on the linked server and bring it back to SQL....

But what if I already have a table in SQL that I wish to Join to the data from the distributed query....?
How do I do this is SQL...I think I need Fully qualified names of DB's.

In Access the query is below where tbcurrentExhangeRate is an existing SQL DB and Mvxadta_oohead is a table in DB2.
How do I do the same in SQL using a distubuted query..

SELECT MVXADTA_OOHEAD.OAORNO, tbCurrentExchangeRate.LastOfCUARAT INTO tbExpectedOrderExchangeRate
FROM MVXADTA_OOHEAD INNER JOIN tbCurrentExchangeRate ON (MVXADTA_OOHEAD.OACUCD = tbCurrentExchangeRate.CUCUCD) AND (MVXADTA_OOHEAD.OACONO = tbCurrentExchangeRate.CUCONO)
WHERE (((MVXADTA_OOHEAD.OACONO)=400));

Thx
Ray

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 06:29:41
Alias the OPENQUERY query with a name.

Try this
SELECT		MVXADTA_OOHEAD.OAORNO,
tbCurrentExchangeRate.LastOfCUARAT
INTO tbExpectedOrderExchangeRate
FROM OPENQUERY(DB2_Linked server, 'SELECT * FROM MVXADTA_OOHEAD') MVXADTA_OOHEAD
INNER JOIN tbCurrentExchangeRate ON MVXADTA_OOHEAD.OACUCD = tbCurrentExchangeRate.CUCUCD AND MVXADTA_OOHEAD.OACONO = tbCurrentExchangeRate.CUCONO
WHERE MVXADTA_OOHEAD.OACONO = 400


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 07:08:17
Thank you so much Peter.
The Alias Did the trick..Code below

Could you quickly explain the purpose of using an alias?
I think it just avoids me having to create 2 steps ie:
1 to create a new table and the 2 to query the first Table

With an alias its like a temp table ?

SELECT OAORNO,tbl3_SWC_Current_Exchange_Rate.CUARAT
INTO tbl3_SWC_Expected_Order_Exchange_Rate
FROM OPENQUERY(MOVEX_EXTRANET, 'SELECT * FROM MVXADTASWC.OOHEAD WHERE OACONO = 400') MVXADTASWCOOHEAD_ALIAS
INNER JOIN tbl3_SWC_Current_Exchange_Rate ON MVXADTASWCOOHEAD_ALIAS.OACUCD = tbl3_SWC_Current_Exchange_Rate.CUCUCD AND MVXADTASWCOOHEAD_ALIAS.OACONO = tbl3_SWC_Current_Exchange_Rate.CUCONO

Ray..
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 07:13:47
For easier query reading mostly.
An alias is just another name for the resultset, or normally a table.

When using a derived table, you must alias the resultset from that derived table.
SELECT     *
FROM (
SELECT Ooops,
ThisInformation
FROM MyTable
WHERE SomeColumn < 4
) d
LEFT JOIN (
SELECT Yikes,
Reason
FROM OtherTable
WHERE Reference > 9
) e ON e.Yikes = d.Ooops
When selecting data from only one table, there is no need to alias the table. But when selecting from two tables with a JOIN or IN of some kind, it is a good practice to alias the tables and prefix the column names for easier reading and maintenance of query. This is actually a requirement if joining two tables and the two tables each has same column name.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 07:20:40
Compare this to the query you posted previous to this answer.
SELECT		w.OAORNO,
q.CUARAT
INTO tbl3_SWC_Expected_Order_Exchange_Rate
FROM OPENQUERY(MOVEX_EXTRANET, 'SELECT * FROM MVXADTASWC.OOHEAD WHERE OACONO = 400') AS w
INNER JOIN tbl3_SWC_Current_Exchange_Rate AS q ON q.CUCUCD = w.OACUCD AND q.CUCONO = w.OACONO
AS is not needed but often used for clarification, but this does the same thing
SELECT		w.OAORNO,
q.CUARAT
INTO tbl3_SWC_Expected_Order_Exchange_Rate
FROM OPENQUERY(MOVEX_EXTRANET, 'SELECT * FROM MVXADTASWC.OOHEAD WHERE OACONO = 400') w
INNER JOIN tbl3_SWC_Current_Exchange_Rate q ON q.CUCUCD = w.OACUCD AND q.CUCONO = w.OACONO


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 07:26:21
Thx Guys,
Much Clearer now...

Ray ;)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 07:30:31
quote:
Originally posted by rwaldron

Thx Guys,
Much Clearer now...
Thank you for the feedback.
Even if I am not a schizofrenic yet...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 08:15:31
I ve just looked at my next access Code and it has 2 DB2 and 1 SQL Server DB.
So I need 3 aliases ?
Access SQL is
SELECT MVXADTASWC_OOCHRG.OECONO, MVXADTASWC_OOCHRG.OEORNO, MVXADTASWC_OOCHRG.OECRAM AS CHARGE, [OECRAM]/[LastOfCUARAT] AS CHARGEEUR, MVXADTASWC_OOCHRG.OECRTY, IIf([OECRTY]=0,[CHARGEEUR],0) AS EXTCHARGE, IIf([OECRTY]=1,[CHARGEEUR],0) AS INTCHARGE, MVXADTASWC_OOCHRG.OECHST INTO tbexpectedmargincharges
FROM MVXADTASWC_OOCHRG INNER JOIN (MVXADTA_OOHEAD INNER JOIN tbCurrentExchangeRate ON (MVXADTA_OOHEAD.OACUCD = tbCurrentExchangeRate.CUCUCD) AND (MVXADTA_OOHEAD.OACONO = tbCurrentExchangeRate.CUCONO)) ON (MVXADTASWC_OOCHRG.OEORNO = MVXADTA_OOHEAD.OAORNO) AND (MVXADTASWC_OOCHRG.OEDIVI = MVXADTA_OOHEAD.OADIVI) AND (MVXADTASWC_OOCHRG.OECONO = MVXADTA_OOHEAD.OACONO)
WHERE (((MVXADTASWC_OOCHRG.OECONO)=400) AND ((MVXADTASWC_OOCHRG.OECHST)<"77"));
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 08:26:51
Yes.
SELECT		t1.OECONO,
t1.OEORNO,
t1.OECRAM AS Charge,
t1.OECRAM / t3.LastOfCUARAT ChargeEUR,
t1.OECRTY,
CASE WHEN t1.OECRTY = 0 THEN t1.OECRAM / t3.LastOfCUARAT ELSE 0 END ExtCharge,
CASE WHEN t1.OECRTY = 1 THEN t1.OECRAM / t3.LastOfCUARAT ELSE 0 END IntCharge,
t1.OECHST
INTO tbExpectedMarginCharges
FROM MVXADTASWC_OOCHRG t1
INNER JOIN MVXADTA_OOHEAD t2 ON t2.OAORNO = t1.OEORNO AND t2.OADIVI = t1.OEDIVI AND t2.OACONO = t1.OECONO
INNER JOIN tbCurrentExchangeRate t3 ON t3.CUCUCD = t2.OACUCD AND t3.CUCONO = t2.OACONO
WHERE t1.OECONO = 400
AND t1.OECHST < '77'
Just ensure that t3.LastOfCUARAT is not zero.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 09:35:39
Thx for your help Guys but in this last Query I need to use a distributed query.
I am close with the the following but I get the error Invalid object name 'MVXADTASWC.OOHEAD'
This table ( MVXADTASWC also needs to be brought back from the distributed query as t2 ?

Any ideas?
SELECT t1.OECONO,
t1.OEORNO,
t1.OECRAM AS Charge,
t1.OECRAM / t3.CUARAT ChargeEUR,
t1.OECRTY,
CASE WHEN t1.OECRTY = 0 THEN t1.OECRAM / t3.CUARAT ELSE 0 END ExtCharge,
CASE WHEN t1.OECRTY = 1 THEN t1.OECRAM / t3.CUARAT ELSE 0 END IntCharge,
t1.OECHST

FROM OPENQUERY (MOVEX_EXTRANET, 'SELECT * FROM MVXADTASWC.OOCHRG
WHERE OECONO=400 AND OECHST < ''77'' ')as t1
INNER JOIN MVXADTASWC.OOHEAD t2 ON t2.OAORNO = t1.OEORNO AND t2.OADIVI = t1.OEDIVI AND t2.OACONO = t1.OECONO
INNER JOIN tbl3_SWC_Current_Exchange_Rate AS t3 ON t3.CUCUCD = t2.OACUCD AND t3.CUCONO = t2.OACONO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-12 : 09:41:35
[code]SELECT t1.OECONO,
t1.OEORNO,
t1.OECRAM Charge,
t1.OECRAM / t3.CUARAT ChargeEUR,
t1.OECRTY,
CASE WHEN t1.OECRTY = 0 THEN t1.OECRAM / t3.CUARAT ELSE 0 END ExtCharge,
CASE WHEN t1.OECRTY = 1 THEN t1.OECRAM / t3.CUARAT ELSE 0 END IntCharge,
t1.OECHST
FROM OPENQUERY(MOVEX_EXTRANET, 'SELECT OEORNO, OEDIVI, OECONO, OECRAM, OECRTY, OECHST FROM MVXADTASWC.OOCHRG WHERE OECONO = 400 AND OECHST < ''77''') t1
INNER JOIN OPENQUERY(MOVEX_EXTRANET, 'SELECT OAORNO, OADIVI, OACONO, OACUCD FROM MVXADTASWC.OOHEAD') t2 ON t2.OAORNO = t1.OEORNO AND t2.OADIVI = t1.OEDIVI AND t2.OACONO = t1.OECONO
INNER JOIN tbl3_SWC_Current_Exchange_Rate AS t3 ON t3.CUCUCD = t2.OACUCD AND t3.CUCONO = t2.OACONO[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-12 : 09:48:12
Thank you Peter....Genius...

I hope you don't mind me annoying you so much..

Thx for your help.

Ray ..
Ireland
Go to Top of Page

rwaldron
Posting Yak Master

131 Posts

Posted - 2006-09-15 : 06:56:17
Hiya Peso,
Could you please help me with the following access code to SQL?
I'm stuck on the calcualtions?

Thx,
Ray..

SELECT MVXADTA_OOLINE.OBCONO, MVXADTA_OOLINE.OBORNO, MVXADTA_OOLINE.OBORQT, MVXADTA_OOLINE.OBSAPR, MVXADTA_OOLINE.OBUCOS, tbExpectedOrderExchangeRate.LastOfCUARAT AS ExchangeRate, [obsapr]*([obdlqt]+[obalqt]+[obplqt]+[obrnqt]) AS SalesValueForCurr, [SalesValueForCurr]/[ExchangeRate] AS SalesValueEUR, [OBDIA1]*([obrnqt]+[obalqt]+[obplqt]+[obdlqt]) AS EngPriceLst, ([engpricelst]/[ExchangeRate]) AS EngPriceLstEur, ([OBDIA2]+[OBDIA3]+[OBDIA4]+[OBDIA5]+[OBDIA6])*([obrnqt]+[obplqt]+[obdlqt]+[obalqt]) AS Discounts, [Discounts]/[ExchangeRate] AS DiscountsEur, MVXADTASWC_OOLICH.O7CRFA AS WARRPERC, IIf([WARRPERC] Is Null,0,[SalesvalueEur]*[O7CRFA]) AS WARRANTY, [SalesValueEur]-([EngPricelstEur]+[Discounts]+[WARRANTY]) AS NetSalesStock, [obucos]*([obrnqt]+[obalqt]+[obplqt]+[obdlqt]) AS EURCOSTS, MVXADTA_OOLINE.OBRGDT, MVXADTA_OOLINE.OBORST INTO tbexpectedmarginmaterial
FROM (MVXADTA_OOLINE LEFT JOIN MVXADTASWC_OOLICH ON (MVXADTA_OOLINE.OBPONR = MVXADTASWC_OOLICH.O7PONR) AND (MVXADTA_OOLINE.OBORNO = MVXADTASWC_OOLICH.O7ORNO) AND (MVXADTA_OOLINE.OBCONO = MVXADTASWC_OOLICH.O7CONO)) INNER JOIN tbExpectedOrderExchangeRate ON MVXADTA_OOLINE.OBORNO = tbExpectedOrderExchangeRate.OAORNO
WHERE (((MVXADTASWC_OOLINE.OBCONO)=400) AND ((MVXADTA_OOLINE.OBORST)<"77") AND ((MVXADTASWC_OOLINE.OBITNO)<>"QUOTEITEM"));
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-15 : 07:20:00
Not the prettiest solution, and could be easier to maintain using a derived table instead...
				SELECT		MVXADTA_OOLINE.OBCONO,
MVXADTA_OOLINE.OBORNO,
MVXADTA_OOLINE.OBORQT,
MVXADTA_OOLINE.OBSAPR,
MVXADTA_OOLINE.OBUCOS,
tbExpectedOrderExchangeRate.LastOfCUARAT AS ExchangeRate,
obsapr * (obdlqt + obalqt + obplqt + obrnqt) AS SalesValueForCurr,
obsapr * (obdlqt + obalqt + obplqt + obrnqt) / tbExpectedOrderExchangeRate.LastOfCUARAT AS SalesValueEUR,
OBDIA1 * (obrnqt + obalqt + obplqt + obdlqt) AS EngPriceLst,
OBDIA1 * (obrnqt + obalqt + obplqt + obdlqt) / tbExpectedOrderExchangeRate.LastOfCUARAT AS EngPriceLstEur,
(OBDIA2 + OBDIA3 + OBDIA4 + OBDIA5 + OBDIA6) * (obrnqt + obplqt + obdlqt + obalqt) AS Discounts,
(OBDIA2 + OBDIA3 + OBDIA4 + OBDIA5 + OBDIA6) * (obrnqt + obplqt + obdlqt + obalqt) / tbExpectedOrderExchangeRate.LastOfCUARAT AS DiscountsEur,
MVXADTASWC_OOLICH.O7CRFA AS WARRPERC,
CASE WHEN MVXADTASWC_OOLICH.O7CRFA Is Null THEN 0 ELSE obsapr * (obdlqt + obalqt + obplqt + obrnqt) / tbExpectedOrderExchangeRate.LastOfCUARAT * O7CRFA END AS WARRANTY,
obsapr * (obdlqt + obalqt + obplqt + obrnqt) / tbExpectedOrderExchangeRate.LastOfCUARAT - (OBDIA1 * (obrnqt + obalqt + obplqt + obdlqt) / tbExpectedOrderExchangeRate.LastOfCUARAT + (OBDIA2 + OBDIA3 + OBDIA4 + OBDIA5 + OBDIA6) * (obrnqt + obplqt + obdlqt + obalqt) + CASE WHEN MVXADTASWC_OOLICH.O7CRFA Is Null THEN 0 ELSE obsapr * (obdlqt + obalqt + obplqt + obrnqt) / tbExpectedOrderExchangeRate.LastOfCUARAT * O7CRFA END) AS NetSalesStock,
obucos * (obrnqt + obalqt + obplqt + obdlqt) AS EURCOSTS,
MVXADTA_OOLINE.OBRGDT,
MVXADTA_OOLINE.OBORST
INTO tbexpectedmarginmaterial
FROM MVXADTA_OOLINE
LEFT JOIN MVXADTASWC_OOLICH ON MVXADTA_OOLINE.OBPONR = MVXADTASWC_OOLICH.O7PONR AND MVXADTA_OOLINE.OBORNO = MVXADTASWC_OOLICH.O7ORNO AND MVXADTA_OOLINE.OBCONO = MVXADTASWC_OOLICH.O7CONO
INNER JOIN tbExpectedOrderExchangeRate ON MVXADTA_OOLINE.OBORNO = tbExpectedOrderExchangeRate.OAORNO
WHERE MVXADTASWC_OOLINE.OBCONO = 400
AND MVXADTA_OOLINE.OBORST < '77'
AND MVXADTASWC_OOLINE.OBITNO <> 'QUOTEITEM'


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -