如何在单个语句中使用子查询更新多个列?

时间:2012-05-03 17:04:16

标签: sql sybase

我正在尝试从源表更新临时表:

    UPDATE #DETAIL
        SET EXCD_ID, CDOR_OR_AMT, CDOR_OR_VALUE
        (SELECT 
            CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
        FROM
            CMC_CDID_DATA CDID
        WHERE
            CDID.CLCL_ID = DTL.CLCL_ID AND
            CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
            CDID_TYPE = 'NDC'
    )
    FROM #DETAIL DTL
    WHERE DTL.CDOR_OR_ID = 'XS'

不幸的是它抱怨

Incorrect syntax near ',' (on the '(SELECT' line)
Incorrect syntax near 'FROM' (the second one)

2 个答案:

答案 0 :(得分:4)

经过多次反复试验后,我在工作中汇集了一些帮助,我们想出了这个:

    UPDATE #DETAIL
        SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
            DTL.CDOR_OR_AMT = CONVERT(MONEY,CDID.CDID_ADDL_DATA),
            DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
    FROM #DETAIL DTL
    INNER JOIN 
            CMC_CDID_DATA  CDID ON
            CDID.CLCL_ID = DTL.CLCL_ID AND
            CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO 
    WHERE DTL.CDOR_OR_ID = 'XS'
          AND CDID.CDID_TYPE = 'NDC'

哪个sybase似乎接受。

答案 1 :(得分:3)

你必须像这样进行更新:

UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
    DTL.CDOR_OR_AMT = CDID.CDID_ADDL_DATA
    DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN (SELECT 
            CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
        FROM
            CMC_CDID_DATA ) CDID ON CDID.CLCL_ID = DTL.CLCL_ID AND
            CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
            CDID_TYPE = 'NDC'
WHERE DTL.CDOR_OR_ID = 'XS'

查看THIS ARTICLE了解详情!