SQL subselect has mulitple rows incoming as opposed to a single row

I am at a loss on how to handle this error. The subselect query below fails with this error:

SQL State: 21000
Vendor Code: -811
Message: [SQL0811] Result of SELECT more than one row. Cause . . . . . :   The result table of a SELECT INTO statement, a subquery, or a subselect of a SET statement contains more than one row.  The error type is 2. If the error type is 1 then a SELECT INTO statement attempted to return more than one row.  If the error type is 2 then a subselect of a basic predicate has produced more than one row.  Only one row is allowed. Recovery  . . . :   Change the selection so that only one result row is returned and then try the request again.  The DECLARE CURSOR, OPEN, and FETCH statements must be used to process more than one result row.  For a subquery the IN, EXISTS, ANY or ALL predicates can be used to process more than one result row.  If one row was expected, there may be data errors, such as duplicate rows, that are causing more than one row to be returned.

I am pretty sure it is just my lack of experience that is causing my frustration. Can someone help me with this problem, please?

Here is the code
update qtemp.items a
set itemallocationid = (select  iak from vd_gpl.ax_siop  
	                          Where (select imprfg from qtemp.items) = trim(productfamily));

Open in new window

I have attached a document showing a sample of the ax_siop file and the qtemp.items file, specifically where the fields are that I am joining on
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gary PattersonVP Technology / Senior Consultant Commented:
"select imprfg from qtemp.items" is going to return multiple rows: all the  rows in qtemp.items.  Looks to me like you need a where clause to correlate.

Look up "correlated subquery" for some guidance.

Post back with what you're trying to do and I'd be happy to help more.
LeogalAuthor Commented:
I am afraid I am still making a mess of it as this still returns multiple rows.. I am not understanding correlated sub queries?

update qtemp.items a
set rtrcode = (select  lmcode from kbm400mfg.fklocmst as  b 
                                where b.lmcode in (select imprfg from qtemp.items  c where c.imprfg = b.lmcode));

Open in new window

what does the subquery return?

(select  iak from vd_gpl.ax_siop  
                                Where (select imprfg from qtemp.items) = trim(productfamily));

It doesn't appear to be correlated to the updated table despite also referencing that same table.
Is the double reference correct?  Should your update be simplified to something like this?

update qtemp.items a
set itemallocationid = (select  iak from vd_gpl.ax_siop  
                                Where a.imprfg = trim(productfamily));

if multiple rows will match the imprfg /productfamily  condition then you need something to choose one
if the rows will all be the same then it could be as simple as adding  "fetch first 1 row only"

update qtemp.items a
set itemallocationid = (select  iak from vd_gpl.ax_siop  
                                Where a.imprfg = trim(productfamily)
                                    fetch first 1 row only);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.