1

我正在使用 SQL 合并并尝试将两个表合并到一个表中。

基本上,有三个名为:t1、t2、t3 的表。我正在尝试做的是使用合并将 t2 表数据和 t3 表数据放入 t1 表中。

这是代码:

MERGE 
daily_so_invoice AS target1
USING 
temp_invoice AS source1 ,temp_so_invoicedetail AS source2

ON 
target1.invoice_id = RIGHT('00000000'+ CONVERT(VARCHAR,source1.invoice_instance_id),8) and
target1.LineKey<>'9999' and 
RIGHT('0000000'+CONVERT(VARCHAR,source1.invoice_instance_id),7) = RIGHT('0000000'+CONVERT(VARCHAR,source2.invoice_instance_id),7)
WHEN MATCHED THEN
        UPDATE 
        SET     
  target1.batchno =   upper(RIGHT('00'+CONVERT(VARCHAR,DAY(source1.billing_date)),2) + left(datename(month, source1.billing_date), 3))       
 WHEN NOT MATCHED THEN
 insert 
(
invoice_id,
LineKey,
item_unit_price ,
invoiced ,
batchno ,
item_name ,
item_description ,
quantity
)
VAlUES
(
RIGHT('00000000'+CONVERT(VARCHAR,source2.invoice_instance_id),8),
RIGHT('0000'+CONVERT(VARCHAR,source2.linekey),4),
source2.item_unit_price ,
'N' ,
  upper(RIGHT('00'+CONVERT(VARCHAR,DAY(source1.billing_date)),2) + left(datename(month, source1.billing_date), 3)),
'/'+source2.item_name ,
source2.item_description ,
source2.quantity 
);

我在以下行遇到错误:

USING 
temp_invoice AS source1 ,temp_so_invoicedetail AS source2

使用两个表时如何进行合并?

4

2 回答 2

1

你在寻找这样的东西吗?

WITH cteSource As
(
    SELECT
        -- Not sure if these two columns are different?
        -- You're joining on 7 characters, but taking 8:
        RIGHT('00000000'+ CONVERT(VARCHAR, source1.invoice_instance_id), 8) As invoice_id,
        RIGHT('00000000' + CONVERT(VARCHAR, source2.invoice_instance_id), 8) As invoice_instance_id,
        RIGHT('0000' + CONVERT(VARCHAR, source2.linekey), 4) As LineKey,
        source2.item_unit_price,
        'N' As invoiced,
        UPPER(RIGHT('00' + CONVERT(VARCHAR, DAY(source1.billing_date)), 2) + LEFT(DateName(month, source1.billing_date), 3)) As batchno,
        '/' + source2.item_name As item_name,
        source2.item_description,
        source2.quantity
    FROM
        temp_invoice AS source1
        INNER JOIN temp_so_invoicedetail AS source2
        ON RIGHT('0000000' + CONVERT(VARCHAR, source1.invoice_instance_id), 7) 
         = RIGHT('0000000' + CONVERT(VARCHAR, source2.invoice_instance_id), 7)
)
MERGE 
    daily_so_invoice AS target
    USING cteSource As source
    ON target.invoice_id = source.invoice_id
    And target.LineKey != '9999'

WHEN MATCHED THEN
    UPDATE 
    SET     
        batchno =  batchno

WHEN NOT MATCHED THEN
    INSERT
    (
        invoice_id,
        LineKey,
        item_unit_price,
        invoiced,
        batchno,
        item_name,
        item_description,
        quantity
    )
    VALUES
    (
        invoice_instance_id,
        LineKey
        item_unit_price ,
        invoiced,
        batchno,
        item_name,
        item_description,
        quantity 
    );
于 2013-05-02T19:13:07.727 回答
0

不,您不能直接使用 2 个来源。但是,您可以使用 CTE。事实上,它也会让你的陈述更容易阅读。

WITH MySource AS (SELECT 
                    RIGHT('00000000'+CONVERT(VARCHAR,source2.invoice_instance_id),8) AS Invoice_Id,
                    RIGHT('0000'+CONVERT(VARCHAR,source2.linekey),4) AS LineKey,
                    source2.item_unit_price ,
                    upper(RIGHT('00'+CONVERT(VARCHAR,DAY(source1.billing_date)),2) + left(datename(month, source1.billing_date), 3)) AS BatchNo,
                    '/'+source2.item_name AS Item_Name,
                    source2.item_description ,
                    source2.quantity 
                FROM temp_invoice AS source1 
                JOIN temp_so_invoicedetail AS source2
                    ON RIGHT('0000000'+CONVERT(VARCHAR,source1.invoice_instance_id),7) = 
                            RIGHT('0000000'+CONVERT(VARCHAR,source2.invoice_instance_id),7)
            )
MERGE 
    daily_so_invoice AS target1
USING 
    MySource
ON 
    target1.invoice_id = MySource.Invoice_Id
    AND target1.LineKey<>'9999' 
WHEN MATCHED THEN
    UPDATE 
    SET target1.batchno =   
            MySource.BatchNo
 WHEN NOT MATCHED THEN
    INSERT
    (
        invoice_id,
        LineKey,
        item_unit_price ,
        invoiced ,
        batchno ,
        item_name ,
        item_description ,
        quantity
    )
    VAlUES
    (
        MySource.Invoice_Id,
        MySource.LineKey,
        MySource.item_unit_price ,
        'N' ,
        MySource.BatchNo,
        MySource.item_name ,
        MySource.item_description ,
        MySource.quantity 
    );

希望我做对了。当您没有用于运行测试的结构时,这很难。如果没有,我想我已经很接近了。

于 2013-05-02T19:12:49.633 回答