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 9215621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:40+00:00 2026-06-18T02:14:40+00:00

I need help to optmize the SQL logic in one of my functions. Please,

  • 0

I need help to optmize the SQL logic in one of my functions. Please, note that I am not able to use store procedure.

Here is my table. It will be initialized using @MainTable that contains a lot of records.

DECLARE TABLE @ResultTable
(
    ResultValue INT
)

These are tables that stores some parameters – they can be emty too.

DECLARE TABLE @ParameterOne (ParameterOne INT)
DECLARE TABLE @ParameterTwo (ParameterOne NVARCHAR(100))
...
DECLARE TABLE @ParameterN(ParameterN TINYINT)

Now, I need to join a lot of tables to my @MainTable in order to select from it only some of its records.
The selected records depend on the information stored in the parameters table.

So, my current solution is:

INSERT INTO  ResultTable(ResultValue)
SELECT ResultValue 
FROM MainTable M
INNER JOIN @MainOne MO
    ON M.ID=MO.ID
....
INNER JOIN @MainN MN
    ON M.IDN=MN.ID
WHERE (EXISTS (SELECT 1 FROM @ParameterOne WHERE ParameterOne=MO.ID) OR NOT EXISTS (SELECT 1 FROM @ParameterOne))
       AND 
      ...
       AND
      (EXISTS (SELECT 1 FROM @ParameterN WHERE ParameterN=MN.Name) OR NOT EXISTS (SELECT 1 FROM @ParameterN ))

So, the idea is to add the records only if they match the current criteria from the parameters tables.

Because I am not able to use procedure to build dynamic query I am using the WHERE clause with combinations of EXISTS and NOT EXISTS for each parameter table.

The problem is that it works slower when I am adding more and more parameters table. Is there an other way to do this without using a lot of IF/ELSE statements checking what parameter table has records – it will make the function a lot bigger and difficult for read.

And ideas and advices are welcomed.

  • 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. Editorial Team
    Editorial Team
    2026-06-18T02:14:42+00:00Added an answer on June 18, 2026 at 2:14 am

    Good question.

    Try the following one:

    INSERT INTO  ResultTable(ResultValue)
    SELECT ResultValue 
    FROM MainTable M
    INNER JOIN (SELECT * FROM @MainOne WHERE (EXISTS (SELECT 1 FROM @ParameterOne WHERE ParameterOne=@MainOne.ID) OR NOT EXISTS (SELECT 1 FROM @ParameterOne))) MO
        ON M.ID=MO.ID
    ....
    INNER JOIN (SELECT * FROM @MainN WHERE (EXISTS (SELECT 1 FROM @ParameterN WHERE ParameterOne=@MainN.Name OR NOT EXISTS (SELECT 1 FROM @ParameterN))) MO
        ON M.IDN=MN.ID
    

    Advantages:

    1. Result of the JOIN is more quickly, because it does not process all data (it is already filtered)
    2. It looks more simple for adjusting
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not expert in sql and I need help with the following query.
Need help with a query that I wrote: I have three tables Company id
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need help with one ajax function This is raw page setup. Page will
I need to optimize a PL/SQL function that is currently like that: CREATE OR
I need help with improving the performance of the following SQL query. The database
Please i need your help with how to optimize my query. I want to
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
Need help on these 2 questions: If i am tracking a virtual pageview for

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.