Looking_for_answers Looking_for_answers - 13 days ago 64
SQL Question

How to update a column with different value using simple Join statement?

I have two tables and i am using a simple join condition between them.

I need to find the common values and updated the String(Success) into the column.

input_table1:

ID || Name || output
1 || ABS || Null
2 || ADF || NULL
3 || AQS || Null
4 || ATF || NULL
5 || APS || Null
6 || AMF || NULL


Input_table2:

ID || Name
1 || ABS
2 || ADF
6 || AMF


Output_table:This is the output I need.

ID || Name || output
1 || ABS || Success
2 || ADF || Success
3 || AQS || Null
4 || ATF || NULL
5 || APS || Null
6 || AMF || Success.


This is the query i am using and this is the error is am receiving
An expression of non-boolean type specified in a context where a condition is expected, near 'Then'.

update .[dbo].[InputTable1]
set Output=
case when (
select INT.ID
from [dbo].[input_table1] INT
join [dbo].[input_table2] SHB
on INT.ID=SHB.ID
) Then 'Success' Else Null End

Answer Source

your query should simply be

update INT
set Output= 'Success'
FROM
[dbo].[input_table1] INT
join [dbo].[input_table2] SHB
on INT.ID=SHB.ID

See working demo