Here is a simple example :
Create or alter PROCEDURE getPersonCountByLastName (
@lastName varchar(20),
@count int OUTPUT
)
As
Begin
select @count = count(personSid) from Person where lastName like @lastName
End;
Execute below statements in one batch (by selecting all)
1. Declare @count int
2. Exec getPersonCountByLastName kumar, @count output
3. Select @count
When i tried to execute statements 1,2,3 individually, I had the same error. But when executed them all at one time, it worked fine.
The reason is that SQL executes declare, exec statements in different sessions.
Open to further corrections.