如何在SQL Server中删除多行

问题描述:

我有一个二手销售汽车数据库和四个关系表。相同的列名有关系。如何在SQL Server中删除多行

表是:
Record:RecID Markname型号...
Features:FeatureID和Featurename
Marks:MarkID Markname
Carfeature:CarfeatureID RecID FeatureID和

现在,我想删除的标记的标记在C#中。当我删除标记时,SQL必须删除具有此标记的所有记录。

我用这样的查询:

DELETE from Carfeature 
where RecID = (select RecID 
from Record 
where Mark = (select markname 
       from Marks 
       where [email protected]))"; 


    string sorgudelmarkfromrecord = "DELETE from Record where Mark= 
(select Markname from Marks where [email protected])"; 

    string sorgudelmark = "DELETE from Marks where [email protected]"; 

当我运行它,我得到一个错误信息是这样的:

Subquery返回超过1个值。

subquery跟在=, !=, <, <= , >, >=或当subquery用作表达式时,这是不允许的。

该声明已终止。

那么,我怎样才能从表中删除多个数据?

如果我理解你的权利,你可以使用这个查询删除所有CarFeatures的cerain MarkID:

DELETE FROM CarFeature 
WHERE RecID IN (
    select RecID 
    from Record 
    where Markname IN (
     select Markname 
     from Marks 
     where MarkID = @MarkID 
    ) 
) 

的记录和标记去除是这个查询的简化版本,我将离开确切的SQL由你决定。

+0

+1。 :) – Moose 2009-10-18 22:40:46

+0

谢谢你们两位朋友。我试过了,它工作。我知道这很简单,但我是一个新手程序员。我应该更努力地处理SQL查询:)再次感谢 – 2009-10-18 22:43:06

而不是使用外键where RecID=(...)使用where RecID in (...)

ON DELETE CASCADE?

使用本

DELETE from Record r 
where r.Mark in 
    (select m.Mark from Marks where r.MarkID=m.MarkID); 

注: markid必须花费更多的时间来清楚地格式化你的答案比我有两个表中的主键..