我想更新一张桌子。但查询返回错误
派生表'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
从此查询的一部分正确拉出我想要更新的行。
答案 0 :(得分: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