Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Server, like "use <<DatabaseName>>", how to "use <<ServerName>>" Query command

In SQL Server, like use <<DatabaseName>>, how to give use <<ServerName>> query command?

Right Click | Change Connection must be associated to some command. Isn't it. Just curious..

like image 646
Nara Avatar asked Apr 25 '14 15:04

Nara


People also ask

What is select @@ servername?

@@SERVERNAME (Transact-SQL)Returns the name of the local server that is running SQL Server.

What does @P mean in SQL?

This syntax FROM tablename p. or JOIN tablename p. create an alias for a table name. In most cases, it is simply a way to shorten your statement, because you can use a shorter name in place of a full table name.

How do I match a substring in SQL?

We can use LIKE Operator of SQL to search sub-string. The LIKE operator is used with the WHERE Clause to search a pattern in string of column. The LIKE operator is used in a conjunction with the two wildcards characters.


1 Answers

If you are attempting to fully switch connections1 within a single script and work with a new connection as opposed to sharing data between connections, then you can do this with SQLCMD mode. This ability / feature is specific to SQL Server Management Studio (SSMS) and the SQLCMD.EXE command-line utility as SQLCMD mode is directives to the client tool (either SSMS or SQLCMD.EXE) and not something that the database engine will be executing (or will even see or ever know about).

You can enable SQLCMD mode in SSMS on a per-session basis by going to the Query menu and selecting SQLCMD Mode. Once SQLCMD mode is enabled, you can change connections be using the :connect command:

SELECT @@SERVERNAME AS [ServerName], DB_NAME() AS [DbName]
GO
:connect DifferentServerName
SELECT @@SERVERNAME AS [ServerName], DB_NAME() AS [DbName]

Notes:

  • SQLCMD directives are processed at the beginning of each batch (since the entire batch is then sent to SQL Server for execution). Without using GO to separate the batches in the above example, the :connect command will take effect before the first SELECT. Comment out the GO and run again to see the effect.
  • When using SQLCMD Mode, Intellisense will not work.
  • SQLCMD Mode cannot be toggled on or off programmatically, but you can have SQLCMD Mode enabled for all new query windows by going to Tools | Options | Query Execution and checking the box for "By default, open new queries in SQLCMD mode".
  • There is no SQLCMD mode for the SQLCMD.EXE command-line utility; these commands always work in that tool.

1 Changing the connection logs you out of the current connection. This will drop temporary objects, rollback uncommitted transactions, etc., and local variables will obviously be out of scope (i.e. cannot cross connection boundaries). If statements executed on both servers need to share any info or objects, then you will need to create a Linked Server on one of them and use that Linked Server to connect to the other server on the current connection. Variables and temp objects still can't transfer between them, but you would then at least have the ability to construct Dynamic SQL containing that info to then execute on the remote server, and/or use the local resources in queries that specify four-part names for the remote objects.

like image 98
Solomon Rutzky Avatar answered Oct 01 '22 07:10

Solomon Rutzky