如何使用Sql Server 2008将两个表中的数据合并为一个?

时间:2013-05-02 18:36:00

标签: sql sql-server sql-server-2008 tsql

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

基本上,有三个表名为:t1,t2,t3。我正在尝试做的是使用merge将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

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

2 个答案:

答案 0 :(得分: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 
    );

答案 1 :(得分: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 
    );

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