The reason is that the variable referenced is out of scope.You can solve this matter by using a similar logic as this below.CREATE PROCEDURE dbo.uspMyProc( @LocalVal VARCHAR(10), @InParam1 VARCHAR(10), @InParam2 VARCHAR(10))ASSET NOCOUNT ONDECLARE @SQL VARCHAR(1000)CREATE TABLE #Temp ( VariableName VARCHAR(20), VariableValue VARCHAR(20) )INSERT #TempSELECT '@InParam1', @InParam1 UNION ALLSELECT '@InParam2', @InParam2SET @SQL = 'SELECT VariableValue FROM #Temp WHERE VariableName = ' + QUOTENAME(@LocalVal, '''')EXEC (@SQL)
and call with simpleEXEC dbo.uspMyProc '@InParam1', 'Peso', 'Yak'EXEC dbo.uspMyProc '@InParam2', 'Peso', 'Yak'
Or just do the easiest thing:IF @InParam = '@MyLocalVariable' SET @MyCursor = @MyLocalVaraibleELSE IF @InParam = '@AnotherVariable' SET @MyCursor = @AnotherVariable
E 12°55'05.25"N 56°04'39.16"