SQL错误 - 语法错误

时间:2010-12-13 16:37:58

标签: sql sql-server tsql

我有以下T-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'.
看着它,我不确定我错过了什么。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:5)

你需要两个FROM我知道它很奇怪

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'

如果你看一下online help这里的语法

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

第一个来自

  

FROM是一个可选的关键字   在DELETE关键字和。之间使用   目标table_or_view_name,或   rowset_function_limited。

第二个来自

  

FROM指定一个   附加的FROM子句。这个   Transact-SQL扩展到DELETE   允许指定数据    并删除   来自表中的相应行   第一个FROM子句。

     

此扩展名,指定连接,可以   用来代替子中的子查询   用于标识行的WHERE子句   除去。

     

有关更多信息,请参阅FROM   (处理SQL)。

正如Tony指出的那样,你可以选择删除第一个FROM,使其更具可读性

DELETE 
   Config 
FROM 
   config ....

答案 1 :(得分:0)

我添加了一些表别名来清理查询,但关键是你需要两个FROM:一个用于DELETE,一个用于查询本身。

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'

答案 2 :(得分:0)

或省略第一个FROM

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'