Need help with some stored procedure

后端 未结 3 609
遇见更好的自我
遇见更好的自我 2021-01-28 09:21

The stored procedure:

ALTER PROC [Admin].[sp_Ques]    
(
 @QuesID bigint
 )
AS
BEGIN
 IF @QuesID = 0
  SET @QuesID =NULL
SELECT     FQ.QuesID, FQ.Ques,QuesAns 
F         


        
相关标签:
3条回答
  • 2021-01-28 10:08

    The scope of FQ is limited to the first select statement.

    Your options include:

    1. Passing AdminLanguageID as a parameter as you have suggested
    2. Retrieving AdminLanguageID in a prior statement (select @AdminLanguageID = AdminLanguageID from...)
    3. Joining Admin.Language with Admin.Ques
    4. Using a subquery (select ... from Admin.Language where AdminLanguageID in (select AdminLanguageID from Admin.Ques where ...)
    0 讨论(0)
  • 2021-01-28 10:10

    Perhaps create a local variable to hold the LanguageID that's being retrieved. Assign a value to it during the previous SELECT. The addition of TOP 1 simply ensures that if/when you ever have multiple matches in the first query (indeed you will when @Ques is zero or null!), only one value is returned in that query, thereby allowing a single value into your variable.

    DECLARE @Lang int  --whatever datatype your QuesID is.
    
    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 TelerikLanguage FROM Admin.Language 
    WHERE AdminLanguageID=@Lang 
    
    0 讨论(0)
  • 2021-01-28 10:20

    Why not just join them into 1 select?

    ALTER PROC [Admin].[sp_Ques]    
    (
     @QuesID bigint
     )
    AS
    BEGIN
     IF @QuesID = 0
      SET @QuesID =NULL
    SELECT     FQ.QuesID, FQ.Ques,QuesAns as QuesAns,FQ.QuesAns[Answers], FQT.QuesType ,FQ.QuesTypeID, FQ.QuesParentID, FQ.Active,FQ.AdminLanguageID, AL.TelerikLanguage
    FROM         Admin.Ques FQ
    LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID
        LEFT JOIN Admin.Language AL ON AL.AdminLanguageID=FQ.AdminLanguageID
    WHERE   FQ.QuesID = QuesID OR @QuesID IS NULL
    
    END
    
    0 讨论(0)
提交回复
热议问题