Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 141251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:44:47+00:00 2026-05-11T07:44:47+00:00

I am using T-SQL query directly in Microsoft SQL Server Studio’s Query window against

  • 0

I am using T-SQL query directly in Microsoft SQL Server Studio’s Query window against to a large Oracle DB, and my SQL server is 2005.

I have created a linked server as myOracleServer. By using the following T-SQL query:

SELECT COUNT(*) FROM myOracleServer..owner.myTable WHERE id = 1000 AND Dt = '2009-02-26' 

It take more than 1 minute to make just one call. For a small size table it is OK, but myTable on Oracle side is very large with millions of rows data.

What I found out is that I could use OPENQUERY to make a similar call with SQL query as a pass-through one. The result is very fast. The execution time is 00:00:02!

SELECT * FROM OPENQUERY(myOracleServer, 'SELECT COUNT(*) FROM owner.myTable WHERE ...'); 

The problem I have is that the query is not a constant string. I may change id and Dt values in the query, something like:

'SELECT COUNT(*) FROM ownwer.myTable WHERE id = ' + CAST(@id AS VARCHAR)... 

OPENQUERY does not support a variable as query string nor expression.

Is there any other way to get the pass-through query to Oracle with fast performance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:44:48+00:00Added an answer on May 11, 2026 at 7:44 am

    I think I figure out a solution to resolve slowness issue when I try to pass a query from SQL 2005 server to an Oracle db.

    There are three ways to do it. The first one is something like:

    SELECT COUNT(*) FROM myOracleServer..owner.myTable    WHERE id = 1000 AND Dt = '2009-02-26' 

    If the table on Oracle side is a big one, with 3M rows of data for example, the execution time is very long. Today I tried it again on my SQL server and it took about 2’44’ just for one query (maybe the table is very busy during business days).

    The second method is to use OPENQUERY, as I stated in my question:

    SELECT COUNT(*) FROM OPENQUERY(myOracleServer, N'SELECT COUNT(*) FROM    owner.myTable WHERE id = 1000 AND Dt = TO_DATE(''2009-02-26'')');  

    It is very fast. I run it again and the execution time is 00:00:00, stunning fast! However, the problem with this method is that OPENQUERY does NOT support variable as a query.

    Actually, I found this method and was very exited about the result late at work yesterday. I wrote a blog on this issue last night. This morning when I tried to put this into production (a stored procedure), I could not use OPENQUERY since in my case, a variable query has to be built on id and date.

    The good news is that I found the third way, an excellent solution to the issue:

    DECLARE @sql NVARCHAR(MAX); SET @sql = N'SELECT COUNT(*) FROM owner.myTable WHERE id = ' +    CAST(@id AS VARCHAR) + N' AND Dt = TO_DATE(''' + @dt +   N''', ''yyyy-mm--dd'')'); EXEC (@sql) AT myOracleServer; 

    The key point here is to use EXEC with AT to specify a remote server or linked server, and don’t forget to use () to bracket @sql variable. The execution time is 00:00:00!

    My bog will be updated this evening.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 135k
  • Answers 135k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like you forgot to create a <tbody> element. May 12, 2026 at 6:56 am
  • Editorial Team
    Editorial Team added an answer You don't need to refresh, Ajax is a simple HTTP… May 12, 2026 at 6:56 am
  • Editorial Team
    Editorial Team added an answer Use a DirectorySearcher ... var search = new DirectorySearcher( new… May 12, 2026 at 6:56 am

Related Questions

I am using T-SQL query directly in Microsoft SQL Server Studio's Query window against
LINQToSQL doesn't like my sproc. It says that the sproc has return type none,
I have a project (private, ASP.net website, password protected with https) where one of
I am currently in the process of debugging a database performance issue. I have
I'm working on repairing the test suite for a project of ours, which is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.