Ticket #1259 (closed defect: fixed)

Opened 12 months ago

Last modified 4 weeks ago

MSSQL: Incorrect Syntax Near the Keyword "DISTINCT'

Reported by: pr0f3t Owned by: guilhermeblanco
Priority: minor Milestone: 1.0.10
Component: Native SQL Version: 1.0.7
Severity: abc Keywords: MSSQL
Cc: Has Test: no
Status: Pending Core Response Has Patch: no

Description (last modified by jwage) (diff)

I have a RecordType? which hasMany Record(s)

When I do this:

$select = Doctrine_Query::create()
            ->from("RecordType t")
            ->leftJoin("t.Record r")
            ->select('t.id, t.name, t.description, t.created_at, t.modified_at')->limit(30)->execute();

I get the above error. Looks like it's the limit clause causing it. Removing the limit clause gets rid of the error.

The SQL looks something like this:

[SELECT [r].[id] AS [r__id], [r].[name] AS [r__name], [r].
[description] AS [r__description], [r].[created_at] AS
[r__created_at], [r].[modified_at] AS [r__modified_at] FROM
[record_type] [r] LEFT JOIN [record] [r2] ON [r].[id] = [r2].
[record_type_id] WHERE [r].[id] IN (SELECT * FROM (SELECT TOP 30 *
FROM (SELECT TOP 30 DISTINCT [r].[id] FROM [record_type] [r]) AS
inner_tbl) AS outer_tbl)]

From #doctrine, the triple sub query should simply be something like:

where r.id in (select distinct top 30 id from record_type)

and the SELECT TOP 30 DISTINCT should be SELECT DISTINCT TOP 30.

Quick, dirty fix to file: Doctrine/Connection/Mssql.php (Line: 128)

$select = (preg_match('/^SELECT(s+)DISTINCT/i',  $query))?"SELECT DISTINCT":"SELECT";
$query = preg_replace('/^'.$select.'s/i', $select.' TOP ' . ($count+$offset) . ' ', $query);

Attachments

limit-subquery-mssql.diff (1.2 KB) - added by guilhermeblanco 7 months ago.
Proposed patch. Cannot apply since I have no way to test it.

Change History

Changed 11 months ago by jwage

  • version changed from 0.11 to 1.0
  • milestone changed from Unknown to 1.0.0-RC1

Changed 10 months ago by anonymous

  • milestone New deleted

Milestone New deleted

Changed 9 months ago by jwage

  • version changed from 1.0.0 to 1.0.2
  • milestone set to 1.0.4

Changed 7 months ago by guilhermeblanco

Proposed patch. Cannot apply since I have no way to test it.

Changed 7 months ago by jwage

  • description modified (diff)
  • milestone changed from 1.0.5 to 1.0.6

Changed 7 months ago by jwage

  • milestone changed from 1.0.6 to 1.0.7

Changed 6 months ago by guilhermeblanco

  • status changed from new to closed
  • resolution set to fixed

(In [5325]) [1.0, 1.1] Fixes #1259. Fixed SELECT DISTINCT query with limit being applied.

Changed 4 months ago by gouiggou

  • status changed from closed to reopened
  • version changed from 1.0.2 to 1.0.7
  • resolution fixed deleted
  • milestone changed from 1.0.7 to 1.0.9

This patch has been deleted on r5448 and has not been applied on doctrine 1.1.

Changed 4 months ago by jwage

  • status changed from reopened to assigned
  • owner changed from romanb to guilhermeblanco

Changed 5 weeks ago by jwage

  • severity set to abc
  • milestone changed from 1.0.9 to 1.0.10

Changed 4 weeks ago by guilhermeblanco

  • status changed from assigned to closed
  • resolution set to fixed

This patch was already applied sometime. Closing this ticket since it's not valid anymore.

Regards,

Note: See TracTickets for help on using tickets.