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..
@@SERVERNAME (Transact-SQL)Returns the name of the local server that is running SQL Server.
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.
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.
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:
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With