执行存储过程时出错

时间:2012-12-21 04:28:23

标签: sql-server stored-procedures

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

我有三个表,RegUsersAnswersQuestions

我必须从Answer表中提取Date_TimeAnswers,同时我必须找到发布答案的User Name,这是要获取的来自RegUsers表。

为此,我尝试了以下代码,该子代码正好运行1个结果。

但是当子查询产生多个结果时,它会给我一个错误:

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

请帮助我,建议我另一个子查询,以获得我想要的结果。

Thanxx。

1 个答案:

答案 0 :(得分:1)

将您的=更改为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 );