Use servername in SSMS? - ssms

I've been working with multiple server on my SSMS. Is there any way to connect to particular server (on query) without having connect or "New Query" from Object Explorer Panel. I mean something like this on a query :
use dbname
Tried this but not working
use [servername].[dbo].[dbname]
It's a bit annoying, to add a new query just to connect to a particular server.
I also tried clicking Connect on the object explorer , it just connect to the server but not changing the current server.

What you are looking for are Linked Servers. You can get to them in SSMS from the following location in the tree of the Object Explorer:
Server Objects-->Linked Servers
or you can use sp_addlinkedserver.
You only have to set up one. Once you have that, you can call a table on the other server like so:
Select * from [ServerName].[DBname].[dbo].[TableName]

Related

Create alias for SQL server to use in query

My SQL server instance name is in the format: 'My-Server\InstanceName'
I want to give the server the alias 'InstanceName'. I did this in the Configuration Manager. When I connect to the server, I'm able to connect using that alias name. However, when I want to fully qualify objects in a query (eg. select * from InstanceName.Database.dbo.Table), it does not work. It says it cannot find that server in sys.servers. Why is this? It seems like it is not carrying the alias name down into the query after it connects.
Thanks!
InstanceName and alias are two different things.
For local server use Database.dbo.Table
To connect to another server (or another instance) you should add Linked Server
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-database-engine.
Usually you do not need any alias, but if you really want to do this use example:
EXEC sp_addlinkedserver
#server=N'S1_instance1',
#srvproduct=N'',
#provider=N'SQLNCLI',
#datasrc=N'S1\instance1'

SQL Server linked-server - cannot see all of the rows in target server's database table

I can't see all of the rows in the linked-server's tables. So if I run select count(*) from cmrez.dbo.reservations on the target server itself it returns 809472, but if I run the same query from a remote (linked) server I only get 78324. What's up with that!?
I seem to have come up with a solution. The target server supports mixed-mode logins, so I created a new login and I specify for the linked server to use that login and it appears to work. I don't know why I couldn't use pass-through security, but that doesn't matter as long as it works ;-).

How to retrieve records from server database into local database

I have a little problem. I want to create a query in my local database (tijdsregistratie.mdf) to retrieve rows from my server database (IT Solutions Develop.dbo) on server itshou-dev03\sql2008.
But I don't know how to connect to the server database. I tried it like this :
select TOP 10 * from [IT Solutions Develop].dbo.[IT Solutions BVBA$Planning]
.. but it gives me this error :
Invalid object name 'IT Solutions Develop.dbo.IT Solutions
BVBA$Planning'.
One way is to link the servers:
http://msdn.microsoft.com/en-us/library/ms188279.aspx?ppud=4
You can also define linked servers by using SQL Server Management
Studio. In the Object Explorer, right-click Server Objects, select
New, and select Linked Server. You can delete a linked server
definition by right-clicking the linked server name and selecting
Delete.
This is the process by which you tell SQL Server where another server is and how to connect to it. You can do this in SQL Server Management Studio or in T-SQL. You can then refer to the linked server by a four part name (similar to what is in your question):
[LinkedServerName].[Database].[Schema].[Object]

SQL Server Management Studio connection defaults to 'master' when selecting a database-specific object

In SQL Server 2008 R2 Management Studio, if I right-click on an object inside a specific database and choose "Select top 1000 rows ..", the database connection for the query window always opens on 'master' while the table name is fully qualified as [database].[dbo].[table]. This makes it impossible to jump in and tweak out this query and insert joins, etc., to the statement without also fully-qualifying everything I add, or add a USE statement, or select the database from the drop-down menu.
Is there a setting or something that will make query windows open with a database connection of the selected object browser's database rather than connect to 'master', and not fully qualify the object's database in the query text? I realize that I can register my SQL connection to default to my database, but we actually go through multiple new databases every week--in a given month I will have touched tens of databases--so it would be difficult to manage multiple database registrations. I would rather it if SSMS just connected to the specified database. Possible and straightforward?
If you are going in via Win Auth, are in a group, are using SA or some other userid, or are in a situation where changing your login is not really the solution, AND if all you wish to do is default to a database in the query editor:
In an existing open query editor, right-click, select Connection, Change Connection.
Click the Options button to expand the options.
In the Connection Properties tab, select the database you wish to connect to.
SSMS will remember your selection for that server. You may have to repeat for other servers, but it does remedy having a default database other than master.
There is no such setting for the SELECT TOP command, but you may be able to do this by changing the default database for your login. This is tedious if you're doing this often for various databases (much like changing the registrations, as I just noticed you already outlined).
Instead of using SELECT TOP 1000 (which in addition to not putting you in the right database context, also puts a TOP in that I assume you're just going to remove as well), you should right-click the table and choose Script Table as > SELECT to > New Query Window. This puts the context in the right DB, adds a USE command, doesn't have a TOP and doesn't database-prefix the table name.
If you want query window connects to some database by default, in SSMS go to the Security -> Logins, select the login that you use to connect to this server, and loock at the properties window. In page 'general' change the default database from 'master' to database you want to connect.
You could just put a USE [database name] at the top of the query window prior to executing a query. You do not need to fully qualify the database names if you do this. If you generate any scripts and version control them, this is a good practice to put at the top anyway. It at least prevents executing the script erroneously against the wrong database (say creation of a stored procedure).
USE MySpecialDatabase
GO
SELECT * FROM MySpecialTable

how to access table across multiple data server

I have two Sysbase servers , server1 and server2 . I have a stored procedure declared and running on server1. In that SP i want to access table from server2. How can i do that? Also my both instances are running in a UNIX box
You can do that, read more about "Proxy tables" at Sybase Infocenter (search for: create proxy_table statement) Afaik, there has to be remote server declared as well. I think it's documented well.
First, create a 'Remote Server' in the database that has the stored procedure.
You will need to tell it what the 'Server Type' is and how to connect (ODBC, for instance), then in the connection information, you can simply put the DSN name.
Now, you can create proxy tables to reference tables in your other server.
Try using the four part name. See here: http://www.dbforums.com/sybase/1001475-call-sql-server-stored-proc-sybase.html