2010-12-13 33 views
4

je la requête T-SQL suivante pour supprimer un enregistrement d'une série de tables:erreur SQL - mauvaise syntaxe

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' 

Mais il garde en jetant l'erreur:

Msg 156, Level 15, State 1, Line 1 
Incorrect syntax near the keyword 'INNER'. 

En regardant cela , Je ne suis pas sûr de ce qui me manque. Toute aide est appréciée.

Répondre

5

Vous avez besoin de deux Froms Je sais que wierd

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' 

Si vous regardez le online help est ici la syntaxe

[ 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 
} 

Le premier de est

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

Le deuxième De est

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).

Comme Tony indique que vous pouvez éventuellement laisser tomber le premier de si il est un peu plus lisible

DELETE 
    Config 
FROM 
    config .... 
+1

Vous pouvez omettre le premier à donner s'il est plus facile sur l'oeil :) SUPPRIMER config DE config INNER JOIN ... – Tony

+0

@Tony Merci I » J'ai mis à jour ma réponse –

0

J'ai ajouté quelques alias de table pour nettoyer la requête un peu, mais la clé est que vous avez besoin de deux Froms : un pour le DELETE et un pour la requête elle-même.

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

ou en omettant le premier à donner

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'