如果存在A,B,Tarih和Not存在如何更新行如何插入?
我的原始代码:
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
我想更新现有值
伪代码:
if exist ( A,B,C in BankaEntegrasyonLog)
{
Update Row!
}
else
{
Insert
X(A,B,C,D,E,Tarih)
select substring(dosya,2,25) as . . . . .
}
答案 0 :(得分:2)
您将要使用MERGE语句。
首先,让我们创建一个测试表:
CREATE TABLE TestLog( SomeKey INTEGER, SomeAttribute VARCHAR(80));
我们可以在其中插入一些值:
INSERT INTO TestLog( SomeKey, SomeAttribute ) VALUES ( 43, 'Something' );
INSERT INTO TestLog( SomeKey, SomeAttribute ) VALUES ( 55, 'Another' );
INSERT INTO TestLog( SomeKey, SomeAttribute ) VALUES ( 77, 'Demo' );
现在,让我们使用MERGE和已经存在的密钥。如果我们应该将'Something'更新为'更新'! for SomeKey = 43:
MERGE TestLog AS TARGET
USING (SELECT 43 AS SomeKey, 'Updated!' AS SomeAttribute)
AS SOURCE( SomeKey, SomeAttribute )
ON (TARGET.SomeKey = SOURCE.SomeKey )
WHEN MATCHED THEN
UPDATE SET SomeAttribute = Source.SomeAttribute
WHEN NOT MATCHED THEN
INSERT (SomeKey, SomeAttribute)
VALUES (SOURCE.SomeKey, SOURCE.SomeAttribute);
我们可以检查它是否有效:
SELECT * FROM TestLog WHERE SomeKey = 43;
我们确实看到“已更新!”对于SomeAttribute。我们可以尝试新的价值:
MERGE TestLog AS TARGET
USING (SELECT 22 AS SomeKey, 'Newone!' AS SomeAttribute)
AS SOURCE( SomeKey, SomeAttribute )
ON (TARGET.SomeKey = SOURCE.SomeKey )
WHEN MATCHED THEN
UPDATE SET SomeAttribute = Source.SomeAttribute
WHEN NOT MATCHED THEN
INSERT (SomeKey, SomeAttribute)
VALUES (SOURCE.SomeKey, SOURCE.SomeAttribute);
并检查它:
SELECT * FROM TestLog;
我们确实看到了新的第四行(22,'Newone!')。将此示例扩展到特定表并插入模式应该不难。如果您需要更多帮助,请告诉我。
答案 1 :(得分:0)
这样的事情:
MERGE [dbo].[X] AS TARGET
USING (
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
) AS SOURCE
ON SOURCE.[A] = TARGET.[A]
AND SOURCE.[B] = TARGET.[B]
AND SOURCE.[Tarih] = TARGET.[Tarih]
WHEN NOT MATCHED THEN
INSERT (
[A],
[B],
[C],
[D],
[E],
[Tarih]
)
VALUES (
SOURCE.[A],
SOURCE.[B],
SOURCE.[C],
SOURCE.[D],
SOURCE.[E],
SOURCE.[Tarih]
)
WHEN MATCHED THEN
UPDATE
SET [C] = SOURCE.[C],
[D] = SOURCE.[D],
[E] = SOURCE.[E];
我还会考虑创建一个查找表来替换该case语句。这可能会使未来的维护变得更容易。
答案 2 :(得分:0)
根据建议Gordon Linoff
MERGE X AS target
USING
(
select Id,
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 TutarParaBrimi, @tarih as Tarih
from #TempLog
) AS source
ON target.Id = source.Id
WHEN MATCHED THEN
UPDATE SET A = source.A,
B = source.B,
C = source.C,
D = source.D,
E = source.E,
Tarih = source.Tarih
WHEN NOT MATCHED THEN
INSERT (A, B, C, D, E, Tarih)
VALUES (source.A, source.B, source.C, source.D, source.E, source.Tarih);
如果您没有Id列,则替换上的target.Id = source.Id
A = source.A
AND target.B = source.B
AND target.C = source.C
AND target.D = source.D
AND target.E = source.E
AND target.Tarih = source.Tarih