从另一个可重复的值更新列

时间:2010-12-05 18:42:44

标签: tsql sql-server-2008

我有下表

发票

+++++++++++++++++++++++++++
+ InvoiceId + InvoiceDate +
+++++++++++++++++++++++++++
+   int     +     int     +
+++++++++++++++++++++++++++

InvoiceLine

++++++++++++++++++++++++++++++++++++++++++++
+ InvoiceLineId +  InvoiceId + InvoiceDate +
++++++++++++++++++++++++++++++++++++++++++++
+    int        +    int     +    int      +
++++++++++++++++++++++++++++++++++++++++++++

我最近将最后一个(InvoiceDate)列添加到表中,我想用Invoice表中的相应值更新它的值。对于在两个表中匹配的每个InvoiceId,InvoiceDate应添加到InvoiceLine。

如果需要更多输入,请不要犹豫。

2 个答案:

答案 0 :(得分:0)

也许是这样的?

    update detail
    set invoicedate = summary.invoicedate
    from invoiceline detail 
         inner join invoice summary
             on summary.invoiceid = detail.invoiceid

答案 1 :(得分:0)

Update InvoiceLine
Set InvoiceDate = Invoice.InvoiceDate 
From Invoice inner join InvoiceLine on Invoice.InvoiceId = InvoiceLine.InvoiceId