需要帮助一些存储过程错误

时间:2010-12-04 07:52:34

标签: c# asp.net sql-server

  

可能重复:
  need help with some stored procedure plz

错误: -

为变量赋值的SELECT语句不能与数据检索操作结合使用。

3 个答案:

答案 0 :(得分:3)

你得到的错误是你的选择是在同时吐出resutls的同时更新变量。 按如下方式拆分查询:

 ALTER PROC [Admin].[sp_Ques]    
    (
        @QuesID bigint
     )

    AS
    BEGIN
DECLARE @Lang int   -- ERROR HERE

        IF @QuesID = 0
            SET @QuesID =NULL
    SELECT TOP 1 @Lang=FQ.AdminLanguageID
    FROM         Admin.Ques FQ
    LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID
    WHERE   FQ.QuesID = Coalesce(@QuesID,QuesID)

    SELECT TOP 1 FQ.QuesID, FQ.Ques,QuesAns as QuesAns,FQ.QuesAns[Answers], FQT.QuesType ,FQ.QuesTypeID, FQ.QuesParentID, FQ.Active,FQ.AdminLanguageID
    FROM         Admin.Ques FQ
    LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID
    WHERE   FQ.QuesID = Coalesce(@QuesID,QuesID)

    SELECT TelerikLanguage FROM Admin.Language 
    WHERE AdminLanguageID=@Lang 
    END

答案 1 :(得分:0)

更改

SELECT TOP 1 FQ.QuesID, FQ.Ques,QuesAns as QuesAns,FQ.QuesAns[Answers], FQT.QuesType ,FQ.QuesTypeID, FQ.QuesParentID, FQ.Active,FQ.AdminLanguageID,@Lang=FQ.AdminLanguageID 
    FROM         Admin.Ques FQ 
    LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID 
    WHERE   FQ.QuesID = Coalesce(@QuesID,QuesID)

SELECT TOP 1 @Lang=FQ.AdminLanguageID 
    FROM         Admin.Ques FQ 
    LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID 
    WHERE   FQ.QuesID = Coalesce(@QuesID,QuesID)

当您为select中的变量赋值时,选择列表中的所有项都必须为变量赋值。不只是其中一些。

答案 2 :(得分:0)

错误信息非常清楚 - 您无法在同一查询中组合变量赋值和普通结果集检索。

SELECT TOP 1 FQ.QuesID, 
FQ.Ques,
QuesAns as QuesAns,
FQ.QuesAns[Answers], 
FQT.QuesType ,
FQ.QuesTypeID, 
FQ.QuesParentID, 
FQ.Active,
FQ.AdminLanguageID,
@Lang=FQ.AdminLanguageID <---- this is the problem

拆分查询,一切都应该没问题。