Update a value in a conditional column by comparing 2 columns from 2 different tables

Asked

Viewed 97 times

0

In a table, I need to enter the value 'ID Related Assembly' in a field, with the following condition:

  • This value can only be entered when the value of the "Id_table1" field is equal to the values that are in a field ("Id_table2") of another table.

This is the code I’m using:

UPDATE tabela1 SET _Montagem = 'ID Relacionado Montagem' WHERE 
bancodb.dbo.tabela1.Id_tabela1 = (SELECT Id_tabela2 FROM tabela2)

SQL is returning me the following error:

A subconsulta retornou mais de 1 valor. Isso não é permitido quando a subconsulta segue um =, !=, <, <= , >, >= ou quando ela é usada como uma expressão.

Most likely because I am using the operator "=", I would like to know how I can condition this insert to the comparison of the 2 fields.

  • 1

    Can you share more information about your table structure? Without being sure of the structure, my best suggestion is to use the IN instead of the =.

  • I just did that, solved my problem. I’m new to SQL and didn’t know the IN command... Thanks!!! :)

  • 1

    Good! I posted as an answer if you want to accept. In case someone has a similar problem in the future.

1 answer

1


Browser other questions tagged

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