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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:25:23+00:00 2026-06-03T05:25:23+00:00

I have a page where user dynamically add search condition(s) to filter out records.

  • 0

I have a page where user dynamically add search condition(s) to filter out records.
I am sending those conditions to stored proc in a TVP.
Now i need to write a query which applies those filters work as “AND” instead of “OR”

Consider an example of student table

ID          Name         Marks
----------- ---------    -------
2           bab          65
4           bad          75
6           baf          85

I am getting conditions to Stored proc in TVP as (i am sending Ids for column and operator, but for clarity i used column name and operator in this example)

 Column     Operator    Value
----------- ---------   -------
Name        Contains    a
Marks       >=          75

Now i need to use this TVP to filter out records from student table, in this example the last 2 rows of student table will be displayed to user.

Can anyone help me in writing a query for this, i tried making cross join between student table and TVP, but query is ORing instead of ANDing the conditions in TVP

Sample query:

    DECLARE @tmpCond TABLE
(
    ColumnId SMALLINT,
    OperatorId SMALLINT,
    Value VARCHAR(10)
)

INSERT INTO @tmpCond
        ( ColumnId ,
          OperatorId ,
          Value
        )
VALUES  ( 1,1,'a')
        ,(2,2,'75')

SELECT * FROM dbo.Student A
CROSS JOIN @tmpCond B
WHERE 
    (B.ColumnId = 1 AND B.OperatorId = 1 AND A.NAME LIKE '%'+B.Value+'%')
    OR
    (B.ColumnId = 2 AND B.OperatorId = 2 AND A.Marks >= B.Value)
  • 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-03T05:25:24+00:00Added an answer on June 3, 2026 at 5:25 am

    You need to do the following:

    1. Create the preliminary query as a string, missing the where parameters: query = 'select * from student where xxx order by name, marks'. We’ll replace xxx with proper contents.
    2. Create the a variable for the query part where_part = ''
    3. Use a cursor to iterate through every row in TVP
    4. For every column, operator, value in TVP, build up a string like name + operator + value + ' and ' and append it to where_part.
    5. After having processed all rows from TVP, remove the last ' and ', in this sense: ‘condition and condition and condition and ‘.
    6. Replace 'xxx' in query with where_part
    7. Run exec(@query) (tsql, other DBs have different syntax). See also this article and google for “dynamic sql in stored procedures “.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page where the user can dynamically add file upload boxes. Adding
I have a web page where I have to let the user dynamically add
I have a page in which I am loading a user control dynamically as
I have a page which contains a dynamically generated grid, where a user can
How do I add a user control on page load dynamically in ASP.NET? I
I have 7 jquery ui tabs on a jspx page. I dynamically add a
I have a user control UserApplianceControl that I need to dynamically add to a
I would like to have the page on user/sidebar on the right in the
I need something simple; I have page where a user clicks an author to
I have a page where a user can import data to the site. either

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.