I need to update a column in table based on some condition.
here is the table -
DGTID | SKAcc | Linkedaccount | DGTStatus
1 8002180831 8102651144,8005370302 C
2 8005370302 8002170111 I
3 8002012348 I
So, for a particular DGT ID, if status = 'C' and if Linkedaccount is present in any other SKAcc, then DGTStatus for that SKAcc should also be C.
Lets say, DGTId = 1, it has DGTStatus as 'C' and it has a linkedaccount = '8005370302' which is the SKAcc for DGTID='2' . So, its DGTStatus should also be 'C'
Now, I believe the main part here is to extract linked account as there can be multiple comma seperated linked account.
This is what I wrote to extract linked account -
select replace(''''+LinkedAccount+'''',',',''',''') from IDCOPES..T_CDI_WHT_DGT where ISNULL(LinkedAccount,'')<>''
This gives me '8102651144','8005370302'.
and then, I wrote -
IF EXISTS (SELECT 1 from IDCOPES..T_CDI_WHT_DGT where DGTID=1 and DGTSTATUS='C')
UPDATE IDCOPES..T_CDI_WHT_DGT
SET DGTSTATUS='C'
Where SKACC in (select replace(''''+LinkedAccount+'''',',',''',''') from IDCOPES..T_CDI_WHT_DGT where ISNULL(LinkedAccount,'')<>'' and DGTID=1)
But, this doesnt work. However, when I write
IF EXISTS (SELECT 1 from IDCOPES..T_CDI_WHT_DGT where DGTID=1 and DGTSTATUS='C')
UPDATE IDCOPES..T_CDI_WHT_DGT
SET DGTSTATUS='C'
Where SKACC in ('8102651144','8005370302')
It works. I can't understand why it won't work when both code are same.