Get output of SQL Procedure and assign it to a variable used in WHERE Clause

  • Get output of SQL Procedure and assign it to a variable used in WHERE Clause

    Later I want to use this variable in my code in the WHERE Clause

    Declare @ProjectNo nvarchar(10)

    --Now I want to assign it to output of a storedprocedure which returns only 1 value and use it in the below SELECT query.

    SELECT ID from TABLEA where Project in (@ProjectNo)

    How to do it. How to assign @ProjectNo to output of storedProcedure called 'GetProjNumber'

    Please help

  • How does the procedure return that? Return code, output variable or resultset?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • ms-techie (3/25/2014)


    Get output of SQL Procedure and assign it to a variable used in WHERE Clause

    Later I want to use this variable in my code in the WHERE Clause

    Declare @ProjectNo nvarchar(10)

    --Now I want to assign it to output of a storedprocedure which returns only 1 value and use it in the below SELECT query.

    SELECT ID from TABLEA where Project in (@ProjectNo)

    How to do it. How to assign @ProjectNo to output of storedProcedure called 'GetProjNumber'

    Please help

    Is @ProjectNo a single value?

  • ms-techie (3/25/2014)


    Get output of SQL Procedure and assign it to a variable used in WHERE Clause

    Later I want to use this variable in my code in the WHERE Clause

    Declare @ProjectNo nvarchar(10)

    --Now I want to assign it to output of a storedprocedure which returns only 1 value and use it in the below SELECT query.

    SELECT ID from TABLEA where Project in (@ProjectNo)

    How to do it. How to assign @ProjectNo to output of storedProcedure called 'GetProjNumber'

    Please help

    Assuming that your select will always return 1 ID is not safe.

    This is what I would write:

    CREATE PROCEDURE GetProjNumber ( @ProjectNo int, @ID int OUTPUT)

    AS

    BEGIN

    SELECT @ID = TOP 1 ID from TABLEA where project in (@ProjectNo);

    END;

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply