Duplicate delete using Common Table Expressions

  • CREATE TABLE Test

    (

    ContactID INT,

    SalesOrderID INT,

    OrderYear DATETIME

    )

    insert into Test(ContactID,SalesOrderID,OrderYear)

    select top 10 ContactID,SalesOrderID,YEAR(OrderDate) as orderyear from AdventureWorks.Sales.SalesOrderHeader

    order by ContactID asc

    ContactID SalesOrderID,OrderYear

    1441322001

    1455792002

    1463892002

    1474542002

    1483952002

    1494952003

    1507562003

    2534592003

    2589072003

    2651572004

    Delete fails when i try to delete using Common Table Expressions

    here is query

    WITH C AS(

    select *, ROW_NUMBER() OVER(PARTITION BY ContactID ORDER BY(SELECT 0)) AS EN from Test

    )

    DELETE FROM C WHERE EN>1

    But the same works fine when you use derived table query

    here is query

    delete from a from (

    select ContactID,SalesOrderID,OrderYear

    ,ROW_NUMBER() over(partition by contactid order by (Select 0)) as a

    from Test) as a

    where a>1

    What could be the problem

  • Is

    delete from a from ... valid?

    If it is, in my defence I currently only query data (I'm currently only a consumer of data, not a maintainer)

    It is valid. I should have tested it first - to me, it just looked wrong.:blush:

    Dave

  • Smash125 (5/6/2012)


    CREATE TABLE Test

    (

    ContactID INT,

    SalesOrderID INT,

    OrderYear DATETIME

    )

    insert into Test(ContactID,SalesOrderID,OrderYear)

    select top 10 ContactID,SalesOrderID,YEAR(OrderDate) as orderyear from AdventureWorks.Sales.SalesOrderHeader

    order by ContactID asc

    ContactID SalesOrderID,OrderYear

    1441322001

    1455792002

    1463892002

    1474542002

    1483952002

    1494952003

    1507562003

    2534592003

    2589072003

    2651572004

    Delete fails when i try to delete using Common Table Expressions

    here is query

    WITH C AS(

    select *, ROW_NUMBER() OVER(PARTITION BY ContactID ORDER BY(SELECT 0)) AS EN from Test

    )

    DELETE FROM C WHERE EN>1

    But the same works fine when you use derived table query

    here is query

    delete from a from (

    select ContactID,SalesOrderID,OrderYear

    ,ROW_NUMBER() over(partition by contactid order by (Select 0)) as a

    from Test) as a

    where a>1

    What could be the problem

    You say the delete fails but you don't tell us if it simply fails to delete any records or if you get an error message. If you get an error message, please share the complete and unabridged text of the error.

  • I do not get any error message.When i execute the query i get the message

    (0 row(s) affected)

  • Okay, can't answer your question as both queries work for me with no issues.

  • OK Will Check again 🙂

  • Works perfectly without any error

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

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

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