SQL error sp_executesql

  • Hi,

    I have the code below. I receive an error and I don't understand why.

    Can someone help?

    thanks,

    PR

    Code:

    SET @SQLString = 'select @maxVal = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2 AND STATUS =0

    and cod_rep_fiscal ='''+@codrf+''''

    SET @ParmDefinition = '@maxVal bigint OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @maxVal = @MaxTotalidade OUTPUT

    SET @SQLString = 'select @MinVal = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =1 AND STATUS =0

    and cod_rep_fiscal ='''+@codrf+''''

    SET @ParmDefinition = '@MinVal bigint OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @MinVal = @MaxDiferenca OUTPUT

    Error Msg:

    Msg 137, Level 15, State 1, Line 1

    Must declare the scalar variable "@MinVal".

  • Looks like you have written @MinVal instead of @MaxDiferenca in your dynamic sql:

    SET @SQLString = 'select @maxVal = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =2 AND STATUS =0

    and cod_rep_fiscal ='''+@codrf+''''

    SET @ParmDefinition = '@maxVal bigint OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @maxVal = @MaxTotalidade OUTPUT

    SET @SQLString = 'select @MaxDiferenca = isnull(max(cod_sincronismo_fim),0)

    from '+@DB+'.dbo.sincronismo where

    tp_processo =''I'' and cod_tp_classificacao =1 AND STATUS =0

    and cod_rep_fiscal ='''+@codrf+''''

    SET @ParmDefinition = '@MinVal bigint OUTPUT'

    EXECUTE sp_executesql @SQLString, @ParmDefinition, @MinVal = @MaxDiferenca OUTPUT



    Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

    Concatenating Row Values in Transact-SQL[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

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