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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:31:00+00:00 2026-05-26T11:31:00+00:00

I had an issue in my stored procedure that needs to include rows or

  • 0

I had an issue in my stored procedure that needs to include rows or not depending on the value of a parameter

  • if @var_exclude = 'Y' I need to show only rows that mytable.field1 = 'N'
  • if @var_exclude = 'N' I need to show all rows

I’m using the following query but doesn’t work properly

My code is like

@var_exclude    varchar(1)

    select mytable.field1, mytable.field2, mytable.field3, mytable.field4,mytable.field5 
    from mytable
Where mytable.field6 is null
  and mytable.field1 is not null
  and (mytable.state = @paramstate or @paramstate = 'ALL')
  and  mytable.field1 = Case when @var_exclude= 'Y' Then 'N' Else @var_exclude End
End

I’m confused!

  • 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-26T11:31:01+00:00Added an answer on May 26, 2026 at 11:31 am

    Your logic handling @var_exclude is wrong.

    When you pass @var_exclude = 'Y', then your query basically gets resolved to:

    select .....
    from mytable
    where mytable.field6 is null 
      and mytable.field1 is not null
      and (mytable.state = @paramstate or @paramstate = 'ALL')
      and mytable.field1 = 'N'
    

    since in this case, the when @var_exclude= 'Y' Then 'N' part of your CASE clause is used.

    If you pass anything else in, then you take the value you’re passing in; so if you pass in @var_exclude = 'N' then your query is resolved to:

    select .....
    from mytable
    where mytable.field6 is null 
      and mytable.field1 is not null
      and (mytable.state = @paramstate or @paramstate = 'ALL')
      and mytable.field1 = 'N'
    

    since in this case the Else @var_exclude of your CASE clause is used – and since @var_exclude = 'N', you get the exact same query as before….

    I would probably write this procedure something like this:

    CREATE PROCEDURE dbo.GetData 
         @paramstate VARCHAR(???),
         @var_exclude CHAR(1)
    AS BEGIN 
       IF @var_exclude = 'N' THEN
          SELECT
             t.field1, t.field2, t.field3, t.field4, t.field5 
          FROM 
             dbo.mytable t
          WHERE 
             t.field6 IS NULL
             AND t.field1 IS NOT NULL
             AND (t.state = @paramstate OR @paramstate = 'ALL')
    
       ELSE
          SELECT
             t.field1, t.field2, t.field3, t.field4, t.field5 
          FROM 
             dbo.mytable t
          WHERE 
             t.field6 IS NULL
             AND t.field1 IS NOT NULL
             AND (t.state = @paramstate OR @paramstate = 'ALL')
             AND t.field1 = 'N'
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that returns a value, not a dataset, and I
I have a weird issue where I have a stored procedure that is inserting
I had an issue with my search not return the results I expect. I
I've consistently had an issue with parsing XML with PHP and not really found
I need some help in fixing the below issue. I had transaction blocks in
We have the following simple Stored Procedure that runs as an overnight SQL server
I have a stored procedure that I've mapped in my entity framework model (trying
I have a C# app that is reading data from a stored procedure and
I have a oracle stored proc that needs to be called from my Java
A coworker of mine has had issue with Mootools being backward compatible and I

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.