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.
| Author |
Topic |
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2005-09-05 : 12:24:47
|
| Hi AllI am currently Migrating alot of Access reports to SQL..I have a linked server in SQL created to an IBM Database B2So I am converting the SQL created with access into Distributed queries in SQLAll has gone well until I have a join of 2 tables that I need to do from WITHIN a Distributed Query.Eg: The following works perfectlyLinked server is Movex_MVXAMODSFCPIMEIG is a table within the linked serverSELECT Z2CONO AS 'Company', Z2WZIS AS 'TAC', Z2TX40 AS 'Desc' FROM OPENQUERY(MOVEX_MVXAMODSFC, 'Select * from MVXAMODSFC.PIMEIG ')WHERE Z2CONO=200The statemt above Returns ok and I getCompany Tac DescData Data Data Data Data Data But if I try this below....Linked Server Movex_MVXAMODSFCMITMAS is a table within the linked serverMITBAL is a Table within the linked serverI am trying to do a join on these 2 tables where The field MITMAS.MMCONO = MITBAL.MBCONO and MITMAS.MMITNO = MITBAL.MBITNOI'm not sure how to go about this on a distributed query asyou only get to do 1 SELECT Stament From an OPENQUERY as above.Does Anyone know how to do this ??I have tried the folowing but I know the overall approach is incorrect.SELECT MMSTAT AS Status, MMITNO AS [Item Number], Sum(MITBAL.MBSTQT) AS [In Stock], Sum(MITBAL.MBAVAL) AS [Allocable on hand Balance]FROM OPENQUERY(MOVEX_MVXAMODSFC MVXADTA_MITMAS LEFT JOIN MVXADTA_MITBAL ON (MVXADTA_MITMAS.MMCONO = MVXADTA_MITBAL.MBCONO) AND (MVXADTA_MITMAS.MMITNO = MVXADTA_MITBAL.MBITNO)Thanks im Advance,Ray. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-09-05 : 15:12:55
|
| OPENQUERY(MOVEX_MVXAMODSFC, 'select * from MVXADTA_MITMAS LEFT JOIN MVXADTA_MITBAL ON (MVXADTA_MITMAS.MMCONO = MVXADTA_MITBAL.MBCONO) AND (MVXADTA_MITMAS.MMITNO = MVXADTA_MITBAL.MBITNO')Openquery just takes the linked server name then sends the string following to be executed on it.don't know what syntax the server takes but it is better to transfer as little as possibleOPENQUERY(MOVEX_MVXAMODSFC, 'select MMSTAT AS Status, MMITNO AS [Item Number], Sum(MITBAL.MBSTQT) AS [In Stock], Sum(MITBAL.MBAVAL) AS [Allocable on hand Balance] from MVXADTA_MITMAS LEFT JOIN MVXADTA_MITBAL ON (MVXADTA_MITMAS.MMCONO = MVXADTA_MITBAL.MBCONO) AND (MVXADTA_MITMAS.MMITNO = MVXADTA_MITBAL.MBITNO grouop by MMSTAT, MMITNO ')In your first query Z2CONO=200 should be inside the remote query if possible - as you have it it would transfer the data to your server then do the filter.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2005-09-06 : 10:47:23
|
| Thx for the reply...I use your advice and got the following to work..Th elast where had to be outside the openquery else I got and error.thx for your helpSELECT *FROM OPENQUERY(MOVEX_MVXADTA, 'SELECT MITMAS.MMSTAT AS Status, MITMAS.MMITNO AS Item_Number, Sum(MITBAL.MBSTQT) AS In_Stock, Sum(MITBAL.MBAVAL) AS Allocable_on_hand_BalanceFROM MITMAS LEFT JOIN MITBAL ON (MITMAS.MMCONO = MITBAL.MBCONO) AND (MITMAS.MMITNO = MITBAL.MBITNO)WHERE MITMAS.MMCONO=200 GROUP BY MITMAS.MMSTAT, MITMAS.MMITNO ')WHERE STATUS = 20GO |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-09-06 : 17:24:01
|
| trySELECT *FROM OPENQUERY(MOVEX_MVXADTA, 'SELECT MITMAS.MMSTAT AS Status, MITMAS.MMITNO AS Item_Number, Sum(MITBAL.MBSTQT) AS In_Stock, Sum(MITBAL.MBAVAL) AS Allocable_on_hand_BalanceFROM MITMAS LEFT JOIN MITBAL ON (MITMAS.MMCONO = MITBAL.MBCONO) AND (MITMAS.MMITNO = MITBAL.MBITNO)WHERE MITMAS.MMCONO=200GROUP BY MITMAS.MMSTAT, MITMAS.MMITNO ') a WHERE STATUS = 20but it is better to include the where clause inside the openquery statement.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2005-09-07 : 10:39:06
|
| Hiya,And thx for your help so far..Th elast statement I got away with having the where outside the openquery but as you said it is quicker if I can get the where statement within the string that get sent to the linked server..Can you help with this one...Linked server to Pervasive DB ( BTRIEVE)The following works from SQL but takes 40 SecondsSELECT *From Openquery(sbase_live_link, 'select * FROM STDCHRGE ') WHERE Unit_Type= 'HANDSET'The following works when run directly on the Pervasive server ..takes 5 secondsSELECT *From STDCHRGE WHERE (Unit_Type= 'HANDSET')But if I try and put the where inside the openquery to speed things up ie:SELECT *From Openquery(sbase_live_link, 'select * FROM STDCHRGE WHERE Unit_Type= 'HANDSET' ')I get the errorLine 2: Incorrect syntax near 'HANDSET'.I also tried No ' ' Quotations and got the belowSELECT *From Openquery(sbase_live_link, 'select * FROM STDCHRGE WHERE Unit_Type= HANDSET ') [ODBC Engine Interface]Error in expression: HANDSET ][OLE/DB provider returned message: [Pervasive][ODBC Client Interface][LNA][Pervasive][ODBC Engine Interface]Invalid column name: 'HANDSET'.]Any ideas ?Ray |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-09-07 : 17:07:23
|
| SELECT *From Openquery(sbase_live_link, 'select * FROM STDCHRGE WHERE Unit_Type= ''HANDSET''')==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rwaldron
Posting Yak Master
131 Posts |
Posted - 2005-09-08 : 04:50:45
|
| Yep,Thx for that.''Handset'' does the trick.So It looks like the column name needs to be wrapped in another set of single quotes when sending as a distributed query.'' as apposed to "Thx again,Ray. |
 |
|
|
|
|
|
|
|