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を使用してみましょう。'Something'を'updated!'に更新する必要がある場合 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!')の新しい4行目が表示されます。この例を特定のテーブルに拡張してパターンを挿入するのは難しいことではありません。さらにサポートが必要な場合はお知らせください。

于 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

推奨事項によるとゴードン・リノフ

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 on
A = source.A AND target.B = source.B AND target.C = source.C AND target.D = source.DANDtargetに置き換え
ます。 E = source.E AND target.Tarih = source.Tarih

于 2012-12-18T16:01:28.543 に答える