我想在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
INNER JOIN Employee e
ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'
只需在要删除记录的位置之间添加表的名称,因为我们必须指定要删除的表DELETE
。FROM
还要删除该ORDER BY
子句,因为在删除记录时没有什么可订购的。
所以你的最终查询应该是这样的:
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 ....
)
尝试这个:
DELETE FROM WorkRecord2
FROM Employee
Where EmployeeRun=EmployeeNo
And Company = '1'
AND Date = '2013-05-06'
它应该是:
DELETE zpost
FROM zpost
INNER JOIN zcomment ON (zpost.zpostid = zcomment.zpostid)
WHERE zcomment.icomment = "first"
在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
This version should work:
DELETE WorkRecord2
FROM WorkRecord2
INNER JOIN Employee ON EmployeeRun=EmployeeNo
Where Company = '1' AND Date = '2013-05-06'
DELETE a FROM WorkRecord2 a
INNER JOIN Employee b
ON a.EmployeeRun = b.EmployeeNo
Where a.Company = '1'
AND a.Date = '2013-05-06'
试试这个查询:
DELETE WorkRecord2, Employee
FROM WorkRecord2
INNER JOIN Employee ON (tbl_name.EmployeeRun=tbl_name.EmployeeNo)
WHERE tbl_name.Company = '1'
AND tbl_name.Date = '2013-05-06';
另一种方法是使用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
注意:我们不能在您想要的时候使用JOIN
inside 。CTE
delete
你甚至可以做一个子查询。像下面的这段代码:
DELETE FROM users WHERE id IN(
SELECT user_id FROM Employee 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'
您没有为Company
and指定表Date
,并且您可能想要修复它。
标准 SQL 使用MERGE
:
MERGE WorkRecord2 T
USING Employee S
ON T.EmployeeRun = S.EmployeeNo
AND Company = '1'
AND Date = '2013-05-06'
WHEN MATCHED THEN DELETE;
Devart 的答案也是标准SQL,尽管不完整。它应该看起来更像这样:
DELETE
FROM WorkRecord2
WHERE EXISTS ( SELECT *
FROM Employee S
WHERE S.EmployeeNo = WorkRecord2.EmployeeRun
AND Company = '1'
AND Date = '2013-05-06' );
关于上述要注意的重要一点是,很明显删除是针对单个表的,如第二个示例中通过要求标量子查询强制执行的。
对我来说,各种专有语法答案更难阅读和理解。我想frans eilering 的回答最好地描述了这种心态,即编写代码的人不一定关心将阅读和维护代码的人。
这是一次从两个表中删除记录的简单查询。
DELETE table1.* ,
table2.*
FROM table1
INNER JOIN table2 ON table1.id= table2.id where table1.id ='given_id'
这是我的 SQL Server 版本
DECLARE @ProfileId table(Id bigint)
DELETE FROM AspNetUsers
OUTPUT deleted.ProfileId INTO @ProfileId
WHERE Email = @email
DELETE FROM UserProfiles
WHERE Id = (Select Id FROM @ProfileId)
使用事务块、表变量和JOIN删除多个表数据。
BEGIN TRANSACTION;
declare @deletedIds table ( id int );
DELETE w
output deleted.EmployeeRun into @deletedIds
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
DELETE e
FROM Employee as e
INNER JOIN @deletedIds as d
ON d.id = e.EmployeeNo;
COMMIT TRANSACTION;
请从网址https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=43330dda6f1b71b8ec4172a24d5b6921检查
用临时表和JOIN删除多个表数据。删除后删除临时表。
BEGIN TRANSACTION;
-- create temporary table
create table #deletedRecords (employeeId int);
-- INSERT INTO #deletedRecords
SELECT e.EmployeeNo
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
-- delete from WorkRecord2
DELETE w
FROM WorkRecord2 w
INNER JOIN #deletedRecords d
ON w.EmployeeRun = d.employeeId;
-- delete from Employee using exists
DELETE
FROM Employee
WHERE EXISTS (SELECT 1
FROM #deletedRecords d
WHERE d.employeeId = EmployeeNo);
-- drop temporary table
DROP TABLE #deletedRecords;
COMMIT TRANSACTION;
请从网址https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=d52c6c1ed91669d68fcc6bc91cb32d78检查
使用 SELECT INTO 创建临时表的替代方法
BEGIN TRANSACTION;
SELECT e.EmployeeNo employeeId
INTO #deletedRecords
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
-- delete from WorkRecord2
DELETE w
FROM WorkRecord2 w
INNER JOIN #deletedRecords d
ON w.EmployeeRun = d.employeeId;
-- delete from Employee using exists
DELETE
FROM Employee
WHERE EXISTS (SELECT 1
FROM #deletedRecords d
WHERE d.employeeId = EmployeeNo);
-- drop temporary table
DROP TABLE #deletedRecords;
COMMIT TRANSACTION;
请从网址https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=0f02f05616ce5b4dcc8fc67c6cf1e640检查
使用 JOIN 删除单个表数据
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06'
请从网址https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=84a60d1368556a8837281df36579334a检查
使用 CTE 删除单表数据
WITH cte AS (
SELECT w.EmployeeRun
FROM WorkRecord2 w
WHERE EXISTS (SELECT 1
FROM Employee
WHERE EmployeeNo = w.EmployeeRun)
AND w.Company = 1
AND w.date = '2013-05-06'
)
DELETE
FROM cte
请从网址https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=6590007b3c8c2ffad5563bd86606c5b1检查
在子表中创建外键时使用ON CASCADE DELETE 。如果删除父表数据,则相应的子表数据将被自动删除。