How to fix "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS" error?

Jamie picture Jamie · Jul 1, 2013 · Viewed 102.9k times · Source

I'm trying to run the following query on MS SQL 2012 Express:

Select (
    Select Id, Salt, Password, BannedEndDate
    from Users
    where username = '" + LoginModel.Username + "'
), (
    Select Count(*)
    From LoginFails
    where username = '" + LoginModel.Username + "'
    And IP = '" + Request.ServerVariables["REMOTE_ADDR"] + "')"
);

But I get the following error:

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

How can I solve this problem?

Answer

Devart picture Devart · Jul 1, 2013

Try this one -

"SELECT 
       ID, Salt, password, BannedEndDate
     , (
          SELECT COUNT(1)
          FROM dbo.LoginFails l
          WHERE l.UserName = u.UserName
               AND IP = '" + Request.ServerVariables["REMOTE_ADDR"] + "'
      ) AS cnt
FROM dbo.Users u
WHERE u.UserName = '" + LoginModel.Username + "'"