• grevesz (1/16/2013)


    1. Why is the X necessary after the DELETE key word? Wouldn't DELETE FROM demodelete x WHERE x.somenum = 1; work? If not, why?

    The DELETE statement consists of six parts:

    1) An optional CTE

    2) A required DELETE clause

    3) An optional OUTPUT clause

    4) An optional FROM clause

    5) An optional WHERE clause

    6) An optional OPTION clause

    The DELETE clause of the DELETE statement has three parts

    1) Required DELETE keyword

    2) Optional FROM keyword

    3) Required source

    a) Object

    b) Alias

    Note that the source is either an object or an alias, but not both.

    The simplest optional FROM clause contains three parts

    1) Required FROM keyword

    2) Required object name

    3) Optional alias

    So there are two different sources for the FROM keyword in a DELETE statement:

    1) The optional FROM in the required DELETE clause

    2) The required FROM in the optional FROM clause

    The DELETE clause can use an alias that is defined in the FROM clause, but it cannot define it's own alias, which is what you are trying to do. You're confusing the two different FROM keywords. In other words, your attempted query can be parsed in one of two ways (both invalid).

    DELETE FROM demodelete x WHERE x.somenum = 1;

    DELETE <missing required source>

    FROM demodelete x

    WHERE x.somenum = 1;

    DELETE FROM demodelete

    <missing required FROM> <missing required object> x

    WHERE x.somenum = 1;

    Perhaps including all of the optional FROM keywords will help it make sense.

    DELETE FROM x

    FROM demodelete x

    WHERE x.somenum = 1;

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA