Reputation: 6962
Is there a way to force the results of a Select Statement to the Output window in SQL Server Management Studio before the end of the entire script is reached?
I have one select query that executes quickly, and one that takes a while to execute. I would like to look at the results from the first query while the second is running.
My issue is that the same variables are used in both queries and if a "GO" statement is used, the variables will be lost. Example below.
declare @MyVar int
set @MyVar = 1
-- Query with short execution time
select *
from MyTable t
where t.Column = @MyVar
GO -- @MyVar is lost
-- Query with long execution time
select *
from MyOtherTable o
where o.Column = @MyVar -- @MyVar no longer exists
Upvotes: 2
Views: 4300
Reputation: 8185
You can use raiseerror , which will cause the buffer to flush :
RAISERROR( 'Just to print everything',0,1) WITH NOWAIT
you can put this between the queries.
Upvotes: 10
Reputation: 135848
As an alternative, you could store your variable in a temp table.
create table #MyTable (MyVar int)
insert into #MyTable (MyVar) values (1)
-- Query with short execution time
select *
from MyTable t
where t.Column = (select MyVar from #MyTable)
GO -- #MyTable is preserved
-- Query with long execution time
select *
from MyOtherTable o
where o.Column = (select MyVar from #MyTable)
Upvotes: 3