I use drupal 7 -rc4 , IIS and microsoft SQL server.
I have also installed the latest (dev version) of MSSQL drivers.
Once I enable the devel module I get the following error

PDOException: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]ORDER BY items must appear in the select list if SELECT DISTINCT is specified.: SELECT DISTINCT TOP(10) u.uid AS uid FROM {users} u LEFT OUTER JOIN {users_roles} r ON u.uid = r.uid WHERE ( (u.uid > :db_condition_placeholder_0) AND (u.status > :db_condition_placeholder_1) AND( (u.uid = :db_condition_placeholder_2) OR (r.rid IN (:db_condition_placeholder_3)) ) ) ORDER BY u.access DESC; Array ( [:db_condition_placeholder_0] => 0 [:db_condition_placeholder_1] => 0 [:db_condition_placeholder_2] => 1 [:db_condition_placeholder_3] => 3 ) in devel_switch_user_list() (line 767 of C:\inetpub\wwwroot\dr7\sites\all\modules\devel\devel.module)

I comment line 767 of devel.module ($uids = $query->execute()->fetchCol(); )
and then I get the following error:

PDOException: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near ')'.: SELECT TOP(10) uid FROM {users} WHERE uid > 0 AND uid NOT IN () AND status > 0 ORDER BY access DESC; Array ( ) in devel_switch_user_list() (line 783 of C:\inetpub\wwwroot\dr7\sites\all\modules\devel\devel.module).

I comment the line 783 ($uids = db_query_range('SELECT uid FROM {users} WHERE uid > 0 AND uid NOT IN (:uids) AND status > 0 ORDER BY access DESC', 0, $list_size - $num_links, array(':uids' => array_keys($links)))->fetchCol();)
and everything works Ok.

Is this an issue of devel module or do I have to report this to MSSQL driver team?

Regards

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

salvis’s picture

Yes, please move this issue to the MSSQL driver queue.

This query works just fine with MySQL. We could probably change it so that its translation would be acceptable to MSSQL, but this should not be necessary.

ben.bunk’s picture

moshe weitzman’s picture

Title: IIS7 - Miscrosoft SQL Server » User list query fails on SQL Server

I'm interested in changing this query so it gets more compatible. Patch welcome.

salvis’s picture

Sorry, I'm unable to create a patch right now, but if I understand the error message correctly, then MSSQL would be happy if we added u.access (and access in the second query) to the field list.

fetchCol() has a default column index of 0, so this should work.

But it should be fixed in the driver, as suggested in the linked issue.

ben.bunk’s picture

Heres a patch for this.

salvis’s picture

Status: Active » Needs work

Did you test this? From the OP I gather that we need this twice...

ben.bunk’s picture

Yes I tested this and it works correctly. The user posted that he commented the line to fetch the query that is fixed in the above patch (basically not executing the query).

The second query he lists failed because it uses a result set from the first query. If you look the error shows an empty IN() clause which was fixed with the patch above.

salvis’s picture

That's right, but the second query also has an ORDER BY access clause without including access in the field list.

I suspect that your first query delivered enough records so that the second one was not executed.

ben.bunk’s picture

The DISTINCT clause is what causes the problems. If DISTINCT is present in a SELECT statement all ORDER BY elements must appear in the SELECT list. Some examples I put together from SQL 2008 are below:

Query 1 - ORDER BY item not in the SELECT list:
SELECT DISTINCT u.uid FROM test_users u ORDER BY u.access DESC

Query 1 Result:
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Query 2 - All ORDER BY items now in the SELECT list:
SELECT u.uid FROM test_users u ORDER BY u.access DESC

Query 2 Result:
uid
1
0

If a ORDER BY element exists all SELECT items must appear in the GROUP BY section but the reverse is not true.

Query 3 - SELECT item not in the GROUP BY list:
SELECT u.uid FROM test_users u GROUP BY u.access
Query 3 Result:
Msg 8120, Level 16, State 1, Line 1
Column 'test_users.uid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Query 4 - All SELECT items now in the GROUP BY list + additional items:
SELECT u.uid FROM test_users u GROUP BY u.uid, u.access
Query 4 Result:
uid
0
1

salvis’s picture

Status: Needs work » Needs review

Ok, thank you for the additional information!

That make sense. I also like having a separate addField() call on a line of its own that will (hopefully) be traceable to this issue.

Can we have a second opinion from another SQL Server user, please?

Status: Needs review » Needs work

The last submitted patch, 1013396_devel_userlist_query.patch, failed testing.

ben.bunk’s picture

Status: Needs work » Needs review
FileSize
427 bytes

See if this will pass.

moshe weitzman’s picture

Status: Needs review » Fixed

Committed. Thx.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.