简体   繁体   中英

AS400 Query/Excel ODBC Union Error

I'm having issues getting a query working in Excel for our AS400/DB2 system.

I'm trying to use two different tables with different info, but return an aisle/slot in the same column based upon a specific SKU/Customer ID(Storer). I thought a UNION would work but I get an "SQL0802 - Data conversion or data mapping error" when trying to run the query.

Here's what I have right now:

SELECT ADJTRAN.AJAISL AS AISLE, ADJTRAN.AJSLOT AS SLOT
FROM S216F06V.WDLSDATA.ADJTRAN ADJTRAN
WHERE (AJITEM=8011989 AND AJSTOR=581)
UNION
SELECT ILCATER.ILAISL AS AISLE, ILCATER.ILSLOT AS SLOT
FROM S216F06V.WDLSDATA.ILCATER ILCATER
WHERE (ILITEM=8011989 AND ILSTOR=581)

Any help would be appreciated.

EDIT: AJAISL, AJSLOT, ILAISL and ILSLOT are character fields with a length of 4.

Guessing at the data found in the columns being selected, CAST() or CONVERT() is likely to help you.

Try something like —

SELECT CAST( ADJTRAN.AJAISL AS VARCHAR(128) ) AS AISLE, 
       CAST( ADJTRAN.AJSLOT AS VARCHAR(128) ) AS SLOT
...
UNION
SELECT CAST( ILCATER.ILAISL AS VARCHAR(128) ) AS AISLE, 
       CAST( ILCATER.ILSLOT AS VARCHAR(128) ) AS SLOT` 
...

EDITED TO ADD

Given that all selected columns are the same data type, it's probably one of the tested columns that's at issue.

You're comparing what look like INTEGER s to values in AJITEM , AJSTOR , ILITEM , and ILSTOR -- but what types are these columns, and is all data strictly conformant to that type?

One troubleshooting strategy is to try smaller versions of your query, and gradually add elements, until you find the error trigger. Here, I'd try each part of the UNION separately, at least, if not one element of the WHERE ... AND therein.

Sometime Excel have bugs... Can you try it:

    select * from (
    SELECT ADJTRAN.AJAISL AS AISLE, ADJTRAN.AJSLOT AS SLOT
    FROM S216F06V.WDLSDATA.ADJTRAN ADJTRAN
    WHERE (AJITEM=8011989 AND AJSTOR=581)
    UNION
    SELECT ILCATER.ILAISL AS AISLE, ILCATER.ILSLOT AS SLOT
    FROM S216F06V.WDLSDATA.ILCATER ILCATER
    WHERE (ILITEM=8011989 AND ILSTOR=581)
    ) tmp

AJITEM and ILITEM are VARCHAR(20). I was comparing it to an INT. Fixed and the code below works with zero issues. My fault for not stating this in the original question and overlooking such a simple mistake.

SELECT ADJTRAN.AJAISL AS AISLE, ADJTRAN.AJSLOT AS SLOT
FROM S216F06V.WDLSDATA.ADJTRAN ADJTRAN
WHERE (AJITEM='8011989' AND AJSTOR=581)
UNION
SELECT ILCATER.ILAISL AS AISLE, ILCATER.ILSLOT AS SLOT
FROM S216F06V.WDLSDATA.ILCATER ILCATER
WHERE (ILITEM='8011989' AND ILSTOR=581)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM