How to update row if exist A,B,Tarih and Not exist how to insert?
My original Code:
Insert X(A,B,C,D,E,Tarih)
select substring(dosya,2,25) as A,
substring(dosya,27,15) as B,
substring(dosya,70,40) as C,
CONVERT(DECIMAL(17,2),substring(dosya,52,17)) as D,
case when substring(dosya,124,2)='00' then 'TL'
when substring(dosya,124,2)='01' then 'USD'
when substring(dosya,124,2)='02' then 'CHF'
when substring(dosya,124,2)='03' then 'CAD'
when substring(dosya,124,2)='04' then 'KWD'
when substring(dosya,124,2)='05' then 'GBP'
when substring(dosya,124,2)='06' then 'SAR'
when substring(dosya,124,2)='07' then 'JPY'
when substring(dosya,124,2)='08' then 'EUR'
when substring(dosya,124,2)='09' then 'AUD'
when substring(dosya,124,2)='10' then 'IRR'
when substring(dosya,124,2)='11' then 'DK'
when substring(dosya,124,2)='12' then 'SEK'
else 'Döviz' end as E,
@tarih as Tarih
from #TempLog
i want to update existing value
Pseudo code :
if exist ( A,B,C in BankaEntegrasyonLog)
{
Update Row!
}
else
{
Insert
X(A,B,C,D,E,Tarih)
select substring(dosya,2,25) as . . . . .
}
You’ll want to use the MERGE statement.
First, let’s create a test table:
And we can insert some values into it:
Now, let’s use MERGE with a key that already exists. If we should update ‘Something’ to ‘updated!’ for SomeKey = 43:
We can check that it worked:
SELECT * FROM TestLog WHERE SomeKey = 43;
and we indeed see “Updated!” for SomeAttribute. We can try a new value:
And to check it:
We indeed see a new fourth row with (22, ‘Newone!’). It shouldn’t be hard to expand this example to your specific table and insert pattern. Let me know if you need more help.