1

如果存在 A、B、Tarih 和不存在如何更新行如何插入?

我的原始代码:

 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   . . . . .  

}
4

3 回答 3

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 和一个已经存在的键。如果我们应该将“某事”更新为“更新!” 对于 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;

我们确实看到了“更新!” 对于某些属性。我们可以尝试一个新的值:

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!')。将此示例扩展到您的特定表和插入模式应该不难。如果您需要更多帮助,请告诉我。

于 2012-12-18T15:55:06.503 回答
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 语句。这可能使将来的维护更容易。

于 2012-12-18T16:01:25.017 回答
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

于 2012-12-18T16:01:28.543 回答