Use 'GO' without losing variables? Is there an equivalent to GO or elegant workaround?

I set some variables that are calculated dates and want to see a check of those as soon as a long-running query is started.  

If I have an initial SELECT that displays the variables, it doesn't display until the query completes.  And of course, if I insert a GO command I lose the variable values.
I want the initial SELECT to execute (display) right away.

The only work-around I can think of (as a sql beginner) is to write my variables into a temp table, use GO, and then read them back out.  I hope there's a more elegant method.  Is there?

(main query displays all at once, not piecemeal, due to a sort on a calculated column)

Thanks,

Ed

DECLARE @variable varchar(20)
SET @variable = '"SEARCH CRITERIA"'

SELECT 'Starting a query based on the criteria of: ' + @variable + '.  This may take a while.' AS 'Intro that allows double check of criteria. ______________________________________'

--GO

WAITFOR DELAY '00:00:05.000'  --just to simulate the time required to run the query.  Often 10 minutes or more.
SELECT 'This is a long running query based on ' + @variable + '.' AS 'Main Query ________________________________________'

Open in new window

nosliwde99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SANDY_SKCommented:
Instead of using the select to display a message, cant you use the print command

print 'Starting a query based on the criteria of: ' + @variable + '.  This may take a while.' AS 'Intro that allows double check of criteria. ______________________________________'
0
Éric MoreauSenior .Net ConsultantCommented:
why do you need GO? I have been writting SQL for years and didn't had to use GO very often!
0
nosliwde99Author Commented:
No dice so far.  Thanks for the idea Sandy.  The PRINT only prints the message to the messages tab at the same time that the results are populated.   The sample code illustrates the problem nicely.  Other thoughts?
0
nosliwde99Author Commented:
So, I just wrote my variables into a temp table, used the GO to be able to execute the first query straight away, then read the variables back out of the temp table.  I'm guessing from the responses that there is no other good method.  Will leave open a bit longer to see if anyone comes up with a better solution.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nosliwde99Author Commented:
No good solution found - this was the best work-around (i.e. writing variables to a temp table, employing the 'GO', then reading the variables back out of the temp table).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.