MERGE语句中的DELETE子句

时间:2017-01-10 05:41:15

标签: java oracle oracle10g

我有一张表如下。

Name         Null         Type          
-----------  --------     -------------- 
EMPLOYEEID   **NOT NULL** NUMBER         
NAME         **NOT NULL** VARCHAR2(1000) 
AGE          *NOT NULL*   NUMBER         
SALARY                    NUMBER        
DELETEDFLAG               NUMBER(1) 

我编写了MERGE语句来插入,更新,删除记录,如下所示。

MERGE INTO EMPLOYEE tgt USING  (
SELECT( SELECT EMPLOYEE.rowId as rid  from EMPLOYEE WHERE employeeid = 70) as rid from dual ) src ON (tgt.rowid = src.rid)
WHEN MATCHED THEN UPDATE SET  name = 'PSS', age = 25, salary = 2589
WHERE employeeid = 70
DELETE WHERE DELETEDFLAG = 1
WHEN NOT MATCHED THEN
INSERT (employeeId, name, age, salary, deletedFlag) VALUES( 70, 'TSS', 28, 15000, 0);

删除记录时我遇到了问题。当我收到name,name和salary的null值并且deleteFlag为零时,我想删除记录。我只知道employeeID。 MERGE查询如下。

MERGE INTO EMPLOYEE tgt USING  (
SELECT( SELECT EMPLOYEE.rowId as rid  from EMPLOYEE WHERE employeeid = 70) as rid from dual ) src ON (tgt.rowid = src.rid)
WHEN MATCHED THEN UPDATE SET  name = null, age = null, salary = null
WHERE employeeid = 70
DELETE WHERE DELETEDFLAG = 0
WHEN NOT MATCHED THEN
INSERT (employeeId, name, age, salary, deletedFlag) VALUES( 70, null, null, null, 0);

我遇到的问题是在这种情况下它会尝试更新记录。该名称不是nullable列,但它尝试使用null更新。所以得到了SQL错误。

  

无法将(%s)更新为NULL

如果我传递not null值,它按预期工作。

任何人都可以建议我如何处理这种情况?

1 个答案:

答案 0 :(得分:0)

将更新部分中的谓词更改为此

WHERE employeeid = 70 and DELETEDFLAG <> 0

如果DELETEDFLAG可以为null,我假设您知道如何更改谓词