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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:18:42+00:00 2026-05-16T23:18:42+00:00

I want to avoid string concatenation to create dynamic SQL query in SQL 2008.

  • 0

I want to avoid string concatenation to create dynamic SQL query in SQL 2008.

How can I achieve the below functionality, if at all, efficiently ?

A table stores the various conditions a user had chosen to search DB by using one or more values to search tables from. For e.g. he can give SSN or DOB or both SSN and DOB.

So if he gives multiple values as input, then the query needs to find records matching ALL the criteria. This needs me to create queries from the below shown two tables

I always need to return personid, which may be from the person table, or the view.

Search conditions given by user will be stored in the ‘conditions’ table below

conditions
-------------
id,custid,dob,ssn,base

person
--------
id,firstname,ssn,dob

view
--------
id,personid,base

So, IF DOB is to be searched

SELECT p.personid from conditon c 
INNER JOIN person p
ON p.dob=c.dob
WHERE c.condition=1

IF SSN and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
WHERE c.condition=1

IF SSN,DOB and base is to be searched

SELECT v.personid from conditon c 
INNER JOIN view v 
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
AND
p.dob=c.dob
WHERE c.condition=1
  • 1 1 Answer
  • 1 View
  • 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-05-16T23:18:43+00:00Added an answer on May 16, 2026 at 11:18 pm

    Use the pattern below with IsNull() or Coalesce() function

      Where ColumnName = IsNull(@InParameter, ColumnName) 
    

    or tyhe same pattern in a Join Condition

       Join Table a 
          On [Other stuff]
               And ColumnName = IsNull(@InParameter, ColumnName)
    

    And then, if the parameter is supplied, the ql will filter or join based on the parameter value, if it not supplied or is null, the query will ignore it (ColumnValue = ColumnVale is always true (except for nullable columns)

    EDITED: to add another predicate to Join Condition. because, in the case where you did not specifiy a parameter value, the join condition would always be true, and, unless some other predicate condition is in the join, you would effectively get a cartesian product…

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

Sidebar

Related Questions

For this question, we want to avoid having to write a special query since
An AJAX query returns an HTML string that has 2 tables. I want to
I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I want to avoid compiling php with fileinfo, ereg, and parch. What is the
I want to avoid code duplication as much as possible. Suppose I have a
Because I want to avoid repetitive code, and I'm using a lot of text
As much as possible I want to avoid unnecessary duplication of code. In my
I have a templated class that I want to avoid copying (because of the
I keep getting this as a warning I want to avoid getting this warning
I have a image gallery like Google+ or Facebook and i want to avoid

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.