• You can try the query below. If it produces the correct 3 records you need for each row, just add an INSERT before it to put them into the table.

    SELECT a.[Acc No], a., a.PayDate, a.[Code]

    ,TaxAmount=CASE WHEN Code1 IS NULL THEN -c.TaxAmount

    WHEN Code1 = Code2 THEN b.Order2

    ELSE 0 END

    ,[TaxRate]=CASE WHEN Code1 IS NULL THEN c.[TaxRate]

    WHEN Code1 = Code2 THEN c.[TaxRate]

    ELSE 0 END

    ,[GrossAmount]=CASE WHEN Code1 IS NULL THEN -c.[GrossAmount]

    WHEN Code1 = Code2 THEN b.Order1

    ELSE b.Order1 END

    FROM dbo.datasheet a

    CROSS APPLY (

    VALUES (NULL, NULL, NULL, NULL)

    ,(NewOrder_Code, [NewOrder ( code 6)], [TaxOnNewOrder_Code], [TaxOnNewOrder ( code 06)])

    ,([NewOrderDiv_Code ], [NewOrderDiv_Code ( code 37)], NULL, NULL)) b (Code1, [Order1], Code2, [Order2])

    INNER JOIN dbo.originalsheet c

    ON a.[Acc No] = c.[AccountNo] AND a. = c. AND a.PayDate = c.PayDate AND

    a.[Code] = c.[Code]


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St