更新同一表中的记录,搜索另一个表中的常见记录

时间:2016-07-15 10:51:43

标签: sql-server sql-server-2008

首先为长标题道歉,但我找不到另一个合适的头衔 对于我要解释的请求: 在db中有3个表,一个提供文档头,一个提供相关详细信息, 第三个用于"链接"在不同类型的文件之间(例如,如果有的话) 发货单后跟发票,通常在月末生成,将所有发票分组 同月的送货单)。 涉及的表和字段称为:

    + DO11_DOCTESTATA  (for the headers)
     - DO11_DOCUM_MG36 Gives the type of the document stored (I need to retrieve data from 'FOR-FATRIEPDDT' and insert into 'FOR-DDT'
     - DO11_NUMDOC Contains the number of the document, needed to search WHERE do11_numdoc = equals the same number in DO33_DOCCORPORIF

    + DO30_DOCCORPO    (for the details)
     - DO30_PREZZO1    Contains the unity price, when I have a match between do11_numdoc = do33_doccorporif.do33_numnsdoc, I update this field)
     - DO30_PREZZO1IVA Same as before, but price added with VAT
     - DO30_IMPORTO    Price * Quantity
     - ...
     - ...

    + DO33_DOCCORPORIF (for the links)
     - DO33_NUMNSDOC  Which contains the number of the relative delivery note that I need to compare to DO11_DOCTESTATA.DO11_NUMDOC to find a match

我的要求是更新DO30_DOCCORPO中的某些字段,因为我需要更新发票中的价格 通过将DO11_DOCTESTATA.DO11_NUMDOC中找到的数字与相同字段匹配,将相同的值放入相应的交货单中 在DO33_DOCCORPORIF.DO33_NUMNSDOC

当找到匹配项时,我需要更新DO30_DOCCORPO中的字段,WHERE DO11_DOCTESTATA =' FOR-DDT'意思是我只在更新时才更新数据 文档类型是交货单(' FOR-DDT')。

我设法创建了SELECT查询,它为我提供了我需要更新的数据,但我无法"转换"这个查询进入UPDATE因为 我缺乏SQL语言的经验。这是SELECT查询

SELECT do11_doctestata.do11_docum_mg36, 
   do11_doctestata.do11_datadoc, 
   do11_doctestata.do11_numdoc, 
   do11_doctestata.do11_sezdoc, 
   do33_doccorporif.do33_numnsdoc, 
   do30_doccorpo.do30_codart_mg66, 
   do30_doccorpo.do30_qta1, 
   do30_doccorpo.do30_prezzo1, 
   do30_doccorpo.do30_prezzo1iva, 
   do30_doccorpo.do30_importo, 
   do30_doccorpo.do30_importoiva, 
   do30_doccorpo.do30_impnetscp 
FROM   do11_doctestata 
   INNER JOIN do33_doccorporif 
           ON do11_doctestata.do11_numdoc = do33_doccorporif.do33_numnsdoc 
   INNER JOIN do30_doccorpo 
           ON do11_doctestata.do11_ditta_cg18 = 
              do30_doccorpo.do30_ditta_cg18 
              AND do11_doctestata.do11_numreg_co99 = 
                  do30_doccorpo.do30_numreg_co99 
WHERE  ( do11_doctestata.do11_docum_mg36 = 'FOR-DDT' ) 

here是结果的示例。突出显示的行是 我需要更新的典型行,但仅当DO11_NUMDOC和DO33_NUMNSDOC的数量匹配时

编辑:仍然坚持这个问题 编辑:使用以下代码:

MERGE INTO DO30_DOCCORPO A
    USING DO30_DOCCORPO_TEMP B
        ON A.DO30_PROGRIGA = B.DO30_PROGRIGA
        AND A.DO30_NUMREG_CO99 = B.DO30_NUMREG_CO99
        AND A.DO30_DITTA_CG18 = B.DO30_DITTA_CG18
        AND A.DO30_CAUSMAG_MG51 = '9999'
WHEN MATCHED THEN
    UPDATE
        SET DO30_PREZZO1 = B.DO30_PREZZO1,
            DO30_PREZZO1IVA = B.DO30_PREZZO1IVA,
            DO30_IMPORTO = B.DO30_IMPORTO,
            DO30_IMPORTOIVA = B.DO30_IMPORTOIVA,
            DO30_IMPNETSCP = B.DO30_IMPNETSCP;

我设法让Management Studio做点什么。事实上,在执行之后,它说,"行影响:1"但还有一个问题:行

AND A.DO30_CAUSMAG_MG51 = '9999' should update WHERE DO30_CAUSMAG_MG51 = '100' 

因此,当值为' 9999'但更新同一列具有' 100'作为价值

1 个答案:

答案 0 :(得分:0)

这是你需要的吗?

UPDATE DO30_DOCCORPO
SET  DO30_DOCCORPO.do30_importo=... 
    , ...
FROM   do11_doctestata 
   INNER JOIN do33_doccorporif 
           ON do11_doctestata.do11_numdoc = do33_doccorporif.do33_numnsdoc 
   INNER JOIN do30_doccorpo 
           ON do11_doctestata.do11_ditta_cg18 = 
              do30_doccorpo.do30_ditta_cg18 
          AND do11_doctestata.do11_numreg_co99 =do30_doccorpo.do30_numreg_co99 
WHERE  ( do11_doctestata.do11_docum_mg36 = 'FOR-DDT' ) 

评论后编辑:现在怎么样?