tags:

views:

301

answers:

6

Hi Everyone,

This is my first post on stackoverflow, I hope one of many!

My question is this: I'm using CTE in a query to detect and remove duplicate records in a table. This query works just fine in SQL Server 2005 / 2008, but in Compact it throws an exception:

There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = WITH ]

This is my query:

SqlCeConnection con = new SqlCeConnection(ConfigurationManager.ConnectionStrings["ADSLConnectionString"].ConnectionString);
                SqlCeCommand command = new SqlCeCommand();

                command.Connection = con;
                command.CommandType = CommandType.Text;

                command.CommandText = "WITH Dublicates_CTE(Username, accountid)" +
                                      " AS" +
                                      " (" +
                                      "     SELECT UserName,min(accountid)" +
                                      "     FROM Accounts" +
                                      "     GROUP BY username" +
                                      "     HAVING Count(*) > 1" +
                                      " )" +
                                      "     DELETE FROM Accounts" +
                                      "     WHERE accountid IN (" +
                                      "         SELECT Accounts.accountid" +
                                      "         FROM Accounts" +
                                      "         INNER JOIN Dublicates_CTE" +
                                      "         ON Accounts.Username = Dublicates_CTE.Username" +
                                      "         AND Accounts.accountid <> Dublicates_CTE.accountid" +
                                      " ) ";

                con.Open();

                command.ExecuteNonQuery();

Am I missing something, or does CTE not work on SQL Server Compact?

+1  A: 

some things are not supported by the mobile version CTE and store procs for example will not work on the mobile version. You could use the express version which is also free

SQLMenace
+1  A: 

Thanks... Maybe a workaround to detect and remove duplicates without using CTE?

Thanks

FaNIX
+1  A: 

You can probably just nest the query, something like this (may have some syntax problems):

DELETE FROM Accounts
WHERE accountid IN (
    SELECT Accounts.accountid
    FROM Accounts
    INNER JOIN (
        SELECT UserName,min(accountid) accountid
        FROM Accounts
        GROUP BY username
        HAVING Count(*) > 1
    ) Dublicates_CTE
    ON Accounts.Username = Dublicates_CTE.Username
    AND Accounts.accountid <> Dublicates_CTE.accountid
)
Lucero
+1  A: 

Thank you very much, that works just fine! Syntax error free :)

FaNIX
Perfect, thanks for the feedback! Please note, here at stackoverflow, the use of comments is encouraged instead of adding new responses, so that the responses remain free of "chatter". Since responses are rearranged depending on their state (accepted answer, up/down-votes) the order does not remain steady anyways.
Lucero
+1  A: 

For the future here is a good link Differences Between SQL Server Compact and SQL Server

SQLMenace
A: 

Some proof regarding whether SQL Compact 3.5's TSQL subset can use Common Table Expressions:

alt text

Tested with Visual Studio 2010 and a new SQL Compact .sdf file.

p.campbell