Is there a TSQL command to connect to another server?
Or when you are in a Query Window, what are the Keyboard shortcuts to connect to another server and have a Query Window show up?
I have seen Ctrl+N pop up the Connect to Server dialog in some screens but when I am in a Query Window already and hit Ctrl+N it just opens up another Query Window.
The USE command lets you connect to other databases on the current server but is there a command that lets you connect to another server?
I am using SQL Server 2005.
You can use OpenDataSource with a linked server
OpenDataSource(provider_name, init_string)
For example
SELECT
FirstName,
Gender
FROM
OpenDataSource (
'SQLOLEDB',
'DataSource = NOLI\SQL2;UserID=myUserID;Password=myPassword'
).Organisation.dbo.Employees
From MSDN-
Like the OPENROWSET function,
OPENDATASOURCE should only reference
OLE DB data sources that are accessed
infrequently. Define a linked server
for any data sources accessed more
than several times. Neither
OPENDATASOURCE nor OPENROWSET provide
all the functionality of linked-server
definitions, such as security
management and the ability to query
catalog information. All connection
information, including passwords, must
be provided every time that
OPENDATASOURCE is called.
Either via the Menu...
Query > Connection > Change Connection
or via the mouse...
(Right Click Mouse Button) > Connection > Change Connection
Both will pop up the Connect to Database Engine dialog box
If your wanting to write some TSQL between servers then you'll need to create a Linked Server and then use OPENQUERY or OPENROWSET in your SQL. There are some good pointers in the previous posts on how to do this.
Another couple of options that may be viable depending on what you want to do are SQLCMD mode, and Registered Servers.
SQLCMD mode can be enabled under the query menu in SSMS. Once it's enabled you can do something like this with it:
:CONNECT SERVER1
SELECT ##SERVERNAME;
GO
:CONNECT SERVER2
SELECT ##SERVERNAME;
GO
With Registered Servers (should be under the View menu) you can set up groups of servers and execute queries against the groups all at once.
Both can be useful in many DBA scenarios, but I'm not sure if that's what you're after.
You have the choice of creating a Linked Server and use with OPENQUERY or use OPENROWSET.
If you are talking about, changing the connection to a query window, simply right-click in the query window and select change connection.
Once you have setup a linked server you can run TSQL against it by fully qualifying each table/view
select * from [Server].[Database].[Owner].Table
In this way you can talk to any server from any query window - if that's what you need. In most Sql you only ever supply the table, as everything else is defaulted. Using this technique you can even write join clauses between servers, as low as the distributed transaction coordinator (MSDTC) is running. Of course you'll only do that once to prove it works, as it runs incredibly slowly.
Related
Recently I run into a scenario, where
I created a table in a database.
Refreshed the tables in Object explorer
Table appeared in the list
I used the query 'select * from sys.tables' and selected the database in dropdown list
Executed the query
Table NOT in the list
We are using SQL server 2008 R2 and this issue is observed on our clients machine. Sadly, I don't know much about the history of the database.
Has anyone seen this issue before. Any little help will be greatly appreciated
Finally figured out the issue..
Issue is because we are using remote connections. It seems SQL query editor will remember the connection. So query editor is using one connection and Object explorer is using a different connection. They both are not pointing to the same server.
So when we created a table using SSMS, then we can see the table created, but not the same when used with query editor. As both the servers has same databases we always thought that it's the same server.
To change the connection of query editor, Right click on the sql query editor -> Connections -> Change connection.
With this we changed the connection of the query editor. And now both Object explorer and query editor are in sync, and we can see the table that is created.
Happy ending!!
#Mortb and #Manngo, once again many thanks for your contribution..
I am a beginner with SQL so I struggle with the MSDN description for creating a linked server in Management Studio.
I whant to link a SQL Server into another to use everything from ServerB on ServerA to e.g. provide one location other systems can connect to.
Both servers are in the same domain and both server have several databases inside.
When I start creating a linked server on ServerA in the general tap I select a name for the linked server and select SQL Server as Server type.
But I struggle on the Security tap. I have on both servers sa privilege so what is to set here?
Or which role should I take/crate for this connection?
My plan is to create views in a certain DB on ServerA with has also content of ServerB inside.
This views will be conusumed from an certain AD service user.
I already added this service user to the security on ServerA where the views are stored.
Do I also have to add this user somewhere on the linked ServerB?
I would recommend that you use Windows Authentication. Activate Security Delegation.
In the Security tab, choose "Add". Select your Windows user and check "Impersonate".
As a quick and dirty solution, you can choose "Be made using this security context" from the options list and enter a SQL Login which is valid on the remote server. Since quick and dirty solutions tend to last, i would strongly recommend to spend some time on impersonation.
In Server Objects => right click New Linked Server
2. The “New Linked Server” Dialog appears. (see below).
3. For “Server Type” make sure “Other Data Source” is selected. (The
SQL Server option will force you to specify the literal SQL Server
Name)
Type in a friendly name that describes your linked server (without spaces). – Select “Microsoft OLE DB Provider for SQL Server”
Product Name – type: SQLSERVER (with no spaces)
Datasource – type the actual server name, and instance name using this convention: SERVERNAMEINSTANCENAME
ProviderString – Blank
Catalog – Optional (If entered use the default database you will be using)
Prior to exiting, continue to the next section (defining security)
Click OK, and the new linked server is created
The easiest way would be to specify to make the connection using the current context, i.e. the user loging on to ServerA:
EXEC master.dbo.sp_addlinkedserver #server = N'ServerB', #srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'ServerB',#useself=N'True',#locallogin=NULL,#rmtuser=NULL,#rmtpassword=NULL
This however assumes that users accessing the linked server connection have the correct privileges on ServerB (part 2 of your question) - otherwise you'd need to use impersonation as described by Martin K.
I have table in Microsoft SQL server; and I'm using SPSS 14.1.
Now I want to connect SQL table to SPSS, so I can use SQL table from SPSS directly.
In SPSS, SQL database -> Data source -> Add New database connection -> Then I have 3 data source options only: dBASE Files, Excel Files, and MS Access Database. Which one can connect to SQL server?
You should open the Microsoft ODBC Administrator from Start or the Control Panel. There you should activate the System SDN tab, click Add... and add a SQL Server data source.
A wizard will open, where you can enter the details. First the name of the connection, which will be used as a reference by SPSS. Then optionally a description, and the ip address or name of the server.
On the next page select the authentication method, and if needed, enter the user and password. From this point, the default settings will most likely be good. If you will have character coding problems or something like that, you may need to change these settings.
I also use ODBC connection to connect to the database from SPSS, and it works well.
I hope this will help.
You may need an ODBC driver from MS or use one from the Data Access Pack for SPSS, but you will need a driver specifically for SQL Server.
Is there a tool for windows that we can use to inspect any SQL commands that go through a particular ODBC data source?
You can make ODBC log out everything it's doing:
http://support.microsoft.com/kb/274551
http://msdn.microsoft.com/en-us/library/ms711020%28VS.85%29.aspx
You can also do it programmatically:
... One can do this by calling SQLSetConnectAttr and set the SQL_ATTR_TRACE attribute in the connection to SQL_OPT_TRACE_ON. So, by doing this you would be enabling/disabling it for the connection duration.
http://decipherinfosys.wordpress.com/2009/01/17/odbc-tracing/
If you're using SQL Server, look at the SQL Server Profiler. Profiler allows you to monitor/trace all communications between your application and the SQL Server, including which procedures are called, parameter values, etc, without having to modify your application.
If you're using a different server, you should be able to find a sql proxy that will do the same thing.
In MS SQL Server, the Database Properties dialog has the "View Connection Properties" link over on the left. Clicking that brings the "Connection Properties" dialog with properties of the current connection, such as Authentication Method, Network Protocol, Computer Name, etc...
Is there a way to get that information programmatically by running a sql query? What would that query look like?
SQL 2005 and after you interrogate sys.dm_exec_connections. To retrieve your current connection properties you'd run:
select * from sys.dm_exec_connections
where session_id = ##SPID
The field values depend on the protocol used to connect (shared memory, named pipes or tcp) but all contain information about authentication method used, protocol and client net address.
Yes you can, but it depends on which property you are after as the ones displayed in the connection properties UI come from several places.
It uses several queries (such as xp_msver and select suser_sname()) to get hold of some properties, but it also uses the xp_instance_regread stored procedure to get hold of some values from the registry of the server.
Pretty much everything that is done is management studio when interacting with the SQL engine can be done using SQL. Starting a profiler session and doing the actions in the UI will uncover what (sometimes obscure/undocumented/unsupported) SQL is being run.
From client tool perspective you could use CONNECTIONPROPERTY:
For a request that comes in to the server, this function returns information about the connection properties of the unique connection which supports that request.
SELECT ConnectionProperty('net_transport') AS [Net transport],
ConnectionProperty('protocol_type') AS [Protocol type];
DBFiddle Demo
I think the answer is 'no'.
Computer information is stored on the computer.
Connection information is most likely stored in a configuraton file or in MS SQL Server.
But have a look at the MSSQL system tables and see what properties they have.