简体   繁体   中英

Error while executing a stored procedure

ALTER PROCEDURE [dbo].[SpFillAnswer] 
    @q_Id nvarchar(50)
AS
BEGIN
    --SET NOCOUNT ON;

    SELECT Answer, Date_Time, Name 
    FROM Answers, RegUsers
    WHERE Answers.Q_Id = @q_Id 
      AND RegUsers.UserId = (SELECT Answers.User_Id FROM Answers WHERE Answers.Q_Id = @q_Id);
END

I have three tables, RegUsers , Answers and Questions .

I have to fetch Answer and Date_Time from Answers table at the same time I have to find out the User Name who posted the answer, which is to be fetched from RegUsers table.

For this I tried the following code which is running properly for 1 result by the subquery.

But it's giving me an error when more than one results comes out from subquery:

(Select Answers.User_Id from Answers where Answers.Q_Id = @q_Id )

Please help me out, suggest me another subquery for my desired result.

Thanxx.

Change your = to IN :

Select Answer, Date_Time, Name from 
Answers, RegUsers
where Answers.Q_Id = @q_Id 
and RegUsers.UserId IN (
   Select Answers.User_Id 
   from Answers 
   where Answers.Q_Id = @q_Id );

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