2010-12-13 25 views
4

私は一連のテーブルからレコードを削除するには、次のT-SQLクエリを持っている:SQLエラー - 不正な構文

DELETE FROM config INNER JOIN config_profile ON config.config_id = config_profile.config_id 
INNER JOIN config_page ON config_profile.config_profile_id = config_page.config_profile_id 
INNER JOIN config_field ON config_page.config_page_id = config_field.config_page_id 
INNER JOIN config_constraint ON config_field.config_field_id = config_constraint.config_field_id 
INNER JOIN config_constraint_type ON config_constraint.config_constraint_type_id = config_constraint_type.config_constraint_type_id 
WHERE config.config_name = 'ConfigName' AND config_profile.profile_name = 'ProfileName' 

しかし、それはエラーを投げ続ける:

それを見て
Msg 156, Level 15, State 1, Line 1 
Incorrect syntax near the keyword 'INNER'. 

私は何が欠けているのか分かりません。どんな助けもありがとうございます。次の2つの必要

答えて

5

は、あなたがonline helpを見れば、私は

FROM Is an optional keyword that can be used between the DELETE keyword and the target table_or_view_name, or rowset_function_limited.

秒であるからであるから、ここでの構文

[ WITH <common_table_expression> [ ,...n ] ] 
DELETE 
    [ TOP (expression) [ PERCENT ] ] 
    [ FROM ] 
    { <object> | rowset_function_limited 
     [ WITH (<table_hint_limited> [ ...n ]) ] 
    } 
    [ <OUTPUT Clause> ] 
    [ FROM <table_source> [ ,...n ] ] 
    [ WHERE { <search_condition> 
      | { [ CURRENT OF 
        { { [ GLOBAL ] cursor_name } 
         | cursor_variable_name 
        } 
       ] 
       } 
      } 
    ] 
    [ OPTION (<Query Hint> [ ,...n ]) ] 
[; ] 

<object> ::= 
{ 

    [ server_name.database_name.schema_name. 
     | database_name. [ schema_name ] . 
     | schema_name. 
    ] 
    table_or_view_name 
} 

が最初だその奇妙

DELETE 
FROM CONfig 
FROM 
config 
INNER JOIN config_profile ON config.config_id = config_profile.config_id 
INNER JOIN config_page ON config_profile.config_profile_id = config_page.config_profile_id 
INNER JOIN config_field ON config_page.config_page_id = config_field.config_page_id 
INNER JOIN config_constraint ON config_field.config_field_id = config_constraint.config_field_id 
INNER JOIN config_constraint_type ON config_constraint.config_constraint_type_id = config_constraint_type.config_constraint_type_id 
WHERE config.config_name = 'ConfigName' AND config_profile.profile_name = 'ProfileName' 

を知っているFROMs

FROM Specifies an additional FROM clause. This Transact-SQL extension to DELETE allows specifying data from and deleting the corresponding rows from the table in the first FROM clause.

This extension, specifying a join, can be used instead of a subquery in the WHERE clause to identify rows to be removed.

For more information, see FROM (Transact-SQL).

トニーが指摘するようにあなたは、必要に応じて、私は少しクエリをクリーンアップするためにいくつかの表の別名を追加しましたので、その少し読みやすく

DELETE 
    Config 
FROM 
    config .... 
+1

からの最初を省略:) JOINの設定INNER FROM設定を削除します...私は「 – Tony

+0

@Tony感謝私の答えを更新しました –

0

からの最初のドロップすることができますが、キーは次の2つがFROMs必要があるということです1つはDELETE用で、もう1つはクエリ自体用です。

DELETE FROM c 
    FROM config c 
     INNER JOIN config_profile cp 
      ON c.config_id = cp.config_id 
     INNER JOIN config_page cpg 
      ON cp.config_profile_id = cpg.config_profile_id 
     INNER JOIN config_field cf 
      ON cpg.config_page_id = cf.config_page_id 
     INNER JOIN config_constraint cc 
      ON cf.config_field_id = cc.config_field_id 
     INNER JOIN config_constraint_type cct 
      ON cc.config_constraint_type_id = cct.config_constraint_type_id 
    WHERE c.config_name = 'ConfigName' 
     AND cp.profile_name = 'ProfileName' 
0

それともそれは目にそれが容易になります場合は、からの最初を省略することができます

DELETE c 
    FROM config c 
     INNER JOIN config_profile cp 
      ON c.config_id = cp.config_id 
     INNER JOIN config_page cpg 
      ON cp.config_profile_id = cpg.config_profile_id 
     INNER JOIN config_field cf 
      ON cpg.config_page_id = cf.config_page_id 
     INNER JOIN config_constraint cc 
      ON cf.config_field_id = cc.config_field_id 
     INNER JOIN config_constraint_type cct 
      ON cc.config_constraint_type_id = cct.config_constraint_type_id 
    WHERE c.config_name = 'ConfigName' 
     AND cp.profile_name = 'ProfileName' 
関連する問題