Is there a way USING T-SQL to connect an unconnected query window in SSMS to a specific server?

  • What is driving the question is that I often write or troubleshoot stored procs that are complex or very long and in order to completely refresh the query window, I currently disconnect then reconnect the query window using the context menus and the graphical connection tools.

    It would help my productivity if I could temporarily plop some TSQL at the top of the proc that would redo the connection each time I ran it.

    Can this be done with T-SQL or would I have to use the command window?

    Thanks in advance. You guys are alway great 🙂

  • jshahan (1/30/2012)


    in order to completely refresh the query window

    I don't understand what you mean with this. Can you please clarify?

    -- Gianluca Sartori

  • I mean that I want to re-initialize everything particularly the temporary tables. I know I can drop them etc. but I would prefer a clean disconnect, reconnect.

  • No way to do this AFAIK.

    You'd better DROP/CREATE all the temporary objects you are using, which is a best practice anyway.

    -- Gianluca Sartori

  • Thanks for the response. 🙂

  • It's not exactly T-SQL, but can't you just put your query window in sqlcmd mode and use the :CONNECT command?

    See here for examples: http://www.mssqltips.com/sqlservertip/2311/using-the-ssms-query-editor-in-sqlcmd-mode/

    The query get executed on the server you name and then disconnects, so all temp objects will be dropped.

    [font="Verdana"]Markus Bohse[/font]

  • Or right-click anywhere in the query window and choose Connection -> Disconnect. Then do the same, choosing Connection -> Connect.

    John

  • It's not exactly T-SQL, but can't you just put your query window in sqlcmd mode and use the :CONNECT command?

    See here for examples: http://www.mssqltips.com/sqlservertip/2311/using-the-ssms-query-editor-in-sqlcmd-mode/

    Thanks Markus. This looks like the way I am going to have to go. I haven't used sqlcmd-moded before but it looks really straightforward and will probably help me a bit down the road as well.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply