1

我想更新一个表。但是查询返回错误

派生表“colIndex”不可更新,因为它包含聚合或 DISTINCT 或 GROUP BY 子句

我怎样才能做到这一点?如果没有直接的方式,也建议任何替代方式。

包括查询:

UPDATE COLINDEX
SET
    COLINDEX.QRESID = ROWINDEX.FRESID
FROM (SELECT QE.COLID,PS.ID SECID,PS.T_ID TEMPLATEID,QE.QUESTIONID,
    MIN(QE.SEQNUM) SEQNUM, Q.QRESID
    FROM SECTION PS
    JOIN SECTIONQUESTIONLINK SX ON PS.ID = SX.SECID
    JOIN QUESTION Q ON SX.QUESTIONID=Q.ID
    JOIN ELEMENT QE ON SX.QUESTIONID=QE.QUESTIONID
    WHERE QE.CONTROLID<>12 
    GROUP BY PS.ID,COLID,PS.T_ID,QE.QUESTIONID ,Q.QRESID)COLINDEX
JOIN (SELECT QE.SEQNUM FSEQNO,QE.ERESID FRESID,PS.ID SECID,PS.T_ID TEMPLATEID,QE.COLID FROM SECTION PS
    JOIN SECTIONQUESTIONLINK SX ON PS.ID = SX.SECID
    JOIN ELEMENT QE ON SX.QUESTIONID=QE.QUESTIONID
    WHERE QE.CONTROLID=12 AND QE.SEQNUM <= PS.COLUMNS + PS.ROWLABEL)ROWINDEX
ON COLINDEX.SECID=ROWINDEX.SECID AND COLINDEX.COLID=ROWINDEX.COLID
AND COLINDEX.TEMPLATEID=5000

从这个查询的一部分正确地提取我想要更新的行。

4

1 回答 1

2

鉴于这COLINDEX是一个派生表——也就是说,它并不真正存在——你如何更新它?

看起来您真正想要的是更新QUESTION,即包含QRESID.

您需要重组更新语句。它应该类似于以下查询。明白我没有(因为我不能,没有你的桌子)以任何方式测试这个,它可以大大简化:

UPDATE QUESTION
SET QUESTION.QRESID = Source.FRESID
FROM QUESTION 
   JOIN ( SELECT QRESID, FRESID FROM
     (SELECT QE.COLID,PS.ID SECID,PS.T_ID TEMPLATEID,QE.QUESTIONID,
     MIN(QE.SEQNUM) SEQNUM, Q.QRESID
     FROM SECTION PS
    JOIN SECTIONQUESTIONLINK SX ON PS.ID = SX.SECID
    JOIN QUESTION Q ON SX.QUESTIONID=Q.ID
    JOIN ELEMENT QE ON SX.QUESTIONID=QE.QUESTIONID
    WHERE QE.CONTROLID<>12 
    GROUP BY PS.ID,COLID,PS.T_ID,QE.QUESTIONID ,Q.QRESID)COLINDEX
      JOIN (SELECT QE.SEQNUM FSEQNO,QE.ERESID FRESID,PS.ID SECID,PS.T_ID TEMPLATEID,QE.COLID     FROM SECTION PS
    JOIN SECTIONQUESTIONLINK SX ON PS.ID = SX.SECID
    JOIN ELEMENT QE ON SX.QUESTIONID=QE.QUESTIONID
    WHERE QE.CONTROLID=12 AND QE.SEQNUM <= PS.COLUMNS + PS.ROWLABEL)ROWINDEX
ON COLINDEX.SECID=ROWINDEX.SECID AND COLINDEX.COLID=ROWINDEX.COLID
AND COLINDEX.TEMPLATEID=5000) Source
ON Source.QRESID = QUESTION.QRESID
于 2013-03-21T15:42:41.803 回答