选择每一行并插入另一个表

时间:2016-03-15 10:19:46

标签: oracle select join foreach insert

乍一看,这是我的查询

 INSERT INTO T2 
 SELECT T1.*,  T3.DATETIME, T3.STATEID
 FROM T1 WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE
 LEFT OUTER JOIN T3
 ON (T1.DOCID =  T3.DOCID);

T1 AND T2在列数方面几乎相同;只有T2有两列额外的(CREATIONDATE,STATEID)

T1的每一行都需要插入T2 +另外两列CREATIONDATE & STATEID中,这些列仅在T3中可用。 T1和'T3'都共享可用于加入它们的DOCID ...

仍有一些问题,非常感谢帮助。

获取错误

Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"

2 个答案:

答案 0 :(得分:2)

WHERE子句位置的问题,必须在连接表列表之后:

INSERT INTO T2 
 SELECT T1.*,  T3.DATETIME, T3.STATEID
 FROM T1 
 LEFT OUTER JOIN T3
   ON (T1.DOCID =  T3.DOCID)
WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE;

答案 1 :(得分:2)

您的语法不正确,在连接之后出现:

 INSERT INTO T2 
 SELECT T1.*,  T3.DATETIME, T3.STATEID
 FROM T1 
LEFT OUTER JOIN T3
ON (T1.DOCID =  T3.DOCID)
WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE