简体   繁体   中英

T-sql run 1st query and then CASE 2nd query and output data or 'No'

Hey all I am trying to output data if the @blah equals Yes but output 'No' if not.

This is my current query:

DECLARE @blah VARCHAR(MAX)

SET @blah = (SELECT 
   CASE WHEN 
      COUNT(email) = '1' THEN     'Yes'
      ELSE                        'No'
   END
FROM 
   usersTbl 
WHERE 
   email = 'someone@somewhere.com');
SELECT
   CASE WHEN 
      @blah =                     'Yes' THEN 
     (SELECT * 
      FROM 
         usersTbl 
      WHERE 
         email = 'someone@somewhere.com')
      ELSE                        'No'
   END
FROM 
   usersTbl;

Currently the error I am getting with the above is:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

What am I missing?

You could use EXISTS here instead. Something like this.

if exists
(
    SELECT * 
      FROM 
         usersTbl 
      WHERE 
             email = 'someone@somewhere.com'
)
SELECT * 
      FROM 
         usersTbl 
      WHERE 
             email = 'someone@somewhere.com'
ELSE
    select 'No'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM