使用不存在的地方删除

时间:2015-01-23 13:59:55

标签: sql-server sql-delete

我正在尝试删除主文件中存在的记录,其中包含三个字段mn_no,sb_no和dp_no的帐号不在事务表中。由于帐号可能在一个或多个交易表中,因此我需要测试其在所有交易表中的存在。当我运行以下查询时,MS-SQL在第1行附近返回不正确的语法错误。

DELETE
FROM SYACTFIL_SQL a
WHERE NOT EXISTS (
        SELECT b.mn_no
            ,b.sb_no
            ,b.dp_no
        FROM gltrxhst_sql b
        WHERE a.mn_no = b.mn_no
            AND a.sb_no = b.sb_no
            AND a.dp_no = b.dp_no
        )
    AND NOT EXISTS (
        SELECT b.mn_no
            ,b.sb_no
            ,b.dp_no
        FROM gltrxfil_sql b
        WHERE a.mn_no = b.mn_no
            AND a.sb_no = b.sb_no
            AND a.dp_no = b.dp_no
        )
    AND a.mn_no <> '00000000'

1 个答案:

答案 0 :(得分:6)

这里的语法不正确。执行以下操作:

DELETE  a
FROM    SYACTFIL_SQL a
WHERE   NOT EXISTS ( SELECT b.mn_no ,
                            b.sb_no ,
                            b.dp_no
                     FROM   gltrxhst_sql b
                     WHERE  a.mn_no = b.mn_no
                            AND a.sb_no = b.sb_no
                            AND a.dp_no = b.dp_no )
        AND NOT EXISTS ( SELECT b.mn_no ,
                                b.sb_no ,
                                b.dp_no
                         FROM   gltrxfil_sql b
                         WHERE  a.mn_no = b.mn_no
                                AND a.sb_no = b.sb_no
                                AND a.dp_no = b.dp_no )
        AND a.mn_no <> '00000000'

或者

DELETE  FROM SYACTFIL_SQL
WHERE   NOT EXISTS ( SELECT b.mn_no ,
                            b.sb_no ,
                            b.dp_no
                     FROM   gltrxhst_sql b
                     WHERE  SYACTFIL_SQL.mn_no = b.mn_no
                            AND SYACTFIL_SQL.sb_no = b.sb_no
                            AND SYACTFIL_SQL.dp_no = b.dp_no )
        AND NOT EXISTS ( SELECT b.mn_no ,
                                b.sb_no ,
                                b.dp_no
                         FROM   gltrxfil_sql b
                         WHERE  SYACTFIL_SQL.mn_no = b.mn_no
                                AND SYACTFIL_SQL.sb_no = b.sb_no
                                AND SYACTFIL_SQL.dp_no = b.dp_no )
        AND mn_no <> '00000000'