You could use sp_executesql to build a temporary SProc or function.
DECLARE @myVar AS INT
DECLARE @test AS nvarchar(500)
DECLARE @paramDef nvarchar(500)
SET @paramDef = N'@myParam INT'
SET @myVar = 10248
SET @test = N'USE Northwind; SELECT * from dbo.Orders where dbo.Orders.OrderID = @myParam'
EXEC sp_executesql @test, @paramDef, @myParam = @myVar;
http://msdn.microsoft.com/en-us/library/ms175170.aspx
You declare your variables, set up your SQl string to execute and then run it against sp_executesql. The syntax of the SProc is
sp_executesql [VARIABLE WITH SQL SOMMAND], [VARIABLE WITH DEFINITION OF THE PARAMETER], [VARIABLE WITH VALUE TO BE PASSED TO THE PARAMETER]
===
http://www.ElementalSQL.com/