Sub-query returned more than one item... i need it to return more than one item though what should i do?

StackOverflow https://stackoverflow.com/questions/22337597

Question

Hi i am making a wall stored procedure that will display a list of peoples posts. this is the code below

    ALTER PROCEDURE [dbo].[spDisplayWall] (@AccountName varchar(50))
    /*****
    Description: displays the users wall
    *****/
    AS
        BEGIN
            SELECT * FROM WallPosts
            WHERE Posted_By = (SELECT AddedAccountName FROM Friends WHERE AccountName = @AccountName)
            ORDER BY WallPosts.Date_Posted ASC
        END

what i want the code above to achieve is that it will select everything from the wallposts where the friends account name is equal to there's. so essentially return all there friends post. but because of the way i have coded it i can only return one friends. i need it to return all the peoples post. below is all it needs to execute and i want to keep it that way if possible, it just takes the account holders name.

            EXEC spDisplayWall 'KMV661'

here is the error i'm getting:

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

La solution

Use in instead of =:

       SELECT *
       FROM WallPosts
       WHERE Posted_By in (SELECT AddedAccountName FROM Friends WHERE AccountName = @AccountName)
       ORDER BY WallPosts.Date_Posted ASC
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top