SQL Server Querying Online - sql

I have two SQL Server database servers in two different server computers.
Server A - 192.168.1.100
Server B - 192.168.2.102
I need to execute a query from server A to retrieve data from a table in the Server B.
How to write the SQL select statement to perform this?
Are there any server configurations to allow these type of querying?

Add a linked server here are commands for 1 way of doing this. replace the user and password values with an appropriate SQL credential.
EXECUTE master.dbo.sp_addlinkedserver #server = N'192.168.2.102', #srvproduct=N'SQL Server'
EXECUTE master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'192.168.2.102',#useself=N'False',#rmtuser='ASQLLogin',#rmtpassword='Password'
then simply query like you would a normal table but append the linked server in front as commented above linked_server.db_name.schema_name.table_name like so:
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName]
You can even join it to your local server A if you want.
SELECT *
FROM
[192.168.2.102].[DatabaseName].[SchemaName].[TableName] b
INNER JOIN SomeTableOnServerA a
ON b.ID = a.ID

Related

Column name in SQL Server fully qualified name

I read in SQL Server documentation that I can specify column names in FROM clause of a simple SELECT query.
But when I try to run this query:
select * from my_db.dbo.test_table.test;
I get the following error:
Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in
sys.servers. Verify that the correct server name was specified. If
necessary, execute the stored procedure sp_addlinkedserver to add the
server to sys.servers.
I know this is a T-SQL page, and I'm trying to run .
Why does this happen?
I'm using SQL Server version 2017 via Microsoft SQL Server Management Studio version 2017 as well.
try rewriting from this
select * from my_db.dbo.test_table.test;
to this
select test_table.test,* from my_db.dbo.test_table;
the way it was written with that many Periods it assume you are trying to fully qualify the table so in what you had tried to the server is treating it as follows
my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror
the fields you want to show should directly follow the Keyword Select so if you only wanted 2 fields you could write
select test,test2 from my_db.dbo.test_table;
or simpler if you only have the one server
select test,test2 from dbo.test_table;
or if you only have the defailt Schema dbo (Database Base Owner)
select test,test2 from test_table;
I hope thaelps
You cannot specify columns in the FROM clause, but you can specify columns in the SELECT clause.
select test from my_db.dbo.test_table
Please run sp_addlinkedserver and sp_addlinkedsrvlogin SPs:
EXEC sp_addlinkedserver #server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'
After that, you can try to run: select * from my_db.dbo.test_table
Ref: https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/
Also, even though it is SQL Server 2014 related maybe helps, please see: Could not find server 'server name' in sys.servers. SQL Server 2014

How to Know who creates (host name) a database in sql server?

I want to know who created a database in sql server. Not the owner user, but the HOST_NAME or the machine name of the user who creates the database. Someone created a database on the server, but we do not know who it was. The only data we have is the user owner of the database, but that user is used by any developer. But what I want is to be able to identify the name of the machine from where the database is created. For example the host name of creation.
I tried with
select * from sys.databases
but it doesn't I need.
Then I tried with
Select
database_name,server_Name,Machine_Name,b.[name]as Backup_Name,Backup_Start_Date,Backup_Finish_Date
,Physical_Device_Name
from master.sys.databases a
Inner Join msdb..backupset
b on
a.Name = b.Database_Name
Inner Join msdb..backupmediafamily
c on c.media_set_id
= b.media_set_id
WHERE database_name = 'database'
But I also do not get the data I need
I'd use extended events for this:
CREATE EVENT SESSION [Create Database] ON SERVER
ADD EVENT sqlserver.database_created(
ACTION(sqlserver.client_hostname))
ADD TARGET package0.ring_buffer;
GO
ALTER EVENT SESSION [Create Database] ON SERVER STATE = START;
I leave the consumption of the target data as an exercise for the reader.
To Get Host Name
`SELECT HOST_NAME() AS HostName'
To Get Machine Name
SELECT serverproperty('Machinename') as Hostname
Note:
Host_Name means the sql server to which database is connected
Machinename means the machine name in which sql server is installed

How to make a query to join local database and linked server database

I have created a linked server and give the name as test.
When I write a query like
select *
from openquery(test, 'select * from account')
it works fine but when I run this query:
select *
from openquery(test, 'select * from account join test1 on account.accountid=test1.student_id')
it throws an error
OLE DB provider "SQLNCLI10" for linked server "test" returned message
"Deferred prepare could not be completed.".
I don't know what I am doing wrong to join both the server.
You can try as:
select * from test.master.dbo.account
join
test.master.dbo.test1
on account.accountid=test1.student_id
likewise:
select * from test.master.dbo.account
When any of the 2 server ( linked server and local server) is using ms sql 2012 and above. Then while creating the linked server make sure Under provider drop down we select SQL Server Native Client 11.0. The reason I was keep on getting the error is because under Provider I selected SQL Server Native Client 10.0 and my linked server is using sql2008 and my local server is sql2012
Solution
select * from test.account.dbo.account m
left join
test t
on m.accountid=t.student_id

how to update table from linked server in sql server?

I have few tables I need to load from linked firebird server into SQL Server from time to time. I use statement like this:
SELECT * into dekr FROM OPENQUERY ( [PLINK] ,'select * from dekr' )
It takes a while since it's going over network etc, is there a way to update once created
table dekr only with changes since last time?
thanks

How to update a table taking values to a second sql instance?

I have two SQL Server instances on two virtual machines, Test server and Production server.
I need to do an UPDATE to the production server taking data values from test server.
For example :
UPDATE [server_production].dbname.mytable
SET column1 = [server_test].dbname.mytable.column1
How do I do this ?
On your production server, define a Linked Server that points to your test server.
Then, on your production server you can run a query similar to the following to update your column:
UPDATE p
SET column1 = t.column1
from <dbname>.<schema>.mytable p
join <TestLinkedServerName>.<dbname>.<schema>.mytable t
on p.<id> = t.<id>
In the above query, you'll need to provide values for the placeholders:
<dbname> - the name of the prod/test database
<schema> - the schema in which the table is defined (typically dbo)
<TestLinkedServerName> - the name you've given to the linked server
<id> - your PK column, or a column that uniquely identifies rows and provides a way to join the two tables
Have a look at redgate sql data compare
http://www.red-gate.com/products/sql-development/sql-data-compare/