如何删除 SQL 服务器中的空列数据

How to delete null column data in SQL Server

在我的存储过程中,我有一个临时 table,其中有如下数据:

我得到一些员工的 2 行,其中一个有 emp 名称,其他为空。

现在我需要删除员工的行,如果它有重复的空 emp 名称行。

如果它是空的单行,我们不需要删除。我只需要删除突出显示的那些。

请帮忙看看这里的where条件是什么

您可以使用 is null 运算符检查名称,并使用另一个 exists 条件来检查具有非空名称的相应 ID:

DELETE a
FROM   mytable a
WHERE  emp_name IS NULL AND
       EXISTS (SELECT *
               FROM   mytable b
               WHERE  b.emp_name IS NOT NULL AND a.emp_id = b.emp_id)

在SQL服务器中,DELETEFROM语法有点不同。尝试其中之一:

delete @Results
from @Results r
where
    r.AffectedEndUserName is null
    and exists  (
                    select
                        1
                    from @Results rr
                    where
                        rr.AffectedEndUserName is not null and rr.EmpId = r.EmpId
                );

delete r
from @Results r
    inner join @Results rr on rr.EmpId = r.EmpId and rr.AffectedEndUserName is not null
where
    r.AffectedEndUserName is null;

我更喜欢后者;干净多了。