How to delete multiple rows in SQL Server

I have a used car sales database and four relationship tables. The names of the same columns are related.

Tables::
RecordRecID ...
FeaturesToken Name Model
Marks: FeatureID Featurename
Carfeature: MarkID Markname : CarfeatureID RecID Function Identifier

Now I want to remove the mark from Marks in C #. When I uncheck, SQL should delete all records that have this character.

I used the following query:

DELETE from Carfeature 
 where RecID = (select RecID 
 from Record 
 where Mark = (select markname 
                 from Marks 
                where MarkID=@MarkID))";


    string sorgudelmarkfromrecord = "DELETE from Record where Mark=
(select Markname from Marks where MarkID=@MarkID)";

    string sorgudelmark = "DELETE from Marks where MarkID=@MarkID";

When I run this, I get an error message like this:

Subqueryvalue returned is greater than 1 .

This is unacceptable if Subqueryfollowed =, !=, <, <= , >, >=or when Subqueryused as an expression.

The statement is complete.

, ?

+3
4

, CarFeatures MarkID:

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

- , SQL.

+11

where RecID=(...) where RecID in (...)

+2

ON DELETE CASCADE by foreign key?

+1
source

use this

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

Note: markid must be the primary key in both tables.

+1
source

Source: https://habr.com/ru/post/1720410/


All Articles