关于c#:关键字“JOIN”附近的语法不正确。

Incorrect syntax near the keyword 'JOIN'. using asp.net

本问题已经有最佳答案,请猛点这里访问。

请帮助我找出这个错误的原因。

Incorrect syntax near the keyword 'JOIN'

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Label name = (Label)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("lblName");

string queryGuitarItems ="DELETE FROM stringInstrumentItem JOIN brand ON stringInstrumentItem.brandId = brand.brandId WHERE stringInstrumentItem.brandId IN(SELECT brand.brandId FROM brand WHERE name = @brand)";

using (SqlConnection connectionGuitarItems = new SqlConnection(ConfigurationManager.ConnectionStrings["musicStoreConnection"].ToString()))
{
    using (SqlCommand commandGuitarItems = new SqlCommand(queryGuitarItems, connectionGuitarItems))
    {
        connectionGuitarItems.Open();
        commandGuitarItems.Connection = connectionGuitarItems;
        commandGuitarItems.Parameters.Add(new SqlParameter("@brand", name.Text));
        commandGuitarItems.ExecuteNonQuery();

        connectionGuitarItems.Close();
        commandGuitarItems.Parameters.Clear();
    }
}


这里是执行delete from join语法的权利

1
2
3
4
5
DELETE S  --missing alias name
FROM stringInstrumentItem S
JOIN brand B
  ON S.brandId = B.brandId
WHERE B.name = @brand

注:您不需要sub-query表,因为Brand表已经联接。


更改此:

1
DELETE FROM stringInstrumentItem JOIN brand ON stringInstrumentItem.brandId = brand.brandId WHERE stringInstrumentItem.brandId IN(SELECT brand.brandId FROM brand WHERE name = @brand)

为此:

1
2
DELETE FROM stringInstrumentItem
FROM stringInstrumentItem t1 JOIN brand t2 ON t1brandId = t2.brandId WHERE t1.brandId IN(SELECT t3.brandId FROM brand t3 WHERE name = @brand)

希望我能帮助你:)


您必须给出要删除alias的表。我刚把桌子命名为"sii"。此外,您不需要WHERE子句。您也可以在联接条件中限制删除。

1
2
3
4
5
   DELETE sII
     FROM stringInstrumentItem  sII
INNER JOIN brand b
        ON sII.brandId = b.brandId
       AND b.name = @brand