我要删除select语句中存在特定列的行,例如:
delete from [ProjectCustomer] pcc
where ProjectKey in (
select p.ProjectKey
FROM [Project] AS [p] inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
inner join Customer as c on pc.CustomerKey = c.CustomerKey
where d.Folio = 3014)
但是在delete语句的where子句中我得到
where子句附近的语法不正确
我在做什么错?问候
答案 0 :(得分:1)
具有表别名的DELETE语句具有不同的语法,因此您可以删除别名而不使用它,或者更改删除语句,例如@Martin注释。
delete pcc from [ProjectCustomer] pcc
我愿意使用INNER JOIN
而不是IN
来修改查询
delete pcc from [ProjectCustomer] AS pcc
inner join [Project] AS [p] ON [p].ProjectKey = [pcc].ProjectKey
inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
inner join Customer as c on pc.CustomerKey = c.CustomerKey
where d.Folio = 3014)
答案 1 :(得分:0)
我认为错误消息是
“ pcc”附近的语法不正确
代替
where子句附近的语法不正确
您将收到第一条错误消息,因为您使用的别名错误,因此当您不加入目标表时,您可能会想要
delete from [ProjectCustomer]
where ProjectKey in (
select p.ProjectKey
FROM [Project] AS [p] inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
inner join Customer as c on pc.CustomerKey = c.CustomerKey
where d.Folio = 3014);
,或者即使您仍想使用别名
delete pcc from [ProjectCustomer] pcc
where pcc.ProjectKey in ...
答案 2 :(得分:0)
您可以稍微简化查询。我认为您不需要子查询,可以在第一个FROM子句中指定要删除的表(不带别名),然后在第二个表中联接表(随便使用别名):
DELETE FROM ProjectCustomer
FROM ProjectCustomer pc
INNER JOIN ProjectDesign pd ON pc.ProjectKey = pd.ProjectKey
INNER JOIN Design d ON pd.DesignKey = d.DesignKey
WHERE d.Folio = 3014;
Customers
和Products
表不是必需的,只要您设置了参照完整性并且pc.CustomerKey
不可为空。