Can't resolve err in delete from syntax

  • Hi I am receiving a syntax error message for the query below...

    I can run this as a select statement ie select * from... but when I try to delete I'm getting a syntax error and can't figure out why.

    Thanks for any help in advance...

    Incorrect syntax near the keyword 'as'

    delete

    from

    table1 as source

    where

    exists

    (

    select id

    from table2 as dest

    where dest.id = source.id

    )

  • Assuming you are trying to delete from table1, try this:

    delete from

    dbo.table1

    from

    dbo.table1 as source

    where

    exists

    (

    select id

    from dbo.table2 as dest

    where dest.id = source.id

  • Thanks Lynn that worked...Not sure why a double 'from' was needed, is there a better way to write something like this?

  • delete

    dbo.table1

    from dbo.table1 as source

    Inner Join dbo.table2 as dest

    on dest.id = source.id

    Is an alternative method to write it.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thank you...

  • You're welcome.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Or even:

    DELETE dbo.Table1 WHERE id = ANY (SELECT D.id FROM dbo.Table2 D);

  • Or this:

    delete

    from

    table1

    where

    exists

    (

    select id

    from table2 as dest

    where dest.id = table1.id

    )

    Just lose the "AS."

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply