SQL INSERT和UPDATE

时间:2014-02-07 19:44:26

标签: sql sql-server insert-update

我在Microsoft SQL Server中工作,我的数据库存在问题,我正在尝试将新数据插入表中,但数据在过去被意外插入,因此重新插入造成重大违规。我正在寻找一种方法来插入我需要的数据并更新已经意外包含的内容。为简洁起见,代码的一些细节已经改变,但我现在的代码是:

BEGIN TRANSACTION 

INSERT INTO dbs_salesinternals.acc_saleshistory 
SELECT a, 
       b AS n 
FROM   dbs_sales.orders AS O 
       INNER JOIN dbs_salesinternals.acc_report_months AS ARM 
               ON ARM.acc_report_month_id = dbo.Reportmonth(CASE 
                             WHEN O.ordertypeid = 2 THEN shipduedate 
                             ELSE shipdate 
                                                            END) 
       INNER JOIN dbo.contactfullnames 
               ON O.customerid = dbo.contactfullnames.contact_id 
       INNER JOIN dbs_sales.ordertypes 
               ON dbs_sales.ordertypes.ordertypeid = O.ordertypeid 
       LEFT JOIN(SELECT OD.orderid, 
                        Sum(CASE 
                              WHEN PB.format LIKE '%kit%' 
                                   AND units.curriculumcode = 'CUR' THEN 
                              OD.price 
                              ELSE 0 
                            END) AS KitPart, 
                        Sum(CASE 
                              WHEN PB.format = 'PD' 
                                   AND units.curriculumcode = 'CUR' THEN 
                              OD.price 
                              ELSE 0 
                            END) AS PDPart, 
                        Sum(CASE 
                              WHEN ( PB.format <> 'PD' 
                                     AND PB.format NOT LIKE '%kit%' 
                                     AND units.curriculumcode = 'CUR' ) THEN 
                              OD.price 
                              ELSE 0 
                            END) AS OtherPart, 
                        Sum(CASE 
                              WHEN PB.format LIKE '%kit%' 
                                   AND units.curriculumcode = 'WEB' THEN 
                              OD.price 
                              ELSE 0 
                            END) AS EKitPart, 
                        Sum(CASE 
                              WHEN PB.format = 'PD' 
                                   AND units.curriculumcode = 'WEB' THEN 
                              OD.price 
                              ELSE 0 
                            END) AS EPDPart, 
                        Sum(CASE 
                              WHEN ( PB.format <> 'PD' 
                                     AND PB.format NOT LIKE '%kit%' 
                                     AND units.curriculumcode = 'WEB' ) THEN 
                              OD.price 
                              ELSE 0 
                            END) AS EGuidePart 
                 FROM   dbs_salesinternals.eieproductsbase AS PB 
                        INNER JOIN dbs_salesinternals.productpricing AS PP 
                                ON PB.productid = PP.product_id 
                        INNER JOIN dbs_sales.[order details] AS OD 
                                ON PP.productpricing_id = OD.productpricing_id 
                        INNER JOIN units 
                                ON PB.unit_id = units.unit_id 
                 GROUP  BY OD.orderid) OP 
              ON OP.orderid = O.orderid 
       LEFT JOIN [dbo].[grant_codes] 
              ON [dbo].[grant_codes].grantcodemap_id = O.accountcode 
WHERE  O.ordertotal <> 0 
       AND O.ordertypeid IN ( 1, 2, 3, 4 ) 
       AND ARM.acc_report_month_id = 166 
       AND ( [dbo].[grant_codes].granttype IS NULL 
              OR [dbo].[grant_codes].granttype <> 2 ) 

COMMIT TRANSACTION 

如何更改此项以完成我需要的工作?

1 个答案:

答案 0 :(得分:0)

我没有使用评论中提到的合并工具,它可能会更好,但我会这样做:

首先,将现有查询更改为插入临时表的内容(临时表在其名称前用#表示):

select <your select-clause>
into #temp
from <your from-clause>
where <your where-clause>

所以现在我们有2个表,都包含数据。让我们调用我们要添加到S的表格,并将临时表格用作要添加的数据的基础T

首先,我们需要确定TS中已存在哪些行。{1}}。它们可能有一个共同的行标识符,我们称之为id,并通过一个简单的连接找到TS中有相应行的行集:

select * from S s join T t on s.id = t.id;

此查询中返回的行是我们要更新的行。为此,可以使用此类型的查询:

update S 
set c1 = (select t.c1 from T t where t.id = S.id),
c2 = (select t.c2 from T t where t.id = S.id),
...
cN = (select t.cN from T t where t.id = S.id)
where S.id in (
    select t.id 
    from S s join T t on s.id = t.id);

我们更新ST中存在的S中的所有行,并选择T内部选择的值。

接下来,我们需要在T中插入尚未包含在S中的行。此查询将执行此操作:

insert into S (id, c1, c2, ..., cN)
select id, c1, c2, ..., cN
from T where id not in (select id from S);

(可以在where - 子句中使用连接,但这也可以。)

此查询会将T中尚未加入S的行插入S