2016-12-21 8 views
-1

私は私を許して(次のコードを使用してSQL ServerでMERGEをやっているにNULL値を挿入することはできません原因MERGEが、私はこれに新しいですし、私はどのように適切にインデントとフォーマットを把握することができませんでしたコピー&ペースト)を介して自分のコード:SQL Serverの:列

MERGE INTO DW_Datawarehouse.[dbo].[DWF_WAREHOUSE] with (HOLDLOCK) AS target 
USING #DataSource AS Source 
     ON target.[DWF_WAREHOUSE_ORDER_NUM] = source.[DWF_WAREHOUSE_ORDER_NUM] 
     AND target.DWF_WAREHOUSE_ORDER_TYPE_CD = source.DWF_WAREHOUSE_ORDER_TYPE_CD 

WHEN MATCHED AND 
     (
     --all non-key fields, attributes and amounts 
     source.[DWF_WAREHOUSE_ORDER_DESC] <> target.[DWF_WAREHOUSE_ORDER_DESC] 
     or source.[DWF_WAREHOUSE_PICK_DATE_DAY] <> target.[DWF_WAREHOUSE_PICK_DATE_DAY] 
     or source.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM] <> target.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM] 
     or source.[DWF_WAREHOUSE_DIFF_QTY] <> target.[DWF_WAREHOUSE_DIFF_QTY] 
     ) 
    THEN 
     UPDATE 
     SET target.[DWF_WAREHOUSE_ORDER_DESC] = source.[DWF_WAREHOUSE_ORDER_DESC], 
      target.[DWF_WAREHOUSE_PICK_DATE_DAY] = source.[DWF_WAREHOUSE_PICK_DATE_DAY], 
      target.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM] = source.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM], 
      target.[DWF_WAREHOUSE_DIFF_QTY] = source.[DWF_WAREHOUSE_DIFF_QTY] 

WHEN NOT MATCHED BY TARGET THEN 
    INSERT ([DWF_WAREHOUSE_ORDER_DESC], [DWF_WAREHOUSE_PICK_DATE_DAY], 
      [DWF_WAREHOUSE_DAY_OF_WEEK_NUM], [DWF_WAREHOUSE_DIFF_QTY]) 
    VALUES (source.[DWF_WAREHOUSE_ORDER_DESC], source.[DWF_WAREHOUSE_PICK_DATE_DAY], 
      source.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM], source.[DWF_WAREHOUSE_DIFF_QTY]) 

WHEN NOT MATCHED BY SOURCE THEN 
    DELETE; 

私はエラーを取得しています:

Cannot insert the value NULL into column 'DWF_WAREHOUSE_ORDER_NUM', table 'DW_Datawarehouse.dbo.DWF_WAREHOUSE'; column does not allow nulls. UPDATE fails.

どのように私はこのエラーを回避するために、私のMERGEコードを変更しますか?

+3

: あなたは

SELECT * FROM #DataSource AS WHERE DWF_WAREHOUSE_ORDER_NUM IS NOT NULL 

に例をデータソースを変更することができます。あなたの現在の 'Merge'クエリはこのエラーを生成しません –

+1

これは、' DWF_WAREHOUSE_ORDER_NUM'カラムの値を指定する必要があることを意味します。あなたの 'INSERT'ステートメントはこの列を含みません。 – DVT

答えて

0

データソースにDWF_WAREHOUSE_ORDER_NUMのヌルがあるようです。あなたが `` DWF_WAREHOUSE`テーブル上trigger`を持っていますか

MERGE INTO DW_Datawarehouse.[dbo].[DWF_WAREHOUSE] with (HOLDLOCK) as target 
    USING 
(SELECT * FROM #DataSource AS WHERE DWF_WAREHOUSE_ORDER_NUM IS NOT NULL)AS Source 

    ON target.[DWF_WAREHOUSE_ORDER_NUM] = source.[DWF_WAREHOUSE_ORDER_NUM] 
    AND target.DWF_WAREHOUSE_ORDER_TYPE_CD = source.DWF_WAREHOUSE_ORDER_TYPE_CD 

WHEN MATCHED AND 
    (
    --all non-key fields, attributes and amounts 
    source.[DWF_WAREHOUSE_ORDER_DESC] <> target.[DWF_WAREHOUSE_ORDER_DESC] 
    or source.[DWF_WAREHOUSE_PICK_DATE_DAY] <> target.[DWF_WAREHOUSE_PICK_DATE_DAY] 
    or source.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM] <> target.[DWF_WAREHOUSE_DAY_OF_WEEK_NUM] 
    or source.[DWF_WAREHOUSE_DIFF_QTY] <> target.[DWF_WAREHOUSE_DIFF_QTY] 
    ) 
.....