我想在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'
我想在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,
Employee e
WHERE w.EmployeeRun = e.EmployeeNo
AND w.Company = '1'
AND w.Date = '2013-05-06'
其他回答
只需在DELETE和FROM之间添加要删除记录的表的名称,因为我们必须指定要删除的表。还要删除ORDERBY子句,因为删除记录时没有要排序的内容。
因此,您的最终查询应该如下所示:
DELETE WorkRecord2
FROM WorkRecord2
INNER JOIN Employee
ON EmployeeRun=EmployeeNo
WHERE Company = '1'
AND Date = '2013-05-06';
这可能对你有帮助-
DELETE FROM dbo.WorkRecord2
WHERE EmployeeRun IN (
SELECT e.EmployeeNo
FROM dbo.Employee e
WHERE ...
)
或者试试这个-
DELETE FROM dbo.WorkRecord2
WHERE EXISTS(
SELECT 1
FROM dbo.Employee e
WHERE EmployeeRun = e.EmployeeNo
AND ....
)
另一种方法是使用CTE:
;WITH cte
AS (SELECT *
FROM workrecord2 w
WHERE EXISTS (SELECT 1
FROM employee e
WHERE employeerun = employeeno
AND company = '1'
AND date = '2013-05-06'))
DELETE FROM cte
注意:当您想要删除时,我们不能在CTE中使用JOIN。
在SQL Server Management Studio中,我可以轻松创建SELECT查询:
SELECT Contact.Naam_Contactpersoon, Bedrijf.BedrijfsNaam, Bedrijf.Adres, Bedrijf.Postcode
FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
我可以执行它,我的所有联系人都会显示出来。
现在将SELECT更改为DELETE:
DELETE Contact
FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
您在SELECT语句中看到的所有记录都将被删除。
您甚至可以使用相同的过程创建更困难的内部联接,例如:
DELETE FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
INNER JOIN LoginBedrijf ON Bedrijf.IDLoginBedrijf = LoginBedrijf.IDLoginBedrijf
您需要指定要从中删除的表。以下是具有别名的版本:
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'