SQL Server: pointing to the ID from other table
I have two tables: (ID = int, Match = varchar, Status = char
)
TableA
ID1 Match1 Status1
23 1200 PASS
24 1300 FAIL
25 1400 PASS
26 1500 PASS
27 1600 FAIL
TableB
ID2 Match2 Status2
456 1200
784 1300
457 1300
124 1400
741 1600
Now, I want to populate tableB (status2)
with 'FAIL' where there is fail in tableA
(match
).
so, I should get:
TableB
ID2 Match2 Status2
456 1200 NULL
784 1300 FAIL
457 1300 FAIL
124 1400 NULL
741 1600 FAIL
now this is pretty simple. I want to put in status2, ID1 which caused the fail so the expected result would be:
TableB
ID2 Match2 Status2
456 1200 NULL
784 1300 FAIL of ID 24
457 1300 FAIL of ID 24
124 1400 NULL
741 1600 FAIL of ID 27
I am currently using simple update statement as follows:
update B
set status2 = 'Fail'
from tableB B
Inner join tableA A
on a.match1 = b.match2
where a.status1 = 'FAIL'
开发者_StackOverflow社区
Please correct to point to the ID1.
Thanks
update B
set status2 = 'Fail of ID ' + CAST(A.ID1 AS VARCHAR(4))
from tableB B
Inner join tableA A
on a.match1 = b.match2
where a.status1 = 'FAIL'
精彩评论