Problem with extracting data from AS400 using SQL

-1

I am writing an SQL request to export data from AS400 using different relations. I want to write it like that to reduce the time and increase the performance of my ETL (I have at least 29 million row to use it in my ETL).

SQL:

SELECT  A.x1, A.x2, A.x3, A.x4, A.x5,
        (SELECT B.y1, B.y2, B.y3, C.w1 as w 
        FROM TEST1 AS B inner join TEST2 AS C ON ((C.w2=B.y4) and (C.w3=B.y5)) 
        where (B.y6 = 2)),
        E.q1

FROM TEST3 AS A
LEFT OUTER JOIN TEST1 AS B ON (A.x6 = B.y7)
LEFT OUTER JOIN TEST4 AS E ON ((A.x6 = E.q2) AND (A.x7 = E.q3))

I really need help for this error

I got this exception:

TITRE : Microsoft Visual Studio
Exception de HRESULT : 0xC0202009 Erreur sur Traitement [Source [2]] : Code d'erreur SSIS DTS_E_OLEDBERROR. Une erreur OLE DB s'est produite. Code d'erreur : 0x80004005. Un enregistrement OLE DB est disponible. Source : « IBMDA400 Command » Hresult : 0x80004005 Description : « SQL0412 : Sous-requête non admise, car plus d'une colonne résultat Cause . . . . . : La sous-requête d'un prédicat doit avoir uniquement une colonne résultat correspondante lorsque l'autre opérande du prédicat est une expression simple. En effet, la sous-requête peut extraire zéro, une ou plusieurs valeurs constituant une liste mais apparaissant dans une seule colonne résultat. Que faire . . . : Modifiez le nombre d'éléments renvoyés par la sous-requête pour qu'il n'y ait qu'une seule colonne résultat ou remplacez l'autre opérande du prédicat pour obtenir une liste d'expressions. ».

sql
sql-server
ssis
ibm-midrange
business-intelligence
asked on Stack Overflow May 26, 2020 by houssem eddine ayari • edited May 26, 2020 by Dale K

2 Answers

0

You can use a "table expression" to join the extra data instead of a "scalar subquery".

Scalar subqueries are limited to one row (yours may return multiple rows) and one column (yours is clearly trying to return more than one column).

For example, you can do:

SELECT  A.x1, A.x2, A.x3, A.x4, A.x5,
        f.y1, f.y2, f.y3, f.w,
        E.q1
FROM TEST3 AS A
LEFT OUTER JOIN TEST1 AS B ON (A.x6 = B.y7)
LEFT OUTER JOIN TEST4 AS E ON ((A.x6 = E.q2) AND (A.x7 = E.q3))
left join (
  SELECT B.y1, B.y2, B.y3, C.w1 as w 
  FROM TEST1 AS B -- you should avoid reusing the same alias B, but OK
  inner join TEST2 AS C ON ((C.w2=B.y4) and (C.w3=B.y5)) 
  where (B.y6 = 2)
) f on 1 = 1 -- what's the join condition? I assume you want all rows to match

EDIT TO SPEED UP THE QUERY

You are selecting all the rows from table A; that's bound to be slow. Nevertheless, since your joins are equi-joins they can greatly benefit from indexes; the following indexes could make your query faster:

create index ix1 on TEST1 (y7, x1, x2, x3, x4, x5);

create index ix2 on TEST1 (y6, y1, y2, y3);

create index ix3 on TEST4 (q2, q3, q1);

create index ix4 on TEST2 (w2, w3, w1);
answered on Stack Overflow May 26, 2020 by The Impaler • edited May 27, 2020 by The Impaler
0

The reason for your error is that the subquery is returning more than one column. AlwaysLearning already said that.

But the next issue is going to be that your subquery is returning multiple rows.

Unless

SELECT B.y1, B.y2, B.y3, C.w1 as w 
FROM TEST1 AS B inner join TEST2 AS C ON ((C.w2=B.y4) and (C.w3=B.y5)) 
where (B.y6 = 2)

only returns a single row. Notice that the subquery has no conditions that correlate to the outer select. Using the alias B in the subquery does not correlate it to the outer select because it is redefined in the inner select at FROM TEST1 AS B. This makes the alias local to the subquery, and hides the use of B in the outer select from the subquery. If that is not what you intended, and even if it was, you should always follow a simple rule in SQL, do not re-use alises.

answered on Stack Overflow May 30, 2020 by jmarkmurphy

User contributions licensed under CC BY-SA 3.0