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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:41:57+00:00 2026-06-14T11:41:57+00:00

I have a table TableX . It contains three columns: A INT , B

  • 0

I have a table TableX. It contains three columns: A INT , B INT and C VARCHAR(1) (valid values for column C being the name of the columns ‘A’ or ‘B’). I need to count the number of occurrences where either of the following conditions are met:

  1. When C is NULL and does contains values in either A or B greater than zero. or
  2. When C is NOT NULL (i.e. ‘A’ or ‘B’) and the value in the specified column (A or B) is either zero or NULL.

My current stored procedure looks like

CREATE PROCEDURE ispcSomeName @NumOcc INT OUTPUT 
AS
SELECT COUNT(*) AS [NumOcc] 
FROM TableName 
WHERE (C IS NULL 
       AND ((A IS NOT NULL OR A > 0) OR 
            (B IS NOT NULL OR B > 0))) 
    OR (CritCarType IS NOT NULL 
        AND (CASE SET @TmpColumnName = CritCareType 
                 WHEN N'A' THEN (A IS NULL OR A <= 0) 
                 WHEN N'B' THEN (B IS NULL OR B <= 0)))
GO

The problem is that I need to check the column that C references. That is if C = 'A', I need to check if A IS NOT NULL OR A <= 0. There is clearly an issue with the SET in the final WHERE. I have got a few of these types of SPs to author and I want to avoid complex TSQL if I can help it…

How do I dynamically get reference to the relevant column in the WHERE clause?

Thanks for your time.

  • 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-14T11:41:59+00:00Added an answer on June 14, 2026 at 11:41 am
    Select Count(*)
    from tab
    where 
    (
        C is null and (Coalesce(A,0)>0 or Coalesce(B,0)>0)
    )
    or 
    (
        C is not null and Case when C='A' then Coalesce(A,0) 
        when C='B' then Coalesce(B,0) end <=0
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table, InnoDB, with columns id(int), name(varchar) and info(text). I need to
You have a table table1 that contains id column, that is int(11), not null,
I have table which contains double values stored in mysql database ...I need to
I have a table that contains several thousand rows, having a Varchar column that
I have a table containing columns id (int), logical expression (varchar) and result (bit).
I have a table with three columns, and two cells contain select boxes with
I have a table that contains an Id column as a primary key. There
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign
I have a table with three columns: id , ref and catName . Every
I have a table in my DB called Emplyee and contains these columns :

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.