top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is open query in sql server?

+3 votes
478 views
What is open query in sql server?
posted Apr 8, 2015 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

OPENQUERY function executes specified query on the given linked server, which is an OLE DB data source. The OPENQUERY function can be referenced in the FROM clause of a query.

OPENQUERY does not accept variables for its arguments.

Syntax:

    OPENQUERY (  linked_server_name ,'query' )  

    SELECT  ename, CompanyName, sal FROM OPENQUERY
     (ServerName, 'SELECT ename, sal FROM emp') 

Above example returns employees ename, sal by creating linked server of specified server.

answer Apr 8, 2015 by Arun Gowda
Similar Questions
+2 votes

I have the following setup:

class Unit
 has_many :reports
end

class Report
 belongs_to :unit
end

Basically I have a list of units and I want to select the last report for each unit (based on time) and order the resulting last reports by longitude.

Sounds simple, but my implementation looks like this:

units = current_user.accessible_units
report_ids = []
if units.size > 0
  units.map(&:id).uniq.each do |id|
    report = Report.select(:id).where(unit_id: id).order("time desc").limit(1)
    if !report.empty?
      report_ids << report.try(:first).try(:id)
    end
  end
end   
reports = Report.where(id: report_ids).order("longitude desc")

Is there a way to perform this same query using sql (active record relations) and minimize the use of ruby iterators, like map and each? Also notice in query above, I make two hits to the database by querying reports for time and then descending order. Is there a way to eliminate that too?

...