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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:49:32+00:00 2026-06-08T06:49:32+00:00

I need to create a query which looks like this: SELECT attr FROM SomeTable

  • 0

I need to create a query which looks like this:

SELECT attr
FROM SomeTable B
WHERE EXISTS
   
(CASE WHEN B.some_attr = 0

       
THEN (SELECT x FROM C WHERE B.z = z)
    
WHEN B.some_attr = 1
       
THEN (SELECT x FROM D WHERE B.z = x)
    
END)

But SQL Server refuses to execute this bringing some syntax error.
How can I modify this query to make it work?

EDIT: I’m adding the error:

Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword ‘CASE’.
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword ‘WHEN’.
Msg 102, Level 15, State 1, Line 8 Incorrect syntax near ‘)’.

  • 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-08T06:49:33+00:00Added an answer on June 8, 2026 at 6:49 am

    Try this:

    SELECT A 
    FROM B 
    WHERE 
         CASE 
         WHEN B.some_attr = 0 AND EXISTS(SELECT x FROM C WHERE B.z = z) THEN 1
         WHEN B.some_attr = 1 AND EXISTS(SELECT x FROM D WHERE B.z = x) THEN 1
         END = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to change my query from native-query to ( named-query or create-query )
For example, I have a table which looks like this : id | name
I asked this question last week on SO- SQL Exclusion Query It looks like
I have a table which looks like this courseid session_date title published 1 2012-07-01
I would like to create a compiled query which uses reusable where predicates. An
I'm used to create query with createQuery() instead of queryBuilder, but I need to
I have a query being created using OLEDB from access and need to get
I found this script on about.com which I'm trying to learn from on how
I currently have a query running on my site which looks for all rows
I have run a query using Eclipse from a Sybase db. I need to

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.