CREATE PROC [dbo].[SupplementaryInsurance_SaveData]
    @ComputerName NVARCHAR(MAX),
    @GoodsCode VARCHAR(15)
AS
BEGIN
    DELETE FROM dbo.SupplementaryInsurance_DrugCondition
    WHERE InsuranceCode NOT IN
          (
              SELECT InsuranceCode
              FROM ##DrugCondition
              WHERE GoodsCode = @GoodsCode
                    AND ComputerName = @ComputerName
          )
          AND GoodsCode = @GoodsCode;

    INSERT INTO dbo.SupplementaryInsurance_DrugCondition
    (
        GoodsCode,
        InsuranceCode,
        IsSpecialDrug,
        NonInsuranceAmount,
        PatientAmount,
        DifferAmount,
        GoodsAmount,
        Ceilling,
        MessageId
    )
    SELECT @GoodsCode,
           InsuranceCode,
           IsSpecialDrug,
           NonInsuranceAmount,
           PatientAmount,
           DifferAmount,
           GoodsAmount,
           Ceilling,
           MessageId
    FROM ##DrugCondition
    WHERE GoodsCode = @GoodsCode
          AND ComputerName = @ComputerName
          AND InsuranceCode NOT IN
              (
                  SELECT InsuranceCode
                  FROM SupplementaryInsurance_DrugCondition
                  WHERE GoodsCode = @GoodsCode
              );

    UPDATE SupplementaryInsurance_DrugCondition
    SET NonInsuranceAmount = tdc.NonInsuranceAmount,
        IsSpecialDrug = tdc.IsSpecialDrug,
        PatientAmount = tdc.PatientAmount,
        DifferAmount = tdc.DifferAmount,
        GoodsAmount = tdc.GoodsAmount,
        Ceilling = tdc.Ceilling,
        MessageId = tdc.MessageId
    FROM SupplementaryInsurance_DrugCondition dc
        JOIN ##DrugCondition tdc
            ON tdc.GoodsCode = dc.GoodsCode
               AND tdc.InsuranceCode = dc.InsuranceCode
    WHERE dc.GoodsCode = @GoodsCode;
END;