协慌网

登录 贡献 社区

如何使用 INER JOIN 与 SQL Server 删除?

我想在SQL Server 2008 中使用INNER JOIN删除。

但我得到这个错误:

消息 156,第 15 级,状态 1,第 15 行
关键字 “INNER” 附近的语法不正确。

我的代码:

DELETE FROM WorkRecord2 
INNER JOIN Employee ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'

答案

您需要指定要删除的表,这是带别名的版本:

DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
  ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'

只需在DELETEFROM之间添加要删除记录的表的名称,因为我们必须指定要删除的表。同时删除ORDER BY子句,因为删除记录时无需订购。

所以你的最终查询应该是这样的:

DELETE WorkRecord2 
      FROM WorkRecord2 
INNER JOIN Employee 
        ON EmployeeRun=EmployeeNo
     WHERE Company = '1' 
       AND Date = '2013-05-06';

试试这个:

DELETE FROM WorkRecord2 
       FROM Employee 
Where EmployeeRun=EmployeeNo
      And Company = '1' 
      AND Date = '2013-05-06'