SQL Server:正确的DELETE语法

时间:2015-06-19 09:14:06

标签: sql-server

我有这个:

SELECT * 
FROM tblPricing pricing 
LEFT OUTER JOIN PUB_CAR.dbo.CAPVehicles vehicle ON pricing.capid = vehicle.CVehicle_ID
WHERE 
    pricing.[type] = 'car' 
    AND pricing.source = 'ARVAL' 
    AND vehicle.cvehicle_mantext = 'BMW' 
    AND vehicle.cvehicle_modtext = '5 SERIES DIESEL SALOON'  
    AND CVehicle_ManText IS NOT NULL

工作正常,我得到了正确的结果。

但是,如何删除相同的结果集。我试过了

DELETE FROM tblPricing pricing 
LEFT OUTER JOIN PUB_CAR.dbo.CAPVehicles vehicle ON pricing.capid = vehicle.CVehicle_ID 
WHERE 
     pricing.[type] = 'car' 
     AND pricing.source = 'ARVAL' 
     AND vehicle.cvehicle_mantext = 'BMW' 
     AND vehicle.cvehicle_modtext = '5 SERIES DIESEL SALOON'  
     AND CVehicle_ManText IS NOT NULL

但是,说“定价”附近的语法不正确

由于

4 个答案:

答案 0 :(得分:1)

您必须先指定表别名。所以不要这样:

DELETE FROM tblPricing pricing ....

这样:

DELETE pricing 
FROM   tblPricing pricing 
LEFT OUTER JOIN PUB_CAR.dbo.CAPVehicles vehicle 
ON              pricing.capid = vehicle.CVehicle_ID 
WHERE           pricing.[type] = 'car' 
AND             pricing.source = 'ARVAL' 
AND             vehicle.cvehicle_mantext = 'BMW' 
AND             vehicle.cvehicle_modtext = '5 SERIES DIESEL SALOON' 
AND             CVehicle_ManText IS NOT NULL

答案 1 :(得分:1)

您在DELETE关键字后缺少表别名。

DELETE pricing FROM tblPricing AS pricing 
LEFT OUTER JOIN PUB_CAR.dbo.CAPVehicles vehicle ON pricing.capid = vehicle.CVehicle_ID 
WHERE pricing.[type] = 'car' AND 
      pricing.source = 'ARVAL' AND 
      vehicle.cvehicle_mantext = 'BMW' AND 
      vehicle.cvehicle_modtext = '5 SERIES DIESEL SALOON' AND 
      CVehicle_ManText IS NOT NULL

答案 2 :(得分:0)

public class ClientIndexData
{
    public IEnumerable<Client> Clients { get; set; }
    public IEnumerable<Benefit> Benefits { get; set; }
    public IEnumerable<Ticket> Tickets { get; set; }
    public IEnumerable<Paid> Paids { get; set; }
    public IEnumerable<Schedule> Schedules { get; set; }

}

答案 3 :(得分:0)

DELETE语句可以选择包含两个 FROM子句。第一个用于标识要删除的表。第二个用于引入FROM子句,该子句看起来与SELECT语句中的子句相同。尽管他们使用相同的关键字,但他们相同。

如果您只有DELETE FROM xxx,那么它就是第一个变体,并且不能用于开始介绍JOIN子句。所以你想要的完整形式是:

DELETE FROM tblPricing --<-- FROM
FROM tblPricing pricing --<-- FROM again. Different feature
LEFT OUTER JOIN PUB_CAR.dbo.CAPVehicles vehicle 
ON pricing.capid = vehicle.CVehicle_ID 
WHERE pricing.[type] = 'car' 
AND pricing.source = 'ARVAL' AND vehicle.cvehicle_mantext = 'BMW'
AND vehicle.cvehicle_modtext = '5 SERIES DIESEL SALOON'
AND CVehicle_ManText IS NOT NULL

其他答案已删除了第一个FROM关键字,因为它是可选的(但是,第二种类型的FROM子句必须在之后表中删除已被确定)