Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何根据日期更新获取员工的状态_Sql_Oracle_Plsql - Fatal编程技术网

Sql 如何根据日期更新获取员工的状态

Sql 如何根据日期更新获取员工的状态,sql,oracle,plsql,Sql,Oracle,Plsql,我有一个表为emp,记录为 EMPID SDATE SAL DESIGNATION ======================================= 1001 21-SEP-17 55000 Technology Analyst 1002 22-SEP-19 52000 Technology Analyst 1003 15-SEP-17 65000 Technology Lead 1004 21-SEP-15

我有一个表为emp,记录为

EMPID   SDATE       SAL     DESIGNATION
=======================================
1001    21-SEP-17   55000   Technology Analyst
1002    22-SEP-19   52000   Technology Analyst
1003    15-SEP-17   65000   Technology Lead
1004    21-SEP-15   72000   Technology Lead
1005    11-MAR-20   55000   SSE
1006    22-JAN-20   55000   SSE
我们需要使用下一个日期(sysdate或1周后)更新所有员工的startDate,在PLSQL中按批次(按指定)更新,同时牢记性能。 更新某些任意表(emp_log)中更新状态如下的-->日志记录时,如果特定员工的更新成功,则状态为Success&如果某些员工的更新失败,则状态为failed

empid   Update_Status
=====================
1001     Success
1002     Failed
1003     Success
.....

什么样的PL/SQL block语句才能使用empid获得这种类型的状态?

如果这是只更新日期的实际用例,我看不到会发生任何错误,除非您对它有任何此类约束或任何触发器验证

然而,如果这只是一个例子,并且您有更大的逻辑,那么我将在下面的代码中提供我这边的注释,这可能会给您一个继续的视野

因为您谈到性能,所以我会使用类似于
bulk collect
forall
的集合,并使用
limit
使用
保存异常捕获DML错误

declare
  -- cursor to fetch the data which we want to update
  -- additional wherr clauses can be added as per need
  cursor cur_emp 
  is
  select *
    from emp;
  -- rowtype variablr to hold the record
  -- we can also use emp%rowtype
  type emp_tab is table of cur_emp%rowtype;
  -- local variable for the table type above
  l_emp_data emp_tab;
  -- user defined exception to catch bulk exception error
  dml_errors exception;
  -- actual ORA error number map to dml_errors
  pragma exception_init(dml_errors, -24381);
  -- othet local variables for convenience purpose
  l_errors number;
  l_errno  number;
  l_msg    varchar2(4000);
  l_idx    number;
begin
  open cur_emp;
  loop
    -- using limit as 100 which is recommended when we use bulk collect
    fetch cur_emp bulk collect 
    into l_emp_data limit 100;
    begin
      -- updating 100 records at one shot 
      -- saving exception per record in case of failure
      forall i in 1 .. l_emp_data.count 
      save exceptions
      update emp
         set sdate = sysdate
       where empid = l_emp_data(i).empid;
      -- we can insert all records as successful intially
      -- which further in the exception section will be updated in case of any dml error
      forall i in 1 .. l_emp_data.count
      insert into emp_log
      values(l_emp_data(i).empid,'Success',null);
    exception
      -- handling the user defined exception 
      -- and updating erroneous records
      when dml_errors then
        l_errors := sql%bulk_exceptions.count;
        for i in 1 .. l_errors
        loop
          l_errno := sql%bulk_exceptions(i).error_code;
          l_msg   := sqlerrm(-l_errno);
          l_idx   := sql%bulk_exceptions(i).error_index;
          -- i added additional error message as well
          update emp_log
             set status  = 'Failed'
                ,err_msg = l_errno||'-'||l_msg
           where empid = l_emp_data(l_idx).empid;
        end loop;
    end;
    exit when cur_emp%notfound;
  end loop;
  close cur_emp;
end;
/
然而,我觉得如果你只想批量更新每个
指定的日期,那么一个简单的更新就足够了,因为每个员工都是唯一标识的,即使你每个指定有10000条记录,我也看不到任何绩效问题。(如果你这样认为,你可以尝试一下)

最后,我还想介绍一下如何利用从10g开始提供的
dml错误日志记录
。您将拥有一个系统生成的表,该表将记录DML操作期间的所有错误记录,您可以根据需要创建一个视图来查看成功或失败


我还提供了上述代码的执行结果,供您参考。我没有模拟我留给您的错误部分。

您想要什么结果?不清楚您要更新什么。@GordonLinoff我更新了我的问题。请你再看一遍这个问题好吗?记住绩效-如果你想更新所有员工,只需更新员工集sdate=@MarcinWroblewski感谢Marcin,但另一个表中的状态更新呢。此语句应在PLSQL块中运行。了解表中的更新状态。这不是代码编写服务。请张贴您尝试过的内容以及您认为问题所在的位置。