One possibility that may work for you is to create a stored procedure. Stored procedures can pass values back into variables in the calling code. You mentioned that you didn't want to create a User function in your database. If you don't want to create permanent objects in your database, you create a local temporary stored procedure in the same way you create a local temporary table (by giving the procedure a name that begins with a hash mark (#)). Like local temporary tables, local temporary stored procedures can only be seen by the connection that created the procedure and automatically go away when the connection is closed. Unfortunately you cannot create temporary used defined function.
As an example of a stored proc that returns values to be used in a succeeding call, the following code creates a temporary stored proc (if you want a permanent stored proc, just remove the # from the name. It creates a Fibonacci series, each call returns the value to create the next number in the series.
Create Procedure #FibSequence (@LastNumber int Output, @PriorLastNumber int Output, @Sequence varchar(max) Output) As
Declare @NewValue int;
Set @NewValue = @LastNumber + @PriorLastNumber;
Set @PriorLastNumber = @LastNumber;
Set @LastNumber = @NewValue;
Set @Sequence = @Sequence + ',' + Cast(@NewValue As varchar(11));
go
Declare @LastNumber int = 1,
@PriorLastNumber int = 1,
@Sequence varchar(max) = '1,1';
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Exec #FibSequence @LastNumber Output, @PriorLastNumber Output, @Sequence Output;
Select @Sequence;
Tom