仅在oracle sql

时间:2018-01-17 15:16:35

标签: sql oracle stored-procedures duplicates sql-insert

我在下面查询我正在使用oracle存储过程。查询运行正常但我想在此查询中添加条件,以便如果行值与name,e_id,c_date,p_check的组合已存在,则不要插入此值。

INSERT INTO M_LOG(NAME, E_ID, C_DATE, STATUS, P_CHECK)
Select MAX(ML.NAME), ML.E_ID, C_DATE, 1, 'M Step_1' from F_LOG ML,DIR_LOG MD
WHERE ML.NAME != MD.NAME and ML.E_ID != MD.E_ID and MD.C_DATE = LAST_DAY(to_date(sysdate,'YYYYMMDD'))
GROUP BY ML.E_ID,C_DATE;

2 个答案:

答案 0 :(得分:1)

您可以将MERGEWHEN NOT MATCHED子句一起使用:

MERGE INTO M_LOG dst
USING (
  Select MAX(ML.NAME) AS name,
         ML.E_ID,
         C_DATE,
         1 AS status,
         'M Step_1 As p_check'
  from   F_LOG ML
         INNER JOIN DIR_LOG MD
         ON (   ML.NAME != MD.NAME
            AND ML.E_ID != MD.E_ID )
  WHERE  MD.C_DATE = LAST_DAY( TRUNC( sysdate ) )
  GROUP BY ML.E_ID,C_DATE
) src
ON (   src.name    = dst.name
   AND src.e_id    = dst.e_id
   AND src.c_date  = dst.c_date
   AND src.p_check = dst.p_check )
WHEN NOT MATCHED THEN
  INSERT (NAME, E_ID, C_DATE, STATUS, P_CHECK)
  VALUES ( src.name, src.e_id, src.c_date, src.status, src.p_check );

答案 1 :(得分:1)

您可以使用 INSERT INTO m_log ( name, e_id, c_date, status, p_check ) SELECT MAX(ml.name) name, ml.e_id, c_date, 1 status, 'M Step_1' p_check FROM f_log ml WHERE NOT EXISTS ( SELECT 1 FROM dir_log md WHERE ( ml.name = md.name OR ml.e_id = md.e_id ) AND md.c_date = LAST_DAY(TRUNC(SYSDATE)) ) GROUP BY ml.e_id, c_date;

var table = $('table').DataTable({
    buttons: [
        extend: 'print',
        title: function(){
            var printTitle = 'New Title';
            return printTitle
        }
    ]
});