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

  • Home
  • SEARCH
  • 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 909205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:50:23+00:00 2026-05-15T16:50:23+00:00

I have a web based program which chooses some records from a database using

  • 0

I have a web based program which chooses some records from a database using a checkboxlist. (my check box list sends parameters to a stored procedure and I use the result of stored procedure).

Here how my check box list looks like

Overflow              [] (sends param1)
Open Records          [] (sends param2)
Records with 4 groups [] (sends param3)

Here how my stored procedure looks like,

SELECT * FROM myRecordsTable WHERE
        (Overflow LIKE '%O%' OR @param1= 'No')
        AND (OpenClose= 'Open' OR @param2= 'No')
        AND (4orMore = '4orMore' OR @param3 = 'No')

So as you can see, my check box lists works like “And” procedure, in other words, when I check Overflow and Open records in my checkboxlist, my stored procedure will return (Open records which has Overflow)

What I like to do is I want my check box list to work like “Or”, in other words, I want to be able to get (Open records “OR” records which has Overflow)

Important PS: I can’t have two stored procedures (1 for OR and 1 for AND) since my procedures are not that short like the example above, because once I change something in one procedure I will have to do the exact same changes in the second one and that is not good coding.

Important PS2: I don’t want to corrupt the structure of the stored procedure. I want to use my stored procedure as “AND” or “OR” whenever I want (using another parameter maybe), and the way I tried to do this was using dynamic sql as in the link below

SQL Server, Can a T-SQL operator be used like a variable ? Or something like that

but I couldn’t manage to solve it.

Thanks for helps.

Edit: Here I will give an example to show the problem more clearly

Below code, nothing is selected in checkboxlist, returns 42703 (Remember this number) rows

declare @op varchar(3)
set @op = 'AND'

declare @query nvarchar(4000)
set @query = 
'
SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
'
set @query = @query + '(ASIM LIKE ''%S%'' OR ''No''=''No'') '+@op+' ' 

set @query = @query + '(SonGrup LIKE ''Son Grup'' OR ''No''=''No'') '+@op+' '

set @query = @query + '(DortUzeri LIKE ''Dört ve Üzeri'' OR ''No''=''No'') '
exec(@query)

Below code ASIM is selected, returns 9349 rows

declare @op varchar(3)
set @op = 'AND'

declare @query nvarchar(4000)
set @query = 
'
SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
'
set @query = @query + '(ASIM LIKE ''%S%'' OR ''Yes''=''No'') '+@op+' ' 

set @query = @query + '(SonGrup LIKE ''Son Grup'' OR ''No''=''No'') '+@op+' '

set @query = @query + '(DortUzeri LIKE ''Dört ve Üzeri'' OR ''No''=''No'') '
exec(@query)

I won’t give the same code anymore, because the logic is same, the “No” turns into “Yes” when the checkbox list is selected, and this is the structure of the code.

ASIM and SonGrup is selected returns 5885 rows
ASIM and SonGrup and DortUzeri is selected returns 1385 rows

The above code works perfectly with AND, so when I wanted to use OR Operator I should get the result 37696 , which is the result of the below code

SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
ASIM LIKE '%S%' OR SonGrup LIKE 'Son Grup' OR DortUzeri LIKE 'Dört ve Üzeri'

So I executed the code with OR operator where the three checkbox list components is selected, the code is below and it returns 37696 , which is correct.

declare @op varchar(3)
set @op = 'OR'

declare @query nvarchar(4000)
set @query = 
'
SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
'
set @query = @query + '(ASIM LIKE ''%S%'' OR ''Yes''=''No'') '+@op+' ' 

set @query = @query + '(SonGrup LIKE ''Son Grup'' OR ''Yes''=''No'') '+@op+' '

set @query = @query + '(DortUzeri LIKE ''Dört ve Üzeri'' OR ''Yes''=''No'') '
exec(@query)

The problem arises when one of the checklist components is not checked, here is the 3rd component is not checked version of the code,

declare @op varchar(3)
set @op = 'OR'

declare @query nvarchar(4000)
set @query = 
'
SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
'
set @query = @query + '(ASIM LIKE ''%S%'' OR ''Yes''=''No'') '+@op+' ' 

set @query = @query + '(SonGrup LIKE ''Son Grup'' OR ''Yes''=''No'') '+@op+' '

set @query = @query + '(DortUzeri LIKE ''Dört ve Üzeri'' OR ''No''=''No'') '
exec(@query)

The above code should have returned 34613 which I found using the following

SELECT ASIM, SonGrup, DortUzeri FROM CacheOPERASYONELRawKayitlar WHERE
ASIM LIKE '%S%' OR SonGrup LIKE 'Son Grup'

However it returns 42703 (The number which I wanted you to remember caused because of ”No”=”No”) , that is where my problem arises. Here I couldn’t find the solution.

  • 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-05-15T16:50:23+00:00Added an answer on May 15, 2026 at 4:50 pm

    You should use dynamic sql – as well as the syntax being easier on the eye it will also give your query a better chance of performing.

    If you can, then I would probably construct the dynamic sql in your application rather than in the stored procedure, however if you really have to call a stored procedure then that also works:

    Disclaimer: I never usually write dynamic SQL inside a stored procedure, and I also don’t have SQL server installed on this machine to make sure that the following works, however hopefully this should be of some help:

    declare @op varchar(3)
    
    IF @operator = "AND"
        set @op = "AND"
    ELSE
        set @op = "OR"
    
    declare @query varchar(max)
    set @query = "select * from myRecordsTable WHERE" +
    
    IF @param1 <> "No"
        set @query = @query + " Overflow LIKE '%O%' " + @op
    IF @param2 <> ' No'
        set @query = " OpenClose = 'Open' " + @op
    
    IF @operator = "AND"
        set @query = @query + " 1=1"
    IF @operator = "OR"
        set @query = @query + " 1=0"
    
    exec(@query)
    

    Important parts to note:

    • I test the value of @operator explicitly to avoid SQL injection (never trust your caller)
    • I use 1=1 and 1=0 to avoid complicated logic with operators due to not knowing which of my conditions will be the first or the last. SQL server optimiser will optimise this away as a tautology early in the optimisation process.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can set these in your Tomcat server.xml. Add a… May 17, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer using System.Linq; var collection = new EngineMeasurementCollection(); int maxSpeed =… May 17, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer Don't put the image in the App_Data folder. Put it… May 17, 2026 at 1:07 am

Trending Tags

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

Top Members

Related Questions

I have a web based application which server's content to authenticated users by interacting
We have a web based client-server product. The client is expected to be used
Is there somewhere a web based app in which you can make a mockup
i am creating a bluetooth based server program in Bluez which basically handles connections
We've got several web-based applications that are launched from our ERP system (SAP R/3
I have a completely blank .aspx file residing on an IIS6 web server. The
I'm part of a team developing a new web-based product for our company. Whilst
I'm searching a software (web-based, php/mysql) that allows users to (anonymously/with registering) write suggestions
I want to know whether unhandled exception will make WCF service crash. I have
Web server log analyzers (e.g. Urchin) often display a number of sessions. A session

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.