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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:01:50+00:00 2026-05-25T13:01:50+00:00

I have to pass a parameter into a condition to select rows, if I

  • 0

I have to pass a parameter into a condition to select rows, if I pass 0 it should select all rows, 1 – only HDD, 2 – All except HDD

Please see code:

declare @Accessories table (id int, name nvarchar(20), atype nvarchar(20), itype int)

insert into @Accessories values (1, 'Seagate HDD 100GB', 'HDD', 1)
insert into @Accessories values (2, 'Samsung HDD 100GB', 'HDD', 1)
insert into @Accessories values (3, 'WD HDD 500GB', 'HDD', 1)
insert into @Accessories values (4, 'Samsung 4GB', 'RAM', 2)
insert into @Accessories values (5, 'GeForce 512MB', 'Video', 3)

declare @param int

set @param = 1 /* 0 - All records, 1 - Only HDD, 2 - All exclude HDD */

select 
    * 
from @Accessories
where itype = @param /* NEED RIGHT CONDITION HERE*/

I can’t write code like if @param = 0 then …
Is it way to write condition in WHERE statement?

Thanks!

  • 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-25T13:01:51+00:00Added an answer on May 25, 2026 at 1:01 pm

    You could use something like this:

    select * 
    from @Accessories 
    where (@Param = 0)
      or (@Param = 1 and atype = 'HDD') 
    --or (@Param = 1 and itype = 1) , if itype and atype are connected
      or (@Param = 2 and atype != 'HDD')
    option (recompile)
    

    If Param is 0 the first condition becomes 0=0 and matches for all rows, so all rows are returned. If param is 1 or 2, only the respective or branch matches on the first condition, so it returns what the second condition specifies.

    Also, the option (recompile) is really important (view the article in Martin’s comment). It instructs SQL server to use the runtime values of the parameters when preparing an execution plan, so basically:

    • when @Param = 0 the query becomes select * from @Accessories
    • when @Param = 1 the query becomes select * from @Accessories where atype = 'HDD'
    • when @Param = 2 the query becomes select * from @Accessories where atype != 'HDD'
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to pass some parameter from an action to another action,for example to
I have to use an other application (console) to pass some parameter to this
if i have a protected method, can i pass in a parameter where the
I have a get-parameter with name controller . When I try to pass it
how to Pass multiple parameters via Command Parameter in WPF?? Do we have to
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I need to convert this xml into array and then i have to pass
I have a workbook where several of my data feeds pass a parameter back
I'm trying to pass a parameter from php into my javascript function inside html.
is there any way to pass parameter into service from SESSION or other source?

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.