INSERT INTO无法使用我的选择

时间:2016-09-14 18:38:16

标签: sql oracle

我需要将一些数据从一个表复制到另一个表:

有一个表具有正确的日期(AUDITLOG),列TIME,但我需要将它放在USER表中,USERS_DATE列并与正确的用户相关联...

SELECT本身返回正确的数据,我在使用SELECT INTO语句时遇到问题。我的黑客代码如下:

INSERT INTO users (USERS_DATE) WHERE USERID=U.USERID
(SELECT U.USERID,
       javaTimeStampToDate (L.TIME)
          AS "Last Login"
  FROM COMPANY C,
       USERS U,
       AUDITMAP M,
       AUDITLOG L
 WHERE     C.COMPANYID = U.COMPANYID
       AND U.USERID = M.ROWID
       AND M.AUDITID = L.AUDITID
       AND C.APPLICATION = 'A'
       AND L.NOTES LIKE '%went inactive%'
       AND U.STATUS = 0);

1 个答案:

答案 0 :(得分:1)

尝试使用以下sql server查询。 我认为你需要一个更新查询。同时使用JOIN条件更改逗号。

  UPDATE U
    SET U.USERS_DATE=L.TIME
  FROM COMPANY C 
       JOIN USERS U ON C.COMPANYID = U.COMPANYID
       JOIN AUDITMAP M ON U.USERID = M.ROWID
       JOIN AUDITLOG L ON M.AUDITID = L.AUDITID
 WHERE   C.APPLICATION = 'A'
       AND L.NOTES LIKE '%went inactive%'
       AND U.STATUS = 0

使用以下查询for oracle,

MERGE INTO USERS U
USING 
(
  SELECT M.ROWID,L.Time
  FROM AUDITMAP M 
       JOIN AUDITLOG L ON M.AUDITID = L.AUDITID
  WHERE L.NOTES LIKE '%went inactive%'
) Au ON (U.USERID = Au.ROWID) 
WHEN MATCHED THEN UPDATE 
    SET U.USERS_DATE = Au.TIME
WHERE U.STATUS = 0 AND  EXISTS (select 1
                            from COMPANY c
                            where .COMPANYID = U.COMPANYID AND C.APPLICATION = 'A')