简体   繁体   中英

Subquery returned more than 1 value. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

Am am getting the following SQL Error and am unsure why?

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I have narrowed the problem down to one select in my proc.

DECLARE @ApplicationName varchar(32)
,@Email varchar(128)
,@Password varchar(128)

SET @ApplicationName = 'PraxiProSite'
SET @Email = 'jn@gmail.com'
SET @Password = 'jn@gmail.com'

DECLARE @PracId int
   SET @PracId = 
   (
        SELECT 
            MU.PractitionerId 
        FROM 
            MembershipUser AS MU WITH(NOLOCK) 
            LEFT JOIN Practitioner AS Pr WITH(NOLOCK) ON Pr.PractitionerId = MU.PractitionerId 
            LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId
    )

SELECT @PracId
SET @PracId = 
   (
        SELECT Top 1
            MU.PractitionerId 
        FROM 
            MembershipUser AS MU WITH(NOLOCK) 
            LEFT JOIN Practitioner AS Pr WITH(NOLOCK) ON Pr.PractitionerId = MU.PractitionerId 
            LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId
    )

Make sure you do the order by here, to get the expected value

Actually, you can do it without a sub-query.

SELECT Top(1) @PracId = MU.PractitionerId 
FROM MembershipUser AS MU WITH(NOLOCK) 
        LEFT JOIN Practitioner AS Pr WITH(NOLOCK) ON Pr.PractitionerId = MU.PractitionerId 
        LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId

And if your final query is SELECT @PracId , you may not need the variable as well.

SELECT TOP(1) MU.PractitionerId 
FROM MembershipUser AS MU WITH(NOLOCK) 
        LEFT JOIN Practitioner AS Pr WITH(NOLOCK) ON Pr.PractitionerId = MU.PractitionerId 
        LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId

I think you've forgotten to include the filter, eg :

SELECT 
  MU.PractitionerId 
FROM MembershipUser AS MU WITH(NOLOCK) 
LEFT JOIN Practitioner AS Pr WITH(NOLOCK) 
  ON Pr.PractitionerId = MU.PractitionerId 
LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId
WHERE MU.Email = @Email and /* whatever else you need */

However, this is still most likely not the best way to do this. You're using ASP.NET membership, right? There's a bunch of stored procedures you should use to get the user ID (otherwise you're going to have trouble comparing the password, I expect), and do the rest in your SQL.

I believe also you maybe missing a WHERE clause in the SELECT statement. Because you are currently select all the rows from MembershipUser

SET @PracId = 
(
    SELECT Top 1
        MU.PractitionerId 
    FROM 
        MembershipUser AS MU WITH(NOLOCK) 
        LEFT JOIN Practitioner AS Pr WITH(NOLOCK) ON Pr.PractitionerId = MU.PractitionerId 
        LEFT JOIN Person AS Pe WITH(NOLOCK) ON Pe.PersonId = Pr.PersonId
    WHERE .....
   (i.e. WHERE MU.email = @email
           AND MU.Application = @application)
)

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