我试图通过加入多个表来删除行,如你所见,但它会引发一个错误:
DELETE FROM Reg INNER JOIN
RegDoc ON Reg.RegId = RegDoc.RegId INNER JOIN
Doc ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
收到错误:
第15行,第1行,第1行,第1行 关键字“INNER”附近的语法不正确。
答案 0 :(得分:4)
正确的语法是
DELETE [ FROM ] { <object> }
[ FROM <table_source>]
[ WHERE { <search_condition> } ]
应用于您的示例,这将成为
DELETE FROM Reg
FROM Reg
INNER JOIN RegDoc ON Reg.RegId = RegDoc.RegId
INNER JOIN Doc ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
请注意两个对Reg
表
这可以通过别名进一步缩短(虽然在这种情况下几乎不需要)
DELETE FROM r
FROM Reg r
INNER JOIN RegDoc ON r.RegId = RegDoc.RegId
INNER JOIN Doc ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
MSDN示例
DELETE FROM Sales.SalesPersonQuotaHistory
FROM Sales.SalesPersonQuotaHistory AS spqh
INNER JOIN Sales.SalesPerson AS sp
ON spqh.BusinessEntityID = sp.BusinessEntityID
WHERE sp.SalesYTD > 2500000.00;
答案 1 :(得分:1)
使用此
DELETE
FROM Reg
FROM Reg
INNER JOIN
RegDoc ON Reg.RegId = RegDoc.RegId INNER JOIN
Doc ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
或者更清楚,恕我直言:
DELETE Reg
FROM Reg
INNER JOIN RegDoc
ON Reg.RegId = RegDoc.RegId
INNER JOIN Doc
ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
答案 2 :(得分:1)
你可以这样做:
DELETE Reg
FROM Reg
INNER JOIN RegDoc ON Reg.RegId = RegDoc.RegId
INNER JOIN Doc ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%'
答案 3 :(得分:1)
您可以使用别名,但如果使用与表名不同的别名则更清楚:
DELETE r
FROM Reg as r
INNER JOIN RegDoc rd ON r.RegId = rd.RegId
INNER JOIN Doc d ON rd.DocId = d.DocId
您实际上也可以delete rd
和delete d
。
答案 4 :(得分:0)
与其他答案(这些都很好)有点不同的方法
DELETE FROM Reg
WHERE Reg.RegID IN (SELECT RegDoc.RegId
FROM RegDoc
INNER JOIN Doc
ON RegDoc.DocId = Doc.DocId
WHERE Doc.Name LIKE N'%Title%')