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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:37:04+00:00 2026-06-03T04:37:04+00:00

If I do SELECT * FROM cte1 c WHERE c.GrantNumber NOT IN ( SELECT

  • 0

If I do

SELECT * FROM cte1 c
    WHERE c.GrantNumber NOT IN
    (

      SELECT t1.nai_grant_number FROM #temp t1

    ) 

This works fine.

But if I do

SELECT * FROM cte1 c
    WHERE c.GrantNumber NOT IN
    (
      CASE WHEN 
                @AutoRenewalChk = 1 THEN
                 (

                     SELECT t1.nai_grant_number FROM #temp t1
                 ) END          
    )

getting error

Msg 512, Level 16, State 1, Line 33 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

What is the reason? Cannot we use a case statement like the above?

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-06-03T04:37:05+00:00Added an answer on June 3, 2026 at 4:37 am
    SELECT * FROM cte1 AS c
    WHERE NOT EXISTS
    (
      SELECT 1 FROM #temp AS t1 
      WHERE t1.nai_grant_number = CASE @AutoRenewalChk 
        WHEN 1 THEN c.GrantNumber END
    );
    

    Here is why NOT IN will not work here. Try these two queries. Would you expect the same result in each case?

    SELECT x FROM (SELECT x = 1) AS x WHERE x NOT IN 
        (SELECT y = 2);
    
    SELECT x FROM (SELECT x = 1) AS x WHERE x NOT IN 
        (SELECT y = 2 UNION ALL SELECT NULL);
    

    Adding a possible NULL here (which Dan P suggested as an ELSE in his answer, or if #temp can contain even a single row where nai_grant_number is NULL) completely changes the semantics of NOT IN. Therefore just about any time you are thinking about writing a NOT IN query, you should re-think it as a NOT EXISTS (or LEFT OUTER JOIN, or other struct).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

select * from table where category=@categoryid I'm not sure how easy is this but
SELECT * FROM (SELECT ROW_NUMBER() over ( ORDER BY CASE WHEN @SortExpression ='Country_id' THEN
SELECT * FROM Orders WHERE [DateSync] > #2010-11-10 03:11:00# This works if you run
select * from temp where ssn not in (select distinct ssn from temp inner
SELECT * FROM ...LIMIT 5, 10 But what if I want the total rows?
SELECT * FROM `objects` WHERE (user_id IN ('7,8,12,9') AND visibility IN ('0,1')); but it
SELECT * FROM [makes$] WHERE Corporate Name='Champion Enterprises, Inc.' I'm running this query on
SELECT * FROM Customer WHERE Name 'LIKE [a-f]%' How Can I acheive this in
I have CTEs which all uses NOLOCK inside. But then selects from those CTEs
select * from myTable where myInt will not show any possible_keys when explaining the

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.