Linked Server in SQL Server? - sql-server

Is it possible to create database in linked server?
If yes then how.
I appreciate your help.
Thank You.

If your linked server allows it, then you can run sp_executesql remotely and by that means you can do absolutely anything on the linked server. Eg. create a database:
exec <linkedserver>.master.sys.sp_execute_sql N'create database foo';

See:
MSDN: Linking Servers
You basically need to call the sp_addlinkedserver stored proc:
sp_addlinkedserver
[ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Something like:
EXEC sp_addlinkedserver #server='S1_instance1', #srvproduct='',
#provider='SQLNCLI', #datasrc='S1\instance1'
For details, see the MSDN docs - it's really pretty good!

if you want create link server in sql server you can go 2 way:
1- write query.
2- use with SQL Server Management Studio with open Object Explorer and expand Server
Objects, right-click Linked Servers and then click New Linked Server.
I want to say, write query:
if you write this query you will create link server:
EXEC sp_addlinkedserver #server = [The_server_address_you_want_have_it]
EXEC sp_addlinkedsrvlogin [ #rmtsrvname = ] 'rmtsrvname'
[ , [ #useself = ] { 'TRUE' | 'FALSE' | NULL } ]
[ , [ #locallogin = ] 'locallogin' ]
[ , [ #rmtuser = ] 'rmtuser' ]
[ , [ #rmtpassword = ] 'rmtpassword' ]
for example:
EXEC sp_addlinkedserver #server = "1.1.1.1"
EXEC sp_addlinkedsrvlogin '1.1.1.1'
,'false'
,NULL
,'yes'
,'123'

Related

What is the 'when keyword' used for in T-SQL?

What is the when keyword used for in T-SQL?
when
NOTE: I tried searching this on the web (e.g. 'Googling')... however due to the ubiquitous nature of the word 'when', I wasn't able to find a good explanation.
Furthermore, a list of SQL keywords did not include 'when' so either the list was not exhaustive or it is unique to T-SQL (or perhaps it was added in some 'newer' version of T-SQL / SSMS). Link to this particular SQL keyword site:
https://www.w3schools.com/sql/sql_ref_keywords.asp
It's used in conjunction with the CASE keyword, which is like a switch, or 'if' statement essentially... for example:
SELECT
CASE WHEN [Column] = 1 THEN 'Column is 1'
WHEN [Column] = 2 THEN 'Column is 2'
ELSE 'Column is not 1 or 2'
END AS [Description]
WHEN is also part of MERGE statement:
Runs insert, update, or delete operations on a target table from the results of a join with a source table
MERGE
[ INTO ] <target_table> [ WITH ( <merge_hint> ) ] [ [ AS ] table_alias ]
USING <table_source>
ON <merge_search_condition>
[ WHEN MATCHED [ AND <clause_search_condition> ]
THEN <merge_matched> ] [ ...n ]
[ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]
THEN <merge_not_matched> ]
[ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]
THEN <merge_matched> ] [ ...n ]
;

How i communicate in two server.i want some data from A server to on B server.if i do it through Web socket if yes then how?

i want to access the data from server 1 to server 2.how can i do this things,can this possible through the p tunnel.i am try to sudo ptunnel -p 192.168.0.66 -lp 8080 -da 192.168.0.66 -dp 9090 this p tunnel on two server but it gives me error [err]: Failed to bind listening socket: Address already in use.
Follow these steps to create a Linked Server:
Server Objects -> Linked Servers -> New Linked Server
Provide Remote Server Name.
Select Remote Server Type (SQL Server or Other).
Select Security -> Be made using this security context and provide login and password of remote server.
Click OK and you are done !!
[HERE][1]ple tutorial for creating a linked server.
OR
You can add linked server using query.
Syntax:
sp_addlinkedserver [ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Read more about sp_addlinkedserver.
You have to create linked server only once. After creating linked server, we can query it as follows:
select * from LinkedServerName.DatabaseName.OwnerName.TableName

copy only tables with data from one database to another database

I have two database , dbOne(version - 10.50.1600 - locate in office server ) and dbTwo(version - 10.0.1600 - locate in my local server) .
I want to copy dbOne's tables with data to dbTwo .
Is there any way or script to do it ? I don't want to upgrade my local server-version !
"Import and Export Data" tool provided by SQL Server is a good tool to transfer data between two different servers.
How about generating the database scripts like in the following artcles
http://www.codeproject.com/Articles/598148/Generate-insert-statements-from
and
http://msdn.microsoft.com/en-us/library/ms186472(v=sql.105).aspx
Its possible to transfer data from one server to another server using SQL linked server query, if both are in a same network. below are the steps
Copying table structures
Generate script of all tables from server1 database then excute in server2 database. using Generate Script utility
Copying table data
sp_addlinkedserver [ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Insert into databaseserver2.db1.table1(columnList)
select columnList
from databaseserver1.db1.table1
Here are general steps you need to take in order for this to work
Migrating tables
Create scripts for tables in db1. Just right click the table and go to “Script table as -> Create to”
Re-order the scripts so that tables that don’t depend on any other tables are executed first
Execute scripts on db2
Migrating data
The most convenient way is to use SQL Server Import/Export wizard

Copying between servers

I need to copy data from a table on one server to the same table on another server. I tried this:
sp_addlinkedserver 'server1'
INSERT INTO [server2].[R2Op].dbo.[RefR2Ops]
SELECT * FROM server1.[R2Op].dbo.[RefR2Ops]
But I am getting an error on the second statement:
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
Could you please tell how I can fix that?
Thanks.
The answer is there. You have not given rights for the local service use to login remote server. You're command is running as default under the rights of NT AUTHORITY\ANONYMOUS LOGON.
you need to specfy the user credentials to access the linked server
see sp_addlinkedsrvlogin
sp_addlinkedsrvlogin [ #rmtsrvname = ] 'rmtsrvname'
[ , [ #useself = ] 'useself' ]
[ , [ #locallogin = ] 'locallogin' ]
[ , [ #rmtuser = ] 'rmtuser' ]
[ , [ #rmtpassword = ] 'rmtpassword' ]
If both servers are in the same ADS domain and your login in present on both servers, you'll probably have to add:
EXEC sp_addlinkedsrvlogin 'server1', 'true'
Otherwise check in which security context the connections to the linked server will be made.

Remote Computer Name in SQL Server

I have an app that run on many computers and connect to sql server
I want to log the machine names of that computers in a table every time they connect how can I do that
I want to know if there is a command like that
"Select ##MachineName"
It's up to you how you want to log this information, but HOST_NAME() returns the name of the workstation connecting to the server.
Create linked server : (allowing access to distributed, heterogeneous queries against OLE DB data sources.) using following command :
sp_addlinkedserver [ #server= ] 'server' [ , [ #srvproduct= ] 'product_name' ]
[ , [ #provider= ] 'provider_name' ]
[ , [ #datasrc= ] 'data_source' ]
[ , [ #location= ] 'location' ]
[ , [ #provstr= ] 'provider_string' ]
[ , [ #catalog= ] 'catalog' ]
Then access is like :
Select * from [server-name].[db-name].dbo.[tablename]
Also, make sure security login you are using on both the servers is same (or atleast exists on other server too).

Resources