Must declare the scalar variable

  • I am getting the following error with the SP shown below

    sp_EdicionImpresaDetalles 912,0,0,0,''

    Msg 137, Level 15, State 1, Line 1

    Must declare the scalar variable "@iEp_E_Id".

    ALTER PROCEDURE [dbo].[sp_EdicionImpresaDetalles]

    @iEp_Id int=0

    ,@iEp_E_Id int OUTPUT

    ,@iEp_R_Id int OUTPUT

    ,@iEp_Seccion int OUTPUT

    ,@sEp_Nombre NVARCHAR(100) OUTPUT

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    declare @sSQL varchar(5000)

    set @sSQL = 'SELECT @iEp_E_Id=Ep_E_Id, @iEp_R_Id=Ep_R_Id,@iEp_Seccion=Ep_Seccion,@sEp_Nombre=Ep_Nombre From tblEdicionPDF WHERE Ep_Id=' + CONVERT(varchar(1), @iEp_Id)

    exec(@sSQL)

    END


    Jean-Luc
    www.corobori.com

  • By using dynamic sql your parameters are going out of scope. If this is how you have to do things lookup sp_execute_sql in BOL and use it. You will have to do your parameter declarations again, but it will work. It actually looks like you do not need the dynamic SQL. Try this instead of the dynamic SQL:

    [font="Courier New"]SELECT  

       @iEp_E_Id=Ep_E_Id,

       @iEp_R_Id=Ep_R_Id,

       @iEp_Seccion=Ep_Seccion,

       @sEp_Nombre=Ep_Nombre  

    FROM

       tblEdicionPDF

    WHERE

       Ep_Id= CONVERT(VARCHAR(1), @iEp_Id)

    [/font]

  • I think error with dynamic sql

    ALTER PROCEDURE [dbo].[sp_EdicionImpresaDetalles]

    @iEp_Id int=0

    ,@iEp_E_Id int OUTPUT

    ,@iEp_R_Id int OUTPUT

    ,@iEp_Seccion int OUTPUT

    ,@sEp_Nombre NVARCHAR(100) OUTPUT

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    declare @sSQL varchar(5000)

    set @sSQL = 'SELECT @iEp_E_Id=Ep_E_Id, @iEp_R_Id=Ep_R_Id,@iEp_Seccion=Ep_Seccion,@sEp_Nombre=Ep_Nombre From tblEdicionPDF WHERE Ep_Id= CONVERT(varchar(1), @iEp_Id)'

    exec(@sSQL)

    END

    -VG

  • You're right that in this case I do not need dynamic SQL. I'll follow up your suggestion in the BOL for the other case where I really need it.


    Jean-Luc
    www.corobori.com

  • declare @sSQL varchar(5000)

    set @sSQL = 'SELECT @iEp_E_Id=Ep_E_Id, @iEp_R_Id=Ep_R_Id,@iEp_Seccion=Ep_Seccion,@sEp_Nombre=Ep_Nombre From tblEdicionPDF WHERE Ep_Id=' + CONVERT(varchar(1), @iEp_Id)

    exec(@sSQL)

    There is no need of Dynamic SQL in this context. You can Use straight sql as suggested above in one post.

    Still if you feel to go for dynamic sql , try using sp_executesql.

    You can pass the parameters and also get the output parameters with sp_executesql.

    Atif Sheikh

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

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

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