Error executing update with sub-query

Asked

Viewed 243 times

0

I have the following situation, I need to update several fields based on the return of a sub-query that returns a main code. This subconsultation is done with Join in two tables to obtain the code. When performing, I have the following error:

Message 512, Level 16, Status 1, Line 24 The subconsultation returned more than 1 value. This is not allowed when the subconsulta follows =, != , <, <= , >, >= or when it is used as an expression.

This is the consultation:

BEGIN TRANSACTION;
UPDATE Produto SET TpProduto = 'C' WHERE IdProduto = (
SELECT DISTINCT(p.IdProduto)
FROM dbo.Produto p 
JOIN dbo.CodigoProduto  cp ON cp.IdProduto = p.IdProduto
WHERE cp.CdChamada IN(
'3VT9200-4ED30                 ',
'3VT9225-6AC00                 ',
'5SX11207                      ',
'6ES73921AJ000AA0              ',
'8910427                       ',
'9676812                       ',
'CON000280                     '

 )

)

1 answer

0


this select return more than one value

SELECT DISTINCT(p.IdProduto)

how to compare

WHERE IdProduto = (vários valores)

this piece here needs to return only one value

SELECT DISTINCT(p.IdProduto)
FROM dbo.Produto p 
JOIN dbo.CodigoProduto  cp ON cp.IdProduto = p.IdProduto
WHERE cp.CdChamada IN(
'3VT9200-4ED30                 ',
'3VT9225-6AC00                 ',
'5SX11207                      ',
'6ES73921AJ000AA0              ',
'8910427                       ',
'9676812                       ',
'CON000280                     '

 )

check if that’s not it.

  • That’s right, I need to compare multiple returns, the ones in the sub-colony and update them. This database I’m working on is confusing, it generates the code in different tables and as it is a lot of data I thought to take the return of the sub-concession and go updating.

  • 1

    Already solved, just change the instruction from = to in in the first query.

  • mark as answered, to help other people.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.