Bugs: Browse | Submit New | Admin

[#13355] add_limit_offset in SQL Server 2005

Date:
2007-08-25 09:39
Priority:
3
Submitted By:
Nobody
Assigned To:
Nobody (None)
Category:
AR-JDBC
State:
Open
Summary:
add_limit_offset in SQL Server 2005

Detailed description
I'm using ActiveRecord-JDBC with SQL Server 2005. I found the attached implementation of add_limit_offset to be more
efficient than the one included in version 0.5. The downside is that it will not work with SQL Server 2000.

Maybe there's some way to distribute both versions of the adapter.


    def add_limit_offset!(sql, options)
      if limit = options[:limit] and offset = options[:offset]        
        if options[:order]
          sql.sub!(/(SELECT.*)(\s+ORDER BY.*)/i) do
            "SELECT B.* FROM (SELECT A.*, ROW_NUMBER() over (ORDER BY #{options[:order]}) AS internal$rownum FROM
(#{$1}) A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset}#{$2}"
          end
        else
          sql.sub!(/(SELECT.*)/i) do
            "SELECT B.* FROM (SELECT A.*, ROW_NUMBER() over (ORDER BY internal$const) AS internal$rownum FROM (SELECT
C.*, 1 AS internal$const FROM ( #{$1} ) C ) A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <=
#{limit + offset}"
          end
        end
      elsif sql !~ /^\s*SELECT (@@|COUNT\()/i
        sql.sub!(/^\s*SELECT([\s]*distinct)?/i) do
          "SELECT#{$1} TOP #{options[:limit]}"
        end unless options[:limit].nil?
      end
    end

Add A Comment: Notepad

Please login


Followup

Message
Date: 2008-06-02 02:31
Sender: Leigh Kennedy

Don't have access to 2005, but in 2000,

select @@version

returns:
Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
	May  3 2005 23:18:38 
	Copyright (c) 1988-2003 Microsoft Corporation
	Standard Edition on Windows NT 5.2 (Build 3790: )


You would just need to parse the output.
(1 row(s) affected)
Date: 2008-04-17 02:40
Sender: Nick Sieger

Is there a way to detect which server version you're running?

Attached Files:

Name Description Download
jdbc_mssql.rb Download

Changes:

Field Old Value Date By
File Added2404: jdbc_mssql.rb2007-08-25 09:39None