相关更新条件

时间:2016-08-29 15:28:46

标签: sql oracle

我正在尝试使用第二个表中的更新值更新一个表的值。但是,由于我正在更新第一个表的MODIFIED_BY和MODIFIED_ON列,所以我只想更新有更新值的行:

UPDATE SAFETY_ADMIN.SAFETY_USERS tbl1
  SET (SUPERVISOR_ID,DEPT_ID,USER_ID,EMPLOYEE_TYPE,EMPLOYEE_NAME,EMAIL,MODIFIED_BY,MODIFIED_ON) = 
(
  SELECT SAFETY_ADMIN.FN_GETSUPERVISORID(tbl2.SUPID),CAST(tbl2.DEPTID AS NUMBER(19)),UPPER(SUBSTR(tbl2.EMAIL,1,INSTR(tbl2.EMAIL,'@')-1)),tbl2.EMPTYPE,tbl2.EMPNAME,tbl2.EMAIL,SYS_CONTEXT('USERENV', 'OS_USER'),CURRENT_TIMESTAMP
  FROM PS_LOAD.EMPLOYEEDATA tbl2 
  WHERE tbl1.EMPLOYEE_ID = CAST(tbl2.EMPID AS NUMBER(19)) AND 
  (
    tbl1.SUPERVISOR_ID <> SAFETY_ADMIN.FN_GETSUPERVISORID(tbl2.SUPID) OR
    tbl1.DEPT_ID <> CAST(tbl2.DEPTID AS NUMBER(19)) OR
    tbl1.USER_ID <> UPPER(SUBSTR(tbl2.EMAIL,1,INSTR(tbl2.EMAIL,'@')-1)) OR
    tbl1.EMPLOYEE_TYPE <> tbl2.EMPTYPE OR
    tbl1.EMPLOYEE_NAME <> tbl2.EMPNAME OR
    tbl1.EMAIL <> tbl2.EMAIL
  )
);

但是,我的查询抱怨使用NULL值更新DEPT_ID,即使没有空值。我觉得我把WHERE条件放在了错误的地方。我更像是一个SQL Server人。有人可以告诉我如何在Oracle中执行此操作吗?

2 个答案:

答案 0 :(得分:1)

我认为如果你添加var Entry = React.createClass({ // executes code on a button press. onButtonClick: function (event) { // (do stuff) }, // generates the entry list with a button and some info. render: function() { var entryList= this.props.data.map(function(entry) { // (convert timestamp into relative time, add some tags etc) return ( <p> entry.information </p> <a onClick={this.onButtonClick} className="btn right" id={entry.link}> go </a> ); },this); // returns the html of the component return ( <div className="entryList"> {entryList} </div> ); } }); 条款,它应该适合你......

where exists

答案 1 :(得分:1)

在Oracle更新中,需要从其他表中获取数据通常使用MERGE编写,这与使用共同相关子查询的普通UPDATE相比要短得多

MERGE INTO safety_admin.safety_users su
USING (
  SELECT safety_admin.fn_getsupervisorid(ed.supid) as supervisor_id,
         to_number(ed.deptid) as deptid,
         to_number(ed.empid) as empid,
         upper(substr(ed.email,1,instr(ed.email,'@')-1)) as user_id,
         ed.emptype,
         ed.empname,
         ed.email,
         ed.empid
  FROM ps_load.employeedata ed 
) t ON (su.employee_id = t.empid)
WHEN MATCHED THEN UPDATE
  SET (supervisor_id, dept_id, user_id, employee_type, employee_name, email, modified_by, modified_on) 
    = (t.supervisor_id, t.deptid, t.email, t.emptype, t.empname, sys_context('userenv', 'os_user'), current_timestamp)
WHERE ( su.supervisor_id <> t.supervisor_id OR
        su.dept_id <> t.deptid OR
        su.user_id <> user_id OR
        su.employee_type <> t.emptype OR
        su.employee_name <> t.empname OR
        su.email <> t.email);

我不完全确定set (a,b,c,) = (....)在MERGE的SET部分中有效。如果这不起作用,您需要单独指定每个列。

使用to_number()代替较长的cast()运算符,可以将varchar转换为数字。

也没有必要在Oracle中以大写形式编写所有内容,SQL不区分大小写。